Initial phase 1 baseline implementation
Some checks failed
CI / Backend (push) Failing after 4s
CI / Frontend (push) Successful in 11s
CI / Contracts and repository policy (push) Failing after 5s
CI / Container (push) Has been skipped

This commit is contained in:
Elijah 2026-07-16 19:14:01 -07:00
parent 099c53badf
commit 077cf7601a
29 changed files with 1339 additions and 39 deletions

View file

@ -7,6 +7,7 @@ import (
"net/http"
"time"
"drive.local/drivev2/internal/application"
"drive.local/drivev2/internal/config"
)
@ -16,15 +17,16 @@ type Server struct {
httpServer *http.Server
}
func NewServer(cfg config.Config, version string, logger *slog.Logger, frontend http.Handler) *Server {
type SystemService interface {
Readiness(context.Context) error
SetupStatus(context.Context) (application.SetupStatus, error)
}
func NewServer(cfg config.Config, logger *slog.Logger, system SystemService, frontend http.Handler) *Server {
mux := http.NewServeMux()
mux.HandleFunc("GET /health/live", jsonResponse(http.StatusOK, map[string]string{"status": "live"}))
mux.HandleFunc("GET /health/ready", jsonResponse(http.StatusOK, map[string]string{"status": "ready"}))
mux.HandleFunc("GET /api/v1/setup/status", jsonResponse(http.StatusOK, map[string]any{
"initialized": false,
"phase": "foundation",
"version": version,
}))
mux.HandleFunc("GET /health/ready", readinessHandler(logger, system))
mux.HandleFunc("GET /api/v1/setup/status", setupStatusHandler(logger, system))
mux.Handle("/", frontend)
return &Server{httpServer: &http.Server{
@ -35,6 +37,29 @@ func NewServer(cfg config.Config, version string, logger *slog.Logger, frontend
}}
}
func readinessHandler(logger *slog.Logger, system SystemService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := system.Readiness(r.Context()); err != nil {
logger.Error("readiness check failed", "error", err)
problemResponse(w, http.StatusServiceUnavailable, "dependency_unavailable", "A required dependency is unavailable")
return
}
writeJSON(w, http.StatusOK, map[string]string{"status": "ready"})
}
}
func setupStatusHandler(logger *slog.Logger, system SystemService) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
status, err := system.SetupStatus(r.Context())
if err != nil {
logger.Error("setup status failed", "error", err)
problemResponse(w, http.StatusServiceUnavailable, "dependency_unavailable", "A required dependency is unavailable")
return
}
writeJSON(w, http.StatusOK, status)
}
}
func (s *Server) ListenAndServe() error {
return s.httpServer.ListenAndServe()
}
@ -44,13 +69,30 @@ func (s *Server) Shutdown(ctx context.Context) error {
}
func jsonResponse(status int, payload any) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(payload); err != nil {
slog.Error("encode response", "error", err)
}
return func(w http.ResponseWriter, _ *http.Request) { writeJSON(w, status, payload) }
}
func writeJSON(w http.ResponseWriter, status int, payload any) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(payload); err != nil {
slog.Error("encode response", "error", err)
}
}
func problemResponse(w http.ResponseWriter, status int, code, detail string) {
w.Header().Set("Content-Type", "application/problem+json")
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(map[string]any{
"type": "about:blank",
"title": http.StatusText(status),
"status": status,
"detail": detail,
"code": code,
}); err != nil {
slog.Error("encode problem response", "error", err)
}
}

View file

@ -1,6 +1,8 @@
package httpapi
import (
"context"
"errors"
"io"
"log/slog"
"net/http"
@ -8,16 +10,31 @@ import (
"strings"
"testing"
"drive.local/drivev2/internal/application"
"drive.local/drivev2/internal/config"
)
type systemServiceStub struct {
readinessErr error
setupStatus application.SetupStatus
setupErr error
}
func (s systemServiceStub) Readiness(context.Context) error {
return s.readinessErr
}
func (s systemServiceStub) SetupStatus(context.Context) (application.SetupStatus, error) {
return s.setupStatus, s.setupErr
}
func TestLiveHealth(t *testing.T) {
t.Parallel()
server := NewServer(
config.Config{HTTPAddress: ":0", WebRoot: t.TempDir()},
"test",
slog.New(slog.NewTextHandler(io.Discard, nil)),
systemServiceStub{},
http.NotFoundHandler(),
)
request := httptest.NewRequest(http.MethodGet, "/health/live", nil)
@ -32,3 +49,50 @@ func TestLiveHealth(t *testing.T) {
t.Fatalf("unexpected body: %s", recorder.Body.String())
}
}
func TestReadinessReportsDependencyFailure(t *testing.T) {
t.Parallel()
server := NewServer(
config.Config{HTTPAddress: ":0", WebRoot: t.TempDir()},
slog.New(slog.NewTextHandler(io.Discard, nil)),
systemServiceStub{readinessErr: errors.New("database unavailable")},
http.NotFoundHandler(),
)
request := httptest.NewRequest(http.MethodGet, "/health/ready", nil)
recorder := httptest.NewRecorder()
server.httpServer.Handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusServiceUnavailable)
}
if contentType := recorder.Header().Get("Content-Type"); contentType != "application/problem+json" {
t.Fatalf("Content-Type = %q, want application/problem+json", contentType)
}
if !strings.Contains(recorder.Body.String(), `"code":"dependency_unavailable"`) {
t.Fatalf("unexpected body: %s", recorder.Body.String())
}
}
func TestSetupStatusUsesApplicationService(t *testing.T) {
t.Parallel()
server := NewServer(
config.Config{HTTPAddress: ":0", WebRoot: t.TempDir()},
slog.New(slog.NewTextHandler(io.Discard, nil)),
systemServiceStub{setupStatus: application.SetupStatus{Initialized: true, Phase: "phase-1", Version: "test"}},
http.NotFoundHandler(),
)
request := httptest.NewRequest(http.MethodGet, "/api/v1/setup/status", nil)
recorder := httptest.NewRecorder()
server.httpServer.Handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusOK)
}
if !strings.Contains(recorder.Body.String(), `"initialized":true`) {
t.Fatalf("unexpected body: %s", recorder.Body.String())
}
}