Skip to content

feat(proxy): start and stop a proxy's server from Flock - #110

Closed
omarabulkhair-66 wants to merge 19 commits into
flock-php:mainfrom
omarabulkhair-66:feat/proxy-commands
Closed

omarabulkhair-66 wants to merge 19 commits into
flock-php:mainfrom
omarabulkhair-66:feat/proxy-commands

Conversation

@omarabulkhair-66

@omarabulkhair-66 omarabulkhair-66 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Closes #108

Builds on #98 (Node.js sites): its commits come first here, since a proxy's command reuses the dev server pieces from it (process-group stop, logs). The work for this issue starts at 05ef1db, so please review it after #98, or only from that commit on.

What and why

A proxy can now have a command that starts its server, as in the mock-ups on #108. Flock runs it only when you click Start, and keeps it running until Stop. The proxy's page shows the command's output.

cd ~/Code/softpack
flock proxy softpack 3000 --command "npm run dev"   # runs in the current folder, or --dir
flock proxies start softpack
flock proxies stop softpack

# a server that runs in the background: start runs the command once, stop runs the stop command
flock proxy s3 9000 --command "docker compose up -d" --stop-command "docker compose stop"

Without a command, a proxy works just like now.

Screenshots (macOS)

A proxy demo with the command node server.js, a small Node.js server that logs each request:

Not started Starting
Running (requests to https://demo.test in its output) Stopped (the last runs' output stays)

What it does

  • config: command, dir and stop_command on a proxy.
    • dir is required with a command: an absolute folder that exists.
    • A stop command needs a command, and both are one line.
  • daemon:
    • Foreground command (no stop command):
      • It's a supervised process, proxy-<name>, run through the shell in its folder.
      • Its whole process group is stopped, since npm's child holds the port.
      • It's restarted when it exits, and its output goes to log/proxy-<name>.log.
      • Removing the proxy, or changing its command, stops or restarts it.
    • Background command (with a stop command): Start runs the command once, and Stop runs the stop command. Both outputs go to the same log.
    • The command gets Flock's shims and global npm tools first on PATH, so the project's PHP and Node.js are used. PORT is set when the upstream is a port on this machine.
    • A command never runs as root, as for Node.js sites.
    • What was started isn't kept across daemon restarts: a dev server only runs after a click.
  • status: command on a proxy has the command, folder, stop command, state (stopped, starting, running, failed), problem and log name. The state is worked out when the status is read, from the process and what answers at the upstream.
  • API:
    • POST /v1/proxies and PATCH /v1/proxies/{name} take command, dir and stop_command. An empty command removes the command and the stop command.
    • POST /v1/proxies/{name}/start and /stop.
    • DELETE /v1/logs/services/{name} empties a log, for the output's Clear.
  • CLI: --command, --dir and --stop-command on flock proxy. flock proxies start|stop <name>. flock proxies shows a command that's starting or failed.
  • MCP:
    • proxy_site takes command, dir (by default, the project the MCP server was started in) and stop_command.
    • A new proxy_server tool starts and stops the server.
  • apps:
    • macOS:
      • The proxy's page gets Command, Folder (Choose…) and Stop command.
      • Start or Stop sits next to the server's state, and the reason shows when it failed.
      • Output shows the log's last 300 lines, refreshed every second while the command runs, with Clear and Open Log, as in the mock-ups.
      • A new command runs in the folder of the same name in a project folder, if there is one, else in the home folder.
    • Windows and Linux:
      • The same fields, edited through a dialog (like the upstream), and Start or Stop.
      • Show Log opens the output in the Logs window.
      • They don't show the output inline. I kept those changes small, since I can't build either app here; I'm happy to add it once CI has built them.
  • docs: "Starting the server with Flock" in sites.md, plus the CLI reference and the MCP tools.

How I tested it

  • Go: make lint and go test -race ./... pass. go run ./tools/docslint passes, and the GOOS=windows and GOOS=linux builds pass.
    • daemon, end to end with the embedded Caddy, using the test binary as the server:
      • A proxy waits to be started. Start runs its command, and the site is served from it. The log has its output.
      • Stop frees the port, and so does removing a started proxy.
      • A background command's server keeps running after the command exits, and the stop command stops it.
      • A command that exits says so, and points to the log. A background command that fails shows its error. A proxy without a command can't be started.
    • config: the folder and command rules.
    • api: the command fields are kept when the upstream changes, and an empty command removes them. Start and stop work, and a log can be emptied (path traversal refused).
    • mcpserver: proxy_site with a command, and proxy_server start and stop.
  • macOS: swift build and swift test pass, including decoding a proxy's command and the new client calls.
  • By hand on macOS, with this branch's daemon and app installed:
    • A proxy with node server.js is Not running until started. Start makes it Starting, then Running on Flock's Node.js 26 (from the shims on PATH), with PORT set.
    • https://demo.test is served from it, and Output shows each request live.
    • Stop sends SIGTERM to its process group: the server logs it, the port is freed, and the domain shows Flock's waiting page again.
    • This caught a bug, fixed in c5da829 with a test: after Stop, the page showed the server running for up to 3s, because the status answered with the upstream's cached probe. Start and Stop now probe it again.
    • Start and Stop went through flock proxies start|stop, which is the same daemon call the buttons make. I couldn't script clicks into the SwiftUI window. The page updated by itself, as the screenshots show.
  • CI on my fork (the Build workflow, run) passes: the Windows and Linux apps compile and pass their tests (rustfmt and clippy included), and so do the Go tests on all platforms. One Windows rerun was for no port is free for both TCP and UDP in the daemon tests' freePort, which isn't touched here.

🤖 Generated with Claude Code

Omar Abulkhair and others added 11 commits November 4, 2026 22:33
… dev

A Next.js, Nuxt or Vite app in a project folder couldn't be a site: a
proxy only knows a port, so it had no folder, no Node.js version and no
way to start the app. A site can now run on Node.js (server "node"), next
to PHP-FPM and FrankenPHP. Flock runs `npm run dev -- --port <port>` (PORT
set too) in the folder with the site's Node.js, on a loopback port of its
own like a FrankenPHP site, restarts it when it exits and serves the
site's domains and HTTPS from it. A new Node.js version (isolated or
pinned) restarts it.

- supervisor: Spec.Group stops the process's whole group, since npm's
  child (the dev server) holds the port
- daemon: the dev server's state (starting, running, failed,
  unavailable), probed with the status; Vite is told the site's domains
  through __VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS
- cli: `flock node serve`, `unserve` and `restart`
- api / mcp: server "node" in PATCH /v1/sites/{name} and update_site
- macOS: "Node.js (npm run dev)" under Runs on, with the dev server's
  state, Restart and Show Log; the panel shows its Node.js version
- docs: Node.js apps

Share isn't available for a Node.js app yet. On Windows, stopping the
dev server ends npm; its children end with the daemon (job object).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Spec.Group only stopped the process group on unix. On Windows, killing
npm left its child (the dev server) holding the port until the daemon
exited; taskkill /T ends the tree.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Share: a Node.js app's share goes to its dev server, which is the app.
  It gets the site's own name as Host, and as Origin when there is one, so
  Vite's allowed hosts and Next.js's cross-site checks (hot reload too)
  take the tunnel's requests; forwarded headers are set as for PHP, and
  the env files and the git repository are never served.
- Worktrees of a Node.js site run a dev server of their own, on a port
  derived from the worktree's path (from 22000), so it stays the same
  while worktrees come and go.
- A command of the site's own (config `command`, PATCH /v1/sites/{name},
  `flock node serve --command`, update_site, Settings → Sites → Command)
  runs instead of npm run dev, through the shell in the project with
  node_modules/.bin on PATH; {port} becomes the port and PORT is set too.
  The status shows the command that runs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The status only had the command as it runs, {port} replaced, so the apps
prefilled Command with the port number: saving it again lost {port}.
node_server.configured is the command as set.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
As in the macOS app, so the platforms don't drift apart:
- Runs on offers Node.js (npm run dev), with the dev server's state, its
  command, Restart and Show Log. The Command box is prefilled with the
  site's own command as set ({port} and all), so saving it again keeps it.
- What only PHP needs is hidden for a Node.js site: the document root,
  the PHP version and extensions, and the FrankenPHP notes. Share stays,
  with a caption that says visitors reach the dev server.
- The panel shows a Node.js site's Node.js version, or that it doesn't run.

Flock.Kit decodes `node_server` (older daemons send none) and sends the
site's command, with tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
As in the macOS app, so the platforms don't drift apart:
- Runs on offers Node.js (npm run dev), with the dev server's state, its
  command, Restart and Show Log. The Command box is prefilled with the
  site's own command as set ({port} and all), so applying it again keeps it.
- What only PHP needs is hidden for a Node.js site: the document root,
  the PHP version and extensions, and the FrankenPHP notes. Share stays,
  with a caption that says visitors reach the dev server.
- The tray shows a Node.js site's Node.js version, or that it doesn't run.

The API models decode `node_server` (older daemons send none) and send the
site's command, with tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- A dev server that can't run (its port taken, say) leaves the site's
  names answering 503 with why, and out of shares, instead of passing
  them to whatever listens on the port.
- The dev server never runs as root, like a project's build scripts.
- A share blocks .env* (.envrc, .env-production) and a .git file or
  directory at any depth.
- Only the dev server's own requests (hot reload, its scripts) get the
  site's name as Origin, so the app's requests keep one that matches
  X-Forwarded-Host, as Next.js's Server Actions check.
- flock node restart refuses a site that isn't a Node.js app.
- Worktree ports skip ports configured for services and sites.
- Switching between FrankenPHP and Node.js stops the outgoing server
  before the port is checked.
- The site's own command is in the status even when the dev server
  can't run, so the apps edit it as it was set.
- A group stop waits for what the launcher started, killing it after
  the grace period, and a launcher that exits takes its group along.
- Windows and Linux keep an unsaved command across status refreshes,
  and Windows refreshes the editor when the command as set changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A proxy can have a command that starts its server, run through the shell
in a folder of its own with Flock's tools on PATH and PORT set. Flock
runs it when the proxy is started and keeps it running, with its output
in the proxy's log, until it's stopped. A stop command covers servers
that start in the background.

- config: command, dir and stop_command on a proxy, validated
- daemon: StartProxy/StopProxy, the command's state in the status
- api: POST /v1/proxies/{name}/start and /stop, DELETE of a service log
- cli: --command, --dir, --stop-command; flock proxies start|stop
- mcp: proxy_site takes the command, proxy_server starts and stops it

Refs flock-php#108

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A proxy's page has its Command, the folder it runs in and an optional
stop command, with Start and Stop next to the server's state.

- macOS: the command's output under the proxy, refreshed while it runs,
  with Clear and Open Log, as in the mock-ups on flock-php#108
- Windows and Linux: Show Log opens the output in the Logs window

Refs flock-php#108

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

Thanks for the PR, @omarabulkhair-66! Flock takes pull requests only for issues a maintainer has approved, so this one is closed. Edit this pull request's description to reference an approved issue, then save it to reopen automatically. See CONTRIBUTING.md.

@github-actions github-actions Bot closed this Nov 5, 2026
The status Stop answered with still had the upstream's last probe, so
the apps showed the server running for up to 3s after it stopped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot reopened this Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

Thanks, @omarabulkhair-66! This pull request references approved issue flock#108, so it has been reopened.

@omarabulkhair-66
omarabulkhair-66 marked this pull request as ready for review November 5, 2026 15:29
@omarabulkhair-66

Copy link
Copy Markdown
Contributor Author

@lukas-frey this is ready for review now that #108 is assigned. Could you approve the workflows so CI runs here? It's green on my fork (run). It builds on #98, so the work for #108 starts at 05ef1db.

@lukas-frey

Copy link
Copy Markdown
Member

Thanks, I'll have a look at it

@onairmarc onairmarc added this to the v1.1.0 milestone Nov 7, 2026
@onairmarc
onairmarc requested a balanced review from Copilot November 8, 2026 04:30
@onairmarc
onairmarc self-requested a review November 8, 2026 04:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical lifecycle and privilege issues can hang shutdown, restart services after Stop, leave background processes running, and execute configured stop commands as root.

11 open issues
What changed in this PR

Adds optional server commands to proxies, enabling Flock to start and stop upstream services and display their output. This builds on the included Node.js site support from #98 and addresses the proxy controls requested in #108.

Changes:

  • Adds proxy command configuration, lifecycle management, logs, and API, CLI and MCP controls.
  • Adds desktop controls across macOS, Windows and Linux, including inline proxy output on macOS.
  • Includes Node.js site supervision, worktree ports, sharing support, tests and documentation.
File Description
windows/​Flock.Tray/​ViewModels/​SitesViewModel.cs Tracks proxy commands and Node.js server changes.
windows/​Flock.Tray/​ViewModels/​PanelViewModel.cs Adds Node.js labels and state indicators.
windows/​Flock.Tray/​Settings/​SitesPane.xaml.cs Adds proxy controls and Node.js settings.
windows/​Flock.Tray/​Settings/​SitesPane.Share.xaml.cs Explains Node.js sharing exposure.
windows/​Flock.Tray/​PanelWindow.xaml Displays Node.js state dots.
windows/​Flock.Tray/​Features/​Sites/​SitesService.cs Connects proxy controls to client operations.
windows/​Flock.Kit/​Settings.cs Adds Node.js command updates.
windows/​Flock.Kit/​Models.cs Models proxy commands and Node.js status.
windows/​Flock.Kit/​FlockClient.cs Adds proxy command and lifecycle requests.
windows/​Flock.Kit.Tests/​DecodingTests.cs Tests new status models.
windows/​Flock.Kit.Tests/​ClientTests.cs Tests proxy requests and Node.js updates.
macos/​Tests/​FlockKitTests/​DecodingTests.swift Tests proxy and Node.js status decoding.
macos/​Tests/​FlockKitTests/​ClientTests.swift Tests proxy controls and log clearing.
macos/​Sources/​FlockUI/​Settings/​SitesPane.swift Adds command editing, lifecycle controls and output.
macos/​Sources/​FlockUI/​PanelView.swift Displays Node.js version and state.
macos/​Sources/​FlockKit/​Settings.swift Adds Node.js command updates.
macos/​Sources/​FlockKit/​Models.swift Models proxy commands and Node.js servers.
macos/​Sources/​FlockKit/​Client.swift Adds proxy controls and log clearing.
linux/​src/​tray.rs Adds Node.js tray labels and tests.
linux/​src/​panes/​sites.rs Adds proxy controls and Node.js settings.
linux/​src/​api.rs Adds client methods, models and serialization tests.
internal/​web/​web.go Returns 503 for unavailable site servers.
internal/​web/​share.go Routes Node.js shares with header and path protections.
internal/​web/​share_serve_test.go Tests Node.js share routing and protections.
internal/​supervisor/​supervisor.go Adds individual stops and process-group cleanup.
internal/​supervisor/​supervisor_test.go Tests descendant cleanup.
internal/​supervisor/​proc_windows.go Adds Windows process-tree termination.
internal/​supervisor/​proc_unix.go Adds Unix process-group termination and checks.
internal/​node/​npm.go Extracts reusable npm command construction.
internal/​mcpserver/​tools_write.go Adds proxy lifecycle tools and Node.js updates.
internal/​mcpserver/​tools_read.go Exposes Node.js server status.
internal/​mcpserver/​server.go Extends the daemon interface for proxy controls.
internal/​mcpserver/​mcpserver_test.go Tests new tool inputs and operations.
internal/​daemon/​share.go Shares Node.js sites without PHP asset preparation.
internal/​daemon/​proxyprobe.go Invalidates cached upstream probes.
internal/​daemon/​proxycommand.go Implements proxy command execution and status.
internal/​daemon/​proxycommand_test.go Tests foreground and background proxy commands.
internal/​daemon/​nodesite.go Constructs and monitors Node.js dev servers.
internal/​daemon/​nodesite_test.go Tests Node.js sites, worktrees and failures.
internal/​daemon/​frankenphp.go Coordinates server switching and Node.js restarts.
internal/​daemon/​daemon.go Integrates command supervision, routing and status.
internal/​daemon/​daemon_test.go Adds a fake Node.js server entry point.
internal/​config/​worktrees.go Assigns Node.js worktree ports.
internal/​config/​worktrees_test.go Tests port stability and conflict avoidance.
internal/​config/​proxies.go Adds and validates proxy command settings.
internal/​config/​proxies_test.go Tests command and folder validation.
internal/​config/​config.go Adds Node.js server configuration and command validation.
internal/​config/​config_test.go Tests Node.js port assignment.
internal/​cli/​versions.go Registers Node.js site commands.
internal/​cli/​status.go Displays Node.js server status.
internal/​cli/​proxy.go Adds command flags and proxy start/stop commands.
internal/​cli/​nodesite.go Adds Node.js serve, unserve and restart commands.
internal/​cli/​frankenphp.go Reports Node.js site server details.
internal/​api/​content.go Adds service-log clearing.
internal/​api/​configapi.go Adds command settings and proxy lifecycle endpoints.
internal/​api/​api.go Defines command status models and backend methods.
internal/​api/​api_test.go Tests settings, lifecycle endpoints and log clearing.
docs/​sites.md Documents proxy commands and controls.
docs/​node.md Documents managed Node.js apps and sharing.
docs/​cli.md Lists new proxy and Node.js commands.
docs/​ai.md Documents expanded MCP tools.

Review effort: Balanced

Comment thread internal/daemon/daemon.go Resolved
Comment thread internal/daemon/proxycommand.go Resolved
Comment thread internal/daemon/proxycommand.go Resolved
Comment thread internal/daemon/proxycommand.go Resolved
Comment thread internal/supervisor/proc_windows.go Resolved
Comment thread internal/api/configapi.go Resolved
Comment thread internal/daemon/proxycommand.go Resolved
Comment thread internal/daemon/proxycommand.go Resolved
Comment thread internal/mcpserver/tools_write.go Resolved
Comment thread linux/src/panes/sites.rs Resolved

@onairmarc onairmarc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please address copilot changes

Omar Abulkhair and others added 5 commits November 8, 2026 19:34
… its launcher exited

When a stop ran out of grace, endGroup got the timer channel already read, so a
group that stayed alive could block Stop, reload and shutdown for good. It now
gets a second more.

On Windows a group's launcher gets a job of its own: taskkill /T can't find a
dev server once npm itself exited, the job still can.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The entry owned its activate handler, which owned the entry back, so every
detail rebuild leaked the row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Address the review of the proxy commands:

- Stop ends a background command that's still starting before it runs the
  stop command, so the server can't come up after Stop.
- A failed stop command leaves the proxy started, so Stop can be tried again.
- Removing a started background proxy, removing its command, or shutting the
  daemon down runs its stop command (the one it was started with).
- Stop holds the reload lock while it stops a foreground command, so a reload
  can't start it again.
- A stop command never runs as root either, and a command ended early (its
  timeout, or Stop) is killed with its whole group, not only the shell.
- The API only drops the stop command when the command is removed, so a stop
  command alone is refused instead of ignored.
- proxy_site sets a folder given without a command.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@onairmarc onairmarc closed this Nov 8, 2026
@onairmarc onairmarc removed this from the v1.1.0 milestone Nov 8, 2026
@flock-php flock-php locked and limited conversation to collaborators Nov 8, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Start/stop proxy via UI

4 participants