refactor: use new health package

This commit is contained in:
2026-02-15 11:56:19 +01:00
parent a3ed6685de
commit 70bfb54acf

View File

@@ -6,6 +6,7 @@ import (
"time" "time"
"goyco/internal/config" "goyco/internal/config"
"goyco/internal/health"
"goyco/internal/middleware" "goyco/internal/middleware"
"goyco/internal/repositories" "goyco/internal/repositories"
"goyco/internal/services" "goyco/internal/services"
@@ -21,7 +22,7 @@ type APIHandler struct {
userRepo repositories.UserRepository userRepo repositories.UserRepository
voteService *services.VoteService voteService *services.VoteService
dbMonitor middleware.DBMonitor dbMonitor middleware.DBMonitor
healthChecker *middleware.DatabaseHealthChecker healthChecker *health.CompositeChecker
metricsCollector *middleware.MetricsCollector metricsCollector *middleware.MetricsCollector
} }
@@ -44,7 +45,21 @@ func NewAPIHandlerWithMonitoring(config *config.Config, postRepo repositories.Po
return NewAPIHandler(config, postRepo, userRepo, voteService) return NewAPIHandler(config, postRepo, userRepo, voteService)
} }
healthChecker := middleware.NewDatabaseHealthChecker(sqlDB, dbMonitor) compositeChecker := health.NewCompositeChecker()
dbChecker := health.NewDatabaseChecker(sqlDB, dbMonitor)
compositeChecker.AddChecker(dbChecker)
smtpConfig := health.SMTPConfig{
Host: config.SMTP.Host,
Port: config.SMTP.Port,
Username: config.SMTP.Username,
Password: config.SMTP.Password,
From: config.SMTP.From,
}
smtpChecker := health.NewSMTPChecker(smtpConfig)
compositeChecker.AddChecker(smtpChecker)
metricsCollector := middleware.NewMetricsCollector(dbMonitor) metricsCollector := middleware.NewMetricsCollector(dbMonitor)
return &APIHandler{ return &APIHandler{
@@ -53,7 +68,7 @@ func NewAPIHandlerWithMonitoring(config *config.Config, postRepo repositories.Po
userRepo: userRepo, userRepo: userRepo,
voteService: voteService, voteService: voteService,
dbMonitor: dbMonitor, dbMonitor: dbMonitor,
healthChecker: healthChecker, healthChecker: compositeChecker,
metricsCollector: metricsCollector, metricsCollector: metricsCollector,
} }
} }
@@ -135,17 +150,17 @@ func (h *APIHandler) GetAPIInfo(w http.ResponseWriter, r *http.Request) {
} }
// @Summary Health check // @Summary Health check
// @Description Check the API health status along with database connectivity details // @Description Check the API health status along with database connectivity and SMTP service details
// @Tags api // @Tags api
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Success 200 {object} CommonResponse "Health check successful" // @Success 200 {object} CommonResponse "Health check successful"
// @Router /health [get] // @Router /health [get]
func (h *APIHandler) GetHealth(w http.ResponseWriter, r *http.Request) { func (h *APIHandler) GetHealth(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if h.healthChecker != nil { if h.healthChecker != nil {
health := h.healthChecker.CheckHealth() health := h.healthChecker.CheckWithVersion(ctx, version.GetVersion())
health["version"] = version.GetVersion()
SendSuccessResponse(w, "Health check successful", health) SendSuccessResponse(w, "Health check successful", health)
return return
} }