52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jmoiron/sqlx"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// HealthHandler handles health check requests
|
|
type HealthHandler struct {
|
|
db *sqlx.DB
|
|
logger *zap.Logger
|
|
}
|
|
|
|
// NewHealthHandler creates a new health handler
|
|
func NewHealthHandler(db *sqlx.DB, logger *zap.Logger) *HealthHandler {
|
|
return &HealthHandler{
|
|
db: db,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
// Health handles GET /health
|
|
func (h *HealthHandler) Health(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "ok",
|
|
"service": "user-service",
|
|
"version": "1.0.0",
|
|
})
|
|
}
|
|
|
|
// Ready handles GET /ready
|
|
func (h *HealthHandler) Ready(c *gin.Context) {
|
|
// Check database connection
|
|
if err := h.db.Ping(); err != nil {
|
|
h.logger.Error("Database health check failed", zap.Error(err))
|
|
c.JSON(http.StatusServiceUnavailable, gin.H{
|
|
"status": "not ready",
|
|
"reason": "database connection failed",
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "ready",
|
|
"service": "user-service",
|
|
"database": "connected",
|
|
})
|
|
} |