Runs (migration 0011): a run groups the cycles of one launch. It opens when an address enters an idle queue, takes everything submitted or re-checked while it is open and is finalized when all its addresses are done; a re-check after that opens a new run, so results of different runs never mix. check_runs, run_results (one result per address and run, with the verdict and the expected and stored check counts), subnets, run_id on ip_queue and checks. Existing data is split into runs at pauses of more than an hour; ingress checks get the validator that held the address (also at write time from now on). Analytics (internal/analytics): figures computed from the stored checks of the latest cycle of each address in the run, as facts next to the verdict: summary, reasons of partial, data quality, subnets, targets and the subnet x target matrix by check type, ingress by site, error classes, validators, and the address lists behind the indicators and error classes. API: analytics runs, report, lists (JSON or CSV), subnet list; run and subnet filters for the registry. Dashboard: /analytics matching the approved mockup (run selector, indicators with address lists and CSV, error-class dialogs, drill-down to the registry), subnet list on /settings. Sidebar: the control-api link state, theme toggle and logout moved to the top, the three dots next to the logo removed, sections grouped. Rebuilt bin/control-api and bin/admin-dashboard to match. Plan, summary and the updated README, API, USAGE, DASHBOARD and ADMIN_CLEANUP docs are in docs/. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
// Package httpapi exposes the Control API's HTTP surface — the only way
|
|
// validator-agents, probers, and operators interact with the system. All
|
|
// business logic lives in internal/orchestrator; handlers here do request
|
|
// parsing/validation, call into the orchestrator or db package, and shape
|
|
// the JSON response.
|
|
package httpapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"cloudipvalidator/internal/db"
|
|
"cloudipvalidator/internal/orchestrator"
|
|
)
|
|
|
|
type Server struct {
|
|
DB *db.DB
|
|
Orch *orchestrator.Orchestrator
|
|
Log *slog.Logger
|
|
Auth Authenticator
|
|
|
|
analytics analyticsCache
|
|
}
|
|
|
|
func New(d *db.DB, o *orchestrator.Orchestrator, log *slog.Logger) *Server {
|
|
return &Server{DB: d, Orch: o, Log: log}
|
|
}
|
|
|
|
func (s *Server) Handler() http.Handler {
|
|
s.Auth.Log = s.Log
|
|
mux := http.NewServeMux()
|
|
s.routes(mux)
|
|
return loggingMiddleware(s.Log, mux)
|
|
}
|
|
|
|
func loggingMiddleware(log *slog.Logger, next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
next.ServeHTTP(w, r)
|
|
log.Debug("request", "method", r.Method, "path", r.URL.Path, "remote", r.RemoteAddr)
|
|
})
|
|
}
|
|
|
|
func writeJSON(w http.ResponseWriter, status int, v interface{}) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
if v != nil {
|
|
_ = json.NewEncoder(w).Encode(v)
|
|
}
|
|
}
|
|
|
|
func writeError(w http.ResponseWriter, status int, msg string) {
|
|
writeJSON(w, status, errorResponse{Error: msg})
|
|
}
|
|
|
|
func readJSON(r *http.Request, v interface{}) error {
|
|
if r.Body == nil || r.ContentLength == 0 {
|
|
return nil
|
|
}
|
|
dec := json.NewDecoder(r.Body)
|
|
return dec.Decode(v)
|
|
}
|
|
|
|
// writeDBError maps the typed sentinel errors returned by internal/db's
|
|
// admin mutation methods to the appropriate HTTP status, instead of
|
|
// defaulting everything to 500 like the older read-only admin handlers do.
|
|
func writeDBError(w http.ResponseWriter, err error) {
|
|
switch {
|
|
case errors.Is(err, db.ErrNotFound):
|
|
writeError(w, http.StatusNotFound, err.Error())
|
|
case errors.Is(err, db.ErrConflict), errors.Is(err, db.ErrBusy), errors.Is(err, db.ErrInUse), errors.Is(err, db.ErrInvalidState):
|
|
writeError(w, http.StatusConflict, err.Error())
|
|
case errors.Is(err, db.ErrValidation):
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
default:
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
}
|
|
}
|