-
This commit is contained in:
70
faas/internal/handlers/health.go
Normal file
70
faas/internal/handlers/health.go
Normal file
@ -0,0 +1,70 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type HealthHandler struct {
|
||||
db *sql.DB
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func NewHealthHandler(db *sql.DB, logger *zap.Logger) *HealthHandler {
|
||||
return &HealthHandler{
|
||||
db: db,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Health(c *gin.Context) {
|
||||
h.logger.Debug("Health check requested")
|
||||
|
||||
response := gin.H{
|
||||
"status": "healthy",
|
||||
"service": "faas",
|
||||
"timestamp": time.Now().UTC().Format(time.RFC3339),
|
||||
"version": "1.0.0",
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Ready(c *gin.Context) {
|
||||
h.logger.Debug("Readiness check requested")
|
||||
|
||||
checks := make(map[string]interface{})
|
||||
overall := "ready"
|
||||
|
||||
// Check database connection
|
||||
if err := h.db.Ping(); err != nil {
|
||||
h.logger.Error("Database health check failed", zap.Error(err))
|
||||
checks["database"] = gin.H{
|
||||
"status": "unhealthy",
|
||||
"error": err.Error(),
|
||||
}
|
||||
overall = "not ready"
|
||||
} else {
|
||||
checks["database"] = gin.H{
|
||||
"status": "healthy",
|
||||
}
|
||||
}
|
||||
|
||||
response := gin.H{
|
||||
"status": overall,
|
||||
"service": "faas",
|
||||
"timestamp": time.Now().UTC().Format(time.RFC3339),
|
||||
"checks": checks,
|
||||
}
|
||||
|
||||
statusCode := http.StatusOK
|
||||
if overall != "ready" {
|
||||
statusCode = http.StatusServiceUnavailable
|
||||
}
|
||||
|
||||
c.JSON(statusCode, response)
|
||||
}
|
||||
Reference in New Issue
Block a user