ffab866c87
Full M4.2 deliverable: 16 endpoints (tenants CRUD + lifecycle, catalog, entitlements, API keys with argon2 hashing, audit append + filter), Store interface with pgx-backed Postgres + in-memory parallel implementations exercised by the same eachStore harness, openapi.yaml at 3.1 with kin-openapi contract test. M4.3 adds auth. Refs: M4.2
72 lines
2.4 KiB
Go
72 lines
2.4 KiB
Go
// Package server wires the HTTP surface for tenant-registry.
|
|
//
|
|
// All routes are registered in NewRouter; per-concern handlers live in
|
|
// peer files (tenants.go, catalog.go, apikeys.go, audit.go).
|
|
package server
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"gitea.meghsakha.com/platform/tenant-registry/internal/config"
|
|
"gitea.meghsakha.com/platform/tenant-registry/internal/store"
|
|
)
|
|
|
|
// Server bundles the dependencies every handler needs.
|
|
type Server struct {
|
|
Cfg *config.Config
|
|
Log *slog.Logger
|
|
Store store.Store
|
|
}
|
|
|
|
// NewRouter builds the http.Handler with logging middleware applied.
|
|
func NewRouter(s *Server) http.Handler {
|
|
mux := http.NewServeMux()
|
|
|
|
// health + status
|
|
mux.HandleFunc("GET /healthz", s.healthz)
|
|
mux.HandleFunc("GET /readyz", s.readyz)
|
|
|
|
// tenants
|
|
mux.HandleFunc("POST /v1/tenants", s.createTenant)
|
|
mux.HandleFunc("GET /v1/tenants/{id}", s.getTenant)
|
|
mux.HandleFunc("GET /v1/tenants/by-slug/{slug}", s.getTenantBySlug)
|
|
mux.HandleFunc("POST /v1/tenants/{id}/activate", s.activateTenant)
|
|
mux.HandleFunc("POST /v1/tenants/{id}/cancel", s.cancelTenant)
|
|
|
|
// entitlements — top-level path so it doesn't conflict with
|
|
// /v1/tenants/by-slug/{slug} (Go 1.22 ServeMux can't disambiguate
|
|
// /v1/tenants/{id}/products vs /v1/tenants/by-slug/{slug=products}).
|
|
mux.HandleFunc("GET /v1/entitlements", s.listTenantProducts)
|
|
|
|
// catalog
|
|
mux.HandleFunc("GET /v1/catalog", s.getCatalog)
|
|
mux.HandleFunc("POST /v1/catalog/request", s.catalogRequest)
|
|
mux.HandleFunc("POST /v1/catalog/trial-request", s.catalogTrialRequest)
|
|
|
|
// api keys — same disambiguation: list lives at /v1/api-keys?tenant_id=X
|
|
// instead of /v1/tenants/{id}/api-keys.
|
|
mux.HandleFunc("POST /v1/api-keys", s.createAPIKey)
|
|
mux.HandleFunc("GET /v1/api-keys", s.listAPIKeys)
|
|
mux.HandleFunc("DELETE /v1/api-keys/{id}", s.revokeAPIKey)
|
|
mux.HandleFunc("POST /v1/internal/api-keys/verify", s.verifyAPIKey)
|
|
|
|
// audit
|
|
mux.HandleFunc("POST /v1/audit", s.appendAudit)
|
|
mux.HandleFunc("GET /v1/audit", s.listAudit)
|
|
|
|
return logRequest(s.Log)(mux)
|
|
}
|
|
|
|
func (s *Server) healthz(w http.ResponseWriter, _ *http.Request) {
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
|
}
|
|
|
|
func (s *Server) readyz(w http.ResponseWriter, r *http.Request) {
|
|
if err := s.Store.Ping(r.Context()); err != nil {
|
|
writeError(w, http.StatusServiceUnavailable, "store_unavailable", err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "ready"})
|
|
}
|