devbox/internal/control/server.go
shoemoney 9f90f2c147 refactor(M8): drop the control /events SSE stream — no consumer yet (ponytail) ✂️
ponytail-review caught it: publishActivity -> ctrl.Publish -> broker -> SSE
was a producer with nothing on the other end. GET /events only feeds the
M11 TUI, which is demand-driven and not built. /state + /pause + /resume
cover every real caller (devbox status/pause/resume). Cut the broker,
handleEvents, Publish, publishActivity, and the ctrl handle — re-add the
(small, known) fan-out when a TUI actually consumes it. Net -100 lines.
2026-06-23 10:50:45 -05:00

102 lines
3 KiB
Go

//go:build !windows
package control
import (
"context"
"encoding/json"
"net"
"net/http"
"os"
"time"
)
// Daemon is the slice of the running daemon the control server steers. The
// daemon package implements it; kept here so the server depends on a behaviour,
// not on the daemon package (no import cycle).
type Daemon interface {
StateSnapshot() State // live per-mount + paused view for GET /state
Pause() // stop syncing until resumed
Resume() // clear pause + catch up all mounts
}
// Server hosts the control API on a Unix socket. The zero value is unusable;
// build one with Serve.
type Server struct {
d Daemon
ln net.Listener
srv *http.Server
sock string
}
// Serve binds the control socket under dir (mode 0600, stale socket removed
// first) and serves the control API until ctx is cancelled, then cleans up. It
// returns once the listener is bound; the serving runs in the background. A bind
// failure is returned so the caller can warn-and-continue — sync must not depend
// on the control plane.
//
// ponytail: GET /events (an SSE activity stream) is intentionally NOT served — it
// only feeds the M11 TUI, which doesn't exist yet. Add the route + a fan-out
// broker when a real consumer lands; today /state + /pause + /resume cover every
// caller (devbox status/pause/resume).
func Serve(ctx context.Context, dir string, d Daemon, logf func(string, ...any)) (*Server, error) {
sock := SockPath(dir)
if err := os.MkdirAll(dir, 0o700); err != nil {
return nil, err
}
// A stale socket from a crashed daemon would make Listen fail with
// "address already in use"; remove it first (it's ours by location).
if err := os.Remove(sock); err != nil && !os.IsNotExist(err) {
return nil, err
}
ln, err := net.Listen("unix", sock)
if err != nil {
return nil, err
}
// Tighten to owner-only: the socket is the daemon's authority boundary.
if err := os.Chmod(sock, 0o600); err != nil {
ln.Close()
os.Remove(sock)
return nil, err
}
s := &Server{d: d, ln: ln, sock: sock}
mux := http.NewServeMux()
mux.HandleFunc("GET /state", s.handleState)
mux.HandleFunc("POST /pause", s.handlePause)
mux.HandleFunc("POST /resume", s.handleResume)
s.srv = &http.Server{Handler: mux}
go func() {
if err := s.srv.Serve(ln); err != nil && err != http.ErrServerClosed && logf != nil {
logf("control server: %v", err)
}
}()
go func() {
<-ctx.Done()
s.close()
}()
return s, nil
}
func (s *Server) close() {
shutCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_ = s.srv.Shutdown(shutCtx)
_ = os.Remove(s.sock)
}
func (s *Server) handleState(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(s.d.StateSnapshot())
}
func (s *Server) handlePause(w http.ResponseWriter, _ *http.Request) {
s.d.Pause()
w.WriteHeader(http.StatusOK)
}
func (s *Server) handleResume(w http.ResponseWriter, _ *http.Request) {
s.d.Resume()
w.WriteHeader(http.StatusOK)
}