Files
mtusi2026lab4/app/internal/health/handler.go
T
2026-11-06 19:06:21 +03:00

62 lines
1.7 KiB
Go

// Package health exposes liveness and readiness endpoints plus the process
// shutdown flag used by readiness.
package health
import (
"sync/atomic"
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/common"
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/objectstore"
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/session"
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/store"
"github.com/gofiber/fiber/v3"
)
type Handler struct {
store *store.Store
sessions *session.Store
objects *objectstore.Client
}
func NewHandler(st *store.Store, sessions *session.Store, objects *objectstore.Client) *Handler {
return &Handler{store: st, sessions: sessions, objects: objects}
}
var shuttingDown atomic.Bool
func SignalShutdown() {
shuttingDown.Store(true)
}
type Status struct {
Status string `json:"status"`
}
func (h *Handler) Live(c fiber.Ctx) error {
return c.JSON(Status{Status: "ok"})
}
func (h *Handler) Ready(c fiber.Ctx) error {
if shuttingDown.Load() {
return common.SendError(c, fiber.StatusServiceUnavailable,
common.ErrorCodeUnavailable, "service is shutting down")
}
if err := h.store.Ping(c.Context()); err != nil {
return common.SendError(c, fiber.StatusServiceUnavailable,
common.ErrorCodeUnavailable, "database unavailable")
}
if err := h.sessions.Ping(c.Context()); err != nil {
return common.SendError(c, fiber.StatusServiceUnavailable,
common.ErrorCodeUnavailable, "session store unavailable")
}
if err := h.objects.Ping(c.Context()); err != nil {
return common.SendError(c, fiber.StatusServiceUnavailable,
common.ErrorCodeUnavailable, "object storage unavailable")
}
return c.JSON(Status{Status: "ok"})
}