Commit Graph
2 Commits
Author SHA1 Message Date
yetone 82f3befd29 daemon: magpie daemon install --web serves the browser page from the service, with a link that lasts (HuanCheng Bai)
HuanCheng Bai on Discord runs `magpie daemon` on a Linux server and asked
for the daemon to serve the web page too, or for an option to turn it on.
`magpie web` run beside it is a second process, and he read it as not
seeing the daemon's state.

- `magpie serve --web[=host:port]` serves the gateway and `magpie web`'s
  page from one process (stats, StartWeb, Wait). The default page address
  is 127.0.0.1:3430. Any other argument is refused, where before it was
  ignored.
- `magpie daemon install --web | --lan | --addr host:port | --no-web` and
  `magpie daemon web [on|off] [--lan] [--addr host:port]`. The unit's
  ExecStart and the plist's ProgramArguments become `serve --web=<addr>`,
  and the key goes into the 0600 file's environment as MAGPIE_WEB_KEY. The
  key is made once. A reinstall with no flags keeps the page and its key
  (read back from the file by daemon.Web), so the link survives restarts
  and reinstalls. `web off` drops it. MAGPIE_WEB_KEY in the shell, when
  set, is used and checked like magpie web's. The page defaults to
  loopback; --lan or a non-loopback --addr prints the network links and
  magpie web --lan's warning.
- `magpie daemon status`, install and `daemon web` print the page's link.
- `magpie web` while the service serves a page prints that page's link and
  exits. Beside a gateway another magpie serves, it says so and points to
  `magpie daemon web on`.
- docs/reference.md (the service section and the Use list), and the
  site's start page (en, zh, ja). The READMEs don't cover the daemon.

Run:
- Tests: TestInstallWithWeb (systemd and launchd, plutil lint, read back,
  reinstall without the page, the shell's key not carried, no key
  refused), TestWebNotInstalled, TestParseDaemon, TestWebOptions,
  TestDaemonStatusPrintsPage and TestServeArgs. Each of these went red
  with its line reverted by hand: withKey, prevKey in webOptions, and the
  status page line.
- Sandbox HOME with a fake launchctl:
  - `daemon install --web --lan` wrote a 0600 plist with
    `serve --web=0.0.0.0:3430` and the key. plutil said OK, and it printed
    the link, the network links and the warning.
  - Reinstalling with no flags kept the key. `daemon status` showed the
    link.
  - `daemon web off` / `daemon web` / `daemon web on` behaved as above,
    with a new key after off.
  - `daemon web bogus` and `serve --lan` gave the usage errors.
- `serve --web=127.0.0.1:3462` served the gateway (3461) and the page in
  one process. The page answered 200 with the key and 401 without, and the
  page and /v1/models both showed 3 models. `magpie web --no-open` beside
  it printed the service's link and exited 0.
- gofmt -l; go vet (gui), and go vet -tags nogui for darwin, linux and
  windows; go test -tags nogui ./... in a sandbox HOME.

Not tried: a real systemd on Linux. The unit text is checked by test only.
2026-10-14 08:32:41 +08:00
yetone 5617281f55 cli: magpie daemon install|uninstall|start|stop|restart|status keeps the gateway running as a service (HuanCheng on Discord)
What changed: a new internal/daemon package and `magpie daemon`, which run
`magpie serve` as the user's own service, with no window and no desktop
session.
- Linux: a systemd user unit at ~/.config/systemd/user/magpie.service
  (Restart=on-failure), driven by systemctl --user. status reads
  `systemctl --user show`, and says `sudo loginctl enable-linger $USER`
  when the user doesn't linger. su/sudo without the user's bus, no
  systemd, and WSL without systemd each get an error that says what to
  do.
- macOS: a launch agent, com.yetone.magpie.serve (RunAtLoad, KeepAlive on
  failure), loaded with launchctl bootstrap gui/$UID. Its log is
  ~/Library/Logs/magpie/serve.log.
- Windows and Android: refused, pointing to `magpie autostart on` (or to
  WSL).
- The service carries PATH, the proxy variables, MAGPIE_*, the XDG folders
  and the agents' folder variables from the install shell. The file is
  0600 because a proxy can hold a password. It runs magpie by its PATH
  name when that is the same file, so Homebrew's link survives updates.
  It refuses a go-build binary and an AppTranslocation copy.
- Install is refused while `magpie autostart` is on: the tray app serves
  the gateway too, and the two would crash-loop on the port.
- Docs: a reference.md section, and one sentence on the site's start page
  (en, zh, ja).

Why: users on headless Linux boxes asked for a supported way to keep
`magpie serve` up across reboots instead of hand-written units. The
existing `magpie autostart` needs a desktop session (an XDG autostart
entry).

What was run:
- gofmt -l; go vet on darwin, GOOS=linux -tags nogui, and GOOS=windows.
- go test ./internal/daemon ./internal/cli on macOS, at origin/main
  5b9530b56.
- The daemon tests cross-built for linux/arm64 and run in a Debian
  container.
- The tests use fake systemctl, launchctl and loginctl on PATH in a test
  home.
- Each guarantee was broken by hand and its test went red: the % escape,
  the autostart refusal, the 0600 chmod, the bootout before re-bootstrap,
  the go-build refusal, the bus advice, the last-exit filter and the
  PATH link.
- Real use on Linux: a Debian bookworm container running systemd as PID 1,
  with the linux/arm64 build, as an unprivileged user.
  - install: the gateway answered on :3425, and the unit was 0600.
  - kill -9: systemd restarted it 5s later.
  - Container restart with linger on: it came back by itself.
  - restart, stop, start and uninstall worked, and the unit and its wants
    link were gone afterwards.
  - Under sudo without XDG_RUNTIME_DIR it printed the bus advice.
- macOS: the darwin build ran `magpie daemon install` under a sandbox HOME
  with a fake launchctl. plutil -lint passed on the plist, and the calls
  were print and then bootstrap.

What wasn't: no real launch agent was loaded on a Mac (deliberately: never
on this machine), so launchd's own acceptance of the plist is checked
only by plutil. The linger advice was checked by the unit test, not seen
from a real SSH login. Windows was not run beyond vet.
2026-10-13 17:05:59 +08:00