refactor(webui): extract the harness resource module (#1778)
Quality / check (push) Successful in 3m56s
deploy / gate (push) Successful in 4m34s
deploy / archied-image (push) Successful in 2m55s
deploy / agent-image (push) Successful in 12s
deploy / dist-zip (push) Skipped

This commit is contained in:
sam committed 2026-11-03 08:17:12 +11:00
1 parent 6e718e165a
commit 1830a89898
3 files changed
+45 -22

No files matched your search

@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"sort"
@@ -93,36 +94,56 @@ type harnessView struct {
Profiles []string `json:"profiles"`
}
// harnessAPI owns the /api/harness routes. Each dependency is optional and a
// handler answers 503 when the one it needs is absent.
type harnessAPI struct {
controlPlane controlpb.ControlPlaneServiceClient
secrets storecontract.HarnessSecretStore
terminal HarnessTerminal
log *slog.Logger
}
func newHarnessAPI(s *Server) *harnessAPI {
return &harnessAPI{controlPlane: s.ControlPlane, secrets: s.HarnessSecrets, terminal: s.HarnessTerminal, log: s.Log}
}
func (a *harnessAPI) logf(msg string, args ...any) { warnf(a.log, msg, args...) }
func (a *harnessAPI) register(mux *http.ServeMux) {
mux.HandleFunc("GET /api/harness/bindings", a.handleHarnessBindings)
mux.HandleFunc("GET /api/harness/terminal", a.handleHarnessTerminal)
}
// handleHarnessBindings lists the org's credential bindings joined with
// their captured OAuth state. It requires both contracts the join reads: an
// unconfigured process answers 503 rather than rendering a page that claims
// the org has no bindings, which it cannot know.
func (s *Server) handleHarnessBindings(w http.ResponseWriter, r *http.Request) {
if s.ControlPlane == nil || s.HarnessSecrets == nil {
func (a *harnessAPI) handleHarnessBindings(w http.ResponseWriter, r *http.Request) {
if a.controlPlane == nil || a.secrets == nil {
http.Error(w, "harness bindings not configured", http.StatusServiceUnavailable)
return
}
orgID := string(org.OrgFromContext(r.Context()))
bindings, err := s.harnessBindings(r.Context(), orgID)
bindings, err := a.harnessBindings(r.Context(), orgID)
if err != nil {
s.logf("list harness bindings", "err", err)
a.logf("list harness bindings", "err", err)
http.Error(w, "list harness bindings failed", http.StatusBadGateway)
return
}
profiles, err := s.harnessProfiles(r.Context())
profiles, err := a.harnessProfiles(r.Context())
if err != nil {
s.logf("list harness profiles", "err", err)
a.logf("list harness profiles", "err", err)
http.Error(w, "list harness bindings failed", http.StatusBadGateway)
return
}
writeJSON(w, harnessView{Terminal: s.HarnessTerminal != nil, Bindings: bindings, Profiles: profiles})
writeJSON(w, harnessView{Terminal: a.terminal != nil, Bindings: bindings, Profiles: profiles})
}
// harnessBindings reads the credential-bindings document and each service's
// captured token state. A service with no captured set is not an error: it
// is exactly the state the setup terminal exists to change.
func (s *Server) harnessBindings(ctx context.Context, orgID string) ([]harnessBindingView, error) {
response, err := s.ControlPlane.Query(ctx, &controlpb.QueryRequest{Kind: harnessBindingsKind})
func (a *harnessAPI) harnessBindings(ctx context.Context, orgID string) ([]harnessBindingView, error) {
response, err := a.controlPlane.Query(ctx, &controlpb.QueryRequest{Kind: harnessBindingsKind})
if err != nil {
return nil, err
}
@@ -138,7 +159,7 @@ func (s *Server) harnessBindings(ctx context.Context, orgID string) ([]harnessBi
continue
}
view := harnessBindingView{Service: doc.Service}
secret, err := s.HarnessSecrets.GetHarnessSecret(ctx, orgID, doc.Service)
secret, err := a.secrets.GetHarnessSecret(ctx, orgID, doc.Service)
switch {
case err == nil:
view.Captured = true
@@ -163,8 +184,8 @@ func (s *Server) harnessBindings(ctx context.Context, orgID string) ([]harnessBi
// harnessProfiles returns the agent profiles that name a Kit, sorted so the
// page's list does not depend on Go's map iteration order.
func (s *Server) harnessProfiles(ctx context.Context) ([]string, error) {
response, err := s.ControlPlane.Query(ctx, &controlpb.QueryRequest{Kind: harnessProfilesKind})
func (a *harnessAPI) harnessProfiles(ctx context.Context) ([]string, error) {
response, err := a.controlPlane.Query(ctx, &controlpb.QueryRequest{Kind: harnessProfilesKind})
if err != nil {
return nil, err
}
@@ -188,8 +209,8 @@ func (s *Server) harnessProfiles(ctx context.Context) ([]string, error) {
// a setup session opened for the named Kit profile. Opening the terminal is
// the update action on the secret, decided by the authorizer before this
// handler runs (authorize.go's route override).
func (s *Server) handleHarnessTerminal(w http.ResponseWriter, r *http.Request) {
if s.HarnessTerminal == nil {
func (a *harnessAPI) handleHarnessTerminal(w http.ResponseWriter, r *http.Request) {
if a.terminal == nil {
http.Error(w, "setup terminal not configured", http.StatusServiceUnavailable)
return
}
@@ -204,9 +225,9 @@ func (s *Server) handleHarnessTerminal(w http.ResponseWriter, r *http.Request) {
}
rows := terminalSize(r.URL.Query(), "rows", terminalDefaultRows)
cols := terminalSize(r.URL.Query(), "cols", terminalDefaultCols)
session, err := s.HarnessTerminal.Open(r.Context(), string(org.OrgFromContext(r.Context())), profile, rows, cols)
session, err := a.terminal.Open(r.Context(), string(org.OrgFromContext(r.Context())), profile, rows, cols)
if err != nil {
s.logf("open setup terminal", "err", err, "profile", profile)
a.logf("open setup terminal", "err", err, "profile", profile)
http.Error(w, "setup terminal failed", http.StatusBadGateway)
return
}
+1 -2
View File
@@ -352,8 +352,7 @@ func (s *Server) registerMappingAndBindingRoutes(mux *http.ServeMux) {
}
func (s *Server) registerHarnessRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/harness/bindings", s.handleHarnessBindings)
mux.HandleFunc("GET /api/harness/terminal", s.handleHarnessTerminal)
newHarnessAPI(s).register(mux)
}
func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) {
+7 -4
View File
@@ -38,11 +38,14 @@ func newTasksAPI(s *Server) *tasksAPI {
}
}
func (a *tasksAPI) logf(msg string, args ...any) {
if a.log == nil {
return
func (a *tasksAPI) logf(msg string, args ...any) { warnf(a.log, msg, args...) }
// warnf logs at warn level and tolerates a nil logger, as tests construct
// servers without one.
func warnf(log *slog.Logger, msg string, args ...any) {
if log != nil {
log.Warn(msg, args...)
}
a.log.Warn(msg, args...)
}
func (a *tasksAPI) register(mux *http.ServeMux) {