fix(health): readiness pings postgres and redis instead of returning 200

This commit is contained in:
Голиков Maximillian Антон
2026-10-02 15:10:03 +03:00
parent eaf04c4769
commit 46181bb404
2 changed files with 38 additions and 9 deletions
+22 -3
View File
@@ -1,17 +1,24 @@
package handler
import (
"context"
"net/http"
"time"
"github.com/gin-gonic/gin"
"high-loud-apps-labs/internal/domain"
"high-loud-apps-labs/internal/httpapi/dto"
)
type HealthHandler struct{}
type HealthCheck func(ctx context.Context) error
func NewHealthHandler() *HealthHandler {
return &HealthHandler{}
type HealthHandler struct {
checks []HealthCheck
}
func NewHealthHandler(checks ...HealthCheck) *HealthHandler {
return &HealthHandler{checks: checks}
}
func (h *HealthHandler) Live(c *gin.Context) {
@@ -19,5 +26,17 @@ func (h *HealthHandler) Live(c *gin.Context) {
}
func (h *HealthHandler) Ready(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
defer cancel()
for _, check := range h.checks {
if check == nil {
continue
}
if err := check(ctx); err != nil {
respondError(c, &domain.UnavailableError{Reason: "зависимость недоступна"})
return
}
}
c.JSON(http.StatusOK, dto.HealthResponse{Status: "ok"})
}