iop/apps/control-plane/cmd/control-plane/http_fleet_handlers.go
toki 9024c852df refactor: architecture refactor - fleet polling & client state split
- Move fleet polling logic to control orchestrator
- Split client state management into separate controller/repository
- Add client bootstrap and home page components
- Update HTTP fleet handlers and views
- Add unit tests for client bootstrap and status controller
- Archive completed subtask documents
2026-06-06 19:10:47 +09:00

46 lines
1.8 KiB
Go

package main
import (
"encoding/json"
"net/http"
"iop/apps/control-plane/internal/wire"
)
func registerFleetHandlers(mux *http.ServeMux, registry *wire.EdgeRegistry, requestStatus edgeStatusRequester, sendCommand edgeCommandSender) {
registerFleetHandlersWithOptions(mux, registry, requestStatus, sendCommand, fleetOptions{})
}
// registerFleetHandlersWithOptions wires the fleet endpoints to a fleetService
// built from the given options. It exists so tests can inject timeout,
// concurrency, and cache policy without changing the default production wiring.
func registerFleetHandlersWithOptions(mux *http.ServeMux, registry *wire.EdgeRegistry, requestStatus edgeStatusRequester, sendCommand edgeCommandSender, opts fleetOptions) {
service := newFleetService(registry, requestStatus, sendCommand, opts)
mux.HandleFunc("/fleet/status", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
methodNotAllowed(w, http.MethodGet)
return
}
writeJSON(w, http.StatusOK, service.Status())
})
mux.HandleFunc("/fleet/commands", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
methodNotAllowed(w, http.MethodPost)
return
}
var body edgeCommandRequestBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
return
}
if body.Operation == "" {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "operation is required"})
return
}
if sendCommand == nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "command dispatch is not configured"})
return
}
writeJSON(w, http.StatusAccepted, service.Command(body.Operation, body.TargetSelector, body.Parameters))
})
}