From e8a84d3e41c18316392f9acffe108198b7d86f01 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 3 Nov 2026 08:34:51 +1100 Subject: [PATCH] refactor(webui): extract the control-plane and logs modules (#1778) --- ...{api_control_plane.go => control_plane.go} | 45 +++++++++++++------ internal/webui/{api_logs.go => logs.go} | 21 +++++++-- internal/webui/server.go | 7 +-- 3 files changed, 51 insertions(+), 22 deletions(-) rename internal/webui/{api_control_plane.go => control_plane.go} (74%) rename internal/webui/{api_logs.go => logs.go} (86%) diff --git a/internal/webui/api_control_plane.go b/internal/webui/control_plane.go similarity index 74% rename from internal/webui/api_control_plane.go rename to internal/webui/control_plane.go index 13d48739d..a5b0a0bcd 100644 --- a/internal/webui/api_control_plane.go +++ b/internal/webui/control_plane.go @@ -1,6 +1,7 @@ package webui import ( + "context" "crypto/rand" "encoding/hex" "encoding/json" @@ -46,12 +47,30 @@ type controlPlaneHistoryResponse struct { Revisions []controlPlaneRevision `json:"revisions"` } -func (s *Server) handleControlPlaneCatalog(w http.ResponseWriter, r *http.Request) { - if s.ControlPlane == nil { +// controlPlaneAPI owns the generic /api/control-plane resource routes. A nil +// client answers 503. +type controlPlaneAPI struct { + controlPlane controlpb.ControlPlaneServiceClient + actorNames func(context.Context) func(string) string +} + +func newControlPlaneAPI(s *Server) *controlPlaneAPI { + return &controlPlaneAPI{controlPlane: s.ControlPlane, actorNames: s.actorNames} +} + +func (a *controlPlaneAPI) register(mux *http.ServeMux) { + mux.HandleFunc("GET /api/control-plane/catalog", a.handleControlPlaneCatalog) + mux.HandleFunc("GET /api/control-plane/resources/{kind}", a.handleControlPlaneQuery) + mux.HandleFunc("GET /api/control-plane/resources/{kind}/history", a.handleControlPlaneHistory) + mux.HandleFunc("POST /api/control-plane/resources/{kind}/commands/{command}", a.handleControlPlaneCommand) +} + +func (a *controlPlaneAPI) handleControlPlaneCatalog(w http.ResponseWriter, r *http.Request) { + if a.controlPlane == nil { http.Error(w, "control plane unavailable", http.StatusServiceUnavailable) return } - response, err := s.ControlPlane.Catalog(r.Context(), &controlpb.CatalogRequest{}) + response, err := a.controlPlane.Catalog(r.Context(), &controlpb.CatalogRequest{}) if err != nil { writeControlPlaneError(w, err) return @@ -59,12 +78,12 @@ func (s *Server) handleControlPlaneCatalog(w http.ResponseWriter, r *http.Reques writeJSON(w, response) } -func (s *Server) handleControlPlaneQuery(w http.ResponseWriter, r *http.Request) { - if s.ControlPlane == nil { +func (a *controlPlaneAPI) handleControlPlaneQuery(w http.ResponseWriter, r *http.Request) { + if a.controlPlane == nil { http.Error(w, "control plane unavailable", http.StatusServiceUnavailable) return } - response, err := s.ControlPlane.Query(r.Context(), &controlpb.QueryRequest{Kind: r.PathValue("kind")}) + response, err := a.controlPlane.Query(r.Context(), &controlpb.QueryRequest{Kind: r.PathValue("kind")}) if err != nil { writeControlPlaneError(w, err) return @@ -72,18 +91,18 @@ func (s *Server) handleControlPlaneQuery(w http.ResponseWriter, r *http.Request) writeJSON(w, controlPlaneResourceResponse{Resource: controlPlaneResourceView(response.Resource)}) } -func (s *Server) handleControlPlaneHistory(w http.ResponseWriter, r *http.Request) { - if s.ControlPlane == nil { +func (a *controlPlaneAPI) handleControlPlaneHistory(w http.ResponseWriter, r *http.Request) { + if a.controlPlane == nil { http.Error(w, "control plane unavailable", http.StatusServiceUnavailable) return } - response, err := s.ControlPlane.History(r.Context(), &controlpb.HistoryRequest{Kind: r.PathValue("kind")}) + response, err := a.controlPlane.History(r.Context(), &controlpb.HistoryRequest{Kind: r.PathValue("kind")}) if err != nil { writeControlPlaneError(w, err) return } revisions := make([]controlPlaneRevision, 0, len(response.Revisions)) - actorName := s.actorNames(r.Context()) + actorName := a.actorNames(r.Context()) for _, revision := range response.Revisions { // The value is real, not redacted: the dashboard stages a revision and // applies it as a replace, so a redacted value would restore the marker. @@ -100,8 +119,8 @@ func (s *Server) handleControlPlaneHistory(w http.ResponseWriter, r *http.Reques writeJSON(w, controlPlaneHistoryResponse{Revisions: revisions}) } -func (s *Server) handleControlPlaneCommand(w http.ResponseWriter, r *http.Request) { - if s.ControlPlane == nil { +func (a *controlPlaneAPI) handleControlPlaneCommand(w http.ResponseWriter, r *http.Request) { + if a.controlPlane == nil { http.Error(w, "control plane unavailable", http.StatusServiceUnavailable) return } @@ -115,7 +134,7 @@ func (s *Server) handleControlPlaneCommand(w http.ResponseWriter, r *http.Reques http.Error(w, "cannot create request ID", http.StatusInternalServerError) return } - response, err := s.ControlPlane.Command(r.Context(), &controlpb.CommandRequest{ + response, err := a.controlPlane.Command(r.Context(), &controlpb.CommandRequest{ Kind: r.PathValue("kind"), Command: r.PathValue("command"), ValueJson: request.Value, ExpectedVersion: request.ExpectedVersion, Actor: string(audit.ActorID), Source: audit.Source, RequestId: audit.RequestID, diff --git a/internal/webui/api_logs.go b/internal/webui/logs.go similarity index 86% rename from internal/webui/api_logs.go rename to internal/webui/logs.go index ebcd696ba..240829389 100644 --- a/internal/webui/api_logs.go +++ b/internal/webui/logs.go @@ -17,7 +17,20 @@ import ( // LogSource reads one service's bounded recent log over its contract. type LogSource func(context.Context, logging.Query) (logging.Result, error) -func (s *Server) handleLogs(w http.ResponseWriter, r *http.Request) { +// logsAPI owns GET /api/logs. With no sources it reads this process's own +// feed; with neither it reports logs disabled. +type logsAPI struct { + sources map[string]LogSource + feed *logging.Feed +} + +func newLogsAPI(s *Server) *logsAPI { return &logsAPI{sources: s.LogSources, feed: s.LogFeed} } + +func (a *logsAPI) register(mux *http.ServeMux) { + mux.HandleFunc("GET /api/logs", instanceOnly(a.handleLogs)) +} + +func (a *logsAPI) handleLogs(w http.ResponseWriter, r *http.Request) { q := r.URL.Query() limit := logging.DefaultTailLines if raw := strings.TrimSpace(q.Get("limit")); raw != "" { @@ -30,9 +43,9 @@ func (s *Server) handleLogs(w http.ResponseWriter, r *http.Request) { limit = min(parsed, logging.MaxTailLines) } } - sources := s.LogSources + sources := a.sources if len(sources) == 0 { - sources = map[string]LogSource{presence.UI: func(_ context.Context, q logging.Query) (logging.Result, error) { return s.LogFeed.Read(q) }} + sources = map[string]LogSource{presence.UI: func(_ context.Context, q logging.Query) (logging.Result, error) { return a.feed.Read(q) }} } service := q.Get("service") owner := service @@ -55,7 +68,7 @@ func (s *Server) handleLogs(w http.ResponseWriter, r *http.Request) { entries = entries[len(entries)-limit:] truncated = true } - writeJSON(w, map[string]any{"entries": entries, "components": components, "services": services, "errors": errors, "truncated": truncated, "disabled": len(s.LogSources) == 0 && s.LogFeed == nil}) + writeJSON(w, map[string]any{"entries": entries, "components": components, "services": services, "errors": errors, "truncated": truncated, "disabled": len(a.sources) == 0 && a.feed == nil}) } func readServiceLogs(ctx context.Context, sources map[string]LogSource, selected string, q logging.Query) ([]logging.Entry, map[string]string, bool) { diff --git a/internal/webui/server.go b/internal/webui/server.go index cef7e743b..3d3f6e593 100644 --- a/internal/webui/server.go +++ b/internal/webui/server.go @@ -357,20 +357,17 @@ func (s *Server) registerHarnessRoutes(mux *http.ServeMux) { func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) { newExtensionsAPI(s).register(mux) - mux.HandleFunc("GET /api/control-plane/catalog", s.handleControlPlaneCatalog) mux.HandleFunc("GET /api/control-plane/apply-status", instanceOnly(s.handleApplyStatus)) mux.HandleFunc("GET /api/services", instanceOnly(s.handleServices)) - mux.HandleFunc("GET /api/control-plane/resources/{kind}", s.handleControlPlaneQuery) - mux.HandleFunc("GET /api/control-plane/resources/{kind}/history", s.handleControlPlaneHistory) - mux.HandleFunc("POST /api/control-plane/resources/{kind}/commands/{command}", s.handleControlPlaneCommand) newAccessAPI(s).register(mux) + newControlPlaneAPI(s).register(mux) + newLogsAPI(s).register(mux) newOrgsAPI(s).register(mux) newIdentitiesAPI(s).register(mux) mux.HandleFunc("PUT /api/orgs/current", s.handleOrgSelect) mux.HandleFunc("GET /api/config", instanceOnly(s.handleConfig)) mux.HandleFunc("GET /api/secret-engines", instanceOnly(s.handleSecretEngines)) mux.HandleFunc("POST /api/secret-references/check", instanceOnly(s.handleSecretCheck)) - mux.HandleFunc("GET /api/logs", instanceOnly(s.handleLogs)) } func (s *Server) registerChatRoutes(mux *http.ServeMux) {