mitbringsl/backend/internal/httpapi/health.go
Tronax 2899eb205b
Backend Phase A: foundation, migrations, Docker setup
- Go backend skeleton: config (caarlos0/env), slog JSON logging,
  pgxpool store, HTTP server with graceful shutdown.
- httpapi: render helpers, Problem errors, middleware chain
  (requestID / logging / recover / CORS), /healthz and /readyz.
- Migrations: full initial schema (users, sessions, lists,
  list_members, items, op_log SOURCE OF TRUTH, item_names) +
  golang-migrate runner binary using source/iofs (embedded).
- Docker: multi-stage Dockerfile (Go 1.26 -> distroless nonroot),
  builds both server and migrate binaries.
- deploy: docker-compose (caddy + backend + migrate + postgres:16),
  Caddyfile (auto-HTTPS), .env.example, pg extensions init script.
- AGENTS.md: project context + roadmap for AI agents.

Verified: image builds, both binaries run in container (smoke test).
2026-08-05 15:14:37 +02:00

28 lines
731 B
Go

package httpapi
import (
"context"
"net/http"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
// HealthHandler exposes /healthz (liveness) and /readyz (readiness + DB ping).
type HealthHandler struct {
Pool *pgxpool.Pool
}
func (h *HealthHandler) Healthz(w http.ResponseWriter, r *http.Request) {
renderJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}
func (h *HealthHandler) Readyz(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
if err := h.Pool.Ping(ctx); err != nil {
renderError(w, http.StatusServiceUnavailable, "Database unavailable", err.Error())
return
}
renderJSON(w, http.StatusOK, map[string]string{"status": "ready"})
}