package handler import ( "context" "net/http" "time" "github.com/jackc/pgx/v5/pgxpool" ) type HealthHandler struct { pool *pgxpool.Pool } func NewHealthHandler(pool *pgxpool.Pool) *HealthHandler { return &HealthHandler{pool: pool} } // Healthz — liveness. Не дёргает БД; жив если процесс отвечает. func (h *HealthHandler) Healthz(w http.ResponseWriter, _ *http.Request) { writeJSON(w, http.StatusOK, map[string]string{"status": "ok"}) } // Readyz — readiness. Один Ping к БД с таймаутом — если не отвечает, // k8s выкидывает pod из service-балансира. 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 { writeError(w, http.StatusServiceUnavailable, "db not ready") return } writeJSON(w, http.StatusOK, map[string]string{"status": "ready"}) }