forked from mtusi2026/mtusi2026lab2
feat(app) lab2 prepared for mr
This commit is contained in:
@@ -3,25 +3,53 @@ package health
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab1/internal/common"
|
||||
"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)
|
||||
}
|
||||
|
||||
func live(c fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"status": "ok"})
|
||||
type Status struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func ready(c fiber.Ctx) error {
|
||||
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.StatusInternalServerError,
|
||||
common.ErrorCodeInternal, "app currently shutting down")
|
||||
return common.SendError(c, fiber.StatusServiceUnavailable,
|
||||
common.ErrorCodeUnavailable, "service is shutting down")
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{"status": "ok"})
|
||||
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"})
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
package health
|
||||
|
||||
import "github.com/gofiber/fiber/v3"
|
||||
import (
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/session"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/store"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func RegisterRoutes(router fiber.Router) {
|
||||
router.Get("/live", live)
|
||||
router.Get("/ready", ready)
|
||||
func RegisterRoutes(router fiber.Router, st *store.Store, sessions *session.Store) {
|
||||
handler := NewHandler(st, sessions)
|
||||
router.Get("/live", handler.Live)
|
||||
router.Get("/ready", handler.Ready)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user