Перенес необходимые изменения из лабораторной работы №2

This commit is contained in:
Grbisba
2026-11-06 14:27:27 +03:00
parent 0ab3a341aa
commit 4c066ff191
92 changed files with 7156 additions and 7 deletions
@@ -0,0 +1,51 @@
package http
import (
"context"
"net/http"
"time"
"gitea.privcloud.tech/mtusi2026/internal/gen/contracts"
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/framework"
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/router"
)
type databasePinger interface{ Ping(context.Context) error }
type HealthHandler struct {
router router.Router
server Server
database databasePinger
}
func NewHealthHandler(r router.Router, server Server, database databasePinger) Handler {
return &HealthHandler{
router: r,
server: server,
database: database,
}
}
func (h *HealthHandler) RegisterRoutes() {
h.router.Public(http.MethodGet, "/health/ready", h.Ready)
h.router.Public(http.MethodGet, "/health/live", h.Live)
}
func (h *HealthHandler) Live(w http.ResponseWriter, _ *http.Request) {
framework.JSONResponse(w, http.StatusOK, contracts.Health{Status: contracts.Ok})
}
func (h *HealthHandler) Ready(w http.ResponseWriter, r *http.Request) {
if !h.server.Ready() {
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeUnavailable, "server is not ready"))
return
}
ctx, cancel := context.WithTimeout(r.Context(), 200*time.Millisecond)
defer cancel()
if err := h.database.Ping(ctx); err != nil {
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeUnavailable, "database unavailable"))
return
}
framework.JSONResponse(w, http.StatusOK, contracts.Health{Status: contracts.Ok})
}