Files
mtusi2026lab2/internal/health/handler.go
T

54 lines
1.0 KiB
Go

// Package health serves the probes. Liveness only proves the process
// answers; readiness is switched off during shutdown so an orchestrator stops
// routing new traffic before the listener closes.
package health
import (
"net/http"
"sync/atomic"
"github.com/gin-gonic/gin"
"messenger/internal/platform/apperr"
"messenger/internal/platform/httpx"
)
type Handler struct {
ready atomic.Bool
}
type status struct {
Status string `json:"status"`
}
func New() *Handler {
h := &Handler{}
h.ready.Store(true)
return h
}
func (h *Handler) SetReady(ready bool) {
h.ready.Store(ready)
}
func (h *Handler) IsReady() bool {
return h.ready.Load()
}
func (h *Handler) Register(r gin.IRouter) {
r.GET("/health/live", h.live)
r.GET("/health/ready", h.readyz)
}
func (h *Handler) live(c *gin.Context) {
httpx.JSON(c, http.StatusOK, status{Status: "ok"})
}
func (h *Handler) readyz(c *gin.Context) {
if !h.ready.Load() {
httpx.Error(c, apperr.Unavailable("service is shutting down"))
return
}
httpx.JSON(c, http.StatusOK, status{Status: "ok"})
}