forked from mtusi2026/mtusi2026lab2
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package health
|
|
|
|
import (
|
|
"sync/atomic"
|
|
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store"
|
|
"github.com/gofiber/fiber/v3"
|
|
)
|
|
|
|
type Handler struct {
|
|
store *store.Store
|
|
sessions *session.Store
|
|
}
|
|
|
|
func NewHandler(st *store.Store, sessions *session.Store) *Handler {
|
|
return &Handler{store: st, sessions: sessions}
|
|
}
|
|
|
|
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"})
|
|
}
|
|
|
|
// Ready answers whether the synchronous mandatory path can be served. Both
|
|
// PostgreSQL and Redis are critical here: business data and sessions live in
|
|
// them respectively.
|
|
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")
|
|
}
|
|
|
|
return c.JSON(Status{Status: "ok"})
|
|
}
|