package httpapi import ( "context" "net/http" "time" ) // handleHealth reports process liveness and database reachability. // // Returns 503 when the database cannot be pinged, so a container orchestrator // restarts a wedged process instead of routing traffic to it. func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second) defer cancel() dbStatus := "ok" code := http.StatusOK if err := s.db.PingContext(ctx); err != nil { dbStatus = "unreachable" code = http.StatusServiceUnavailable s.log.Error("health check: database unreachable", "err", err) } status := "ok" if code != http.StatusOK { status = "degraded" } writeJSON(w, code, map[string]any{ "status": status, "version": s.version, "uptime_seconds": int(time.Since(s.started).Seconds()), "database": dbStatus, }) } // handleVersion returns build information. func (s *Server) handleVersion(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, map[string]any{ "name": "vellum", "version": s.version, "api": "v1", }) }