47 lines
973 B
Go
47 lines
973 B
Go
// Path: Backend/internal/handlers/health/health.go
|
|
|
|
package health
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"boostai-backend/internal/database"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
type Handler struct {
|
|
environment string
|
|
db *database.DB
|
|
}
|
|
|
|
func NewHandler(environment string, db *database.DB) *Handler {
|
|
return &Handler{environment: environment, db: db}
|
|
}
|
|
|
|
func (h *Handler) Check(c *fiber.Ctx) error {
|
|
status := "healthy"
|
|
databaseStatus := "up"
|
|
httpStatus := fiber.StatusOK
|
|
|
|
if h.db != nil {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
|
|
if err := h.db.Health(ctx); err != nil {
|
|
status = "unhealthy"
|
|
databaseStatus = "down"
|
|
httpStatus = fiber.StatusServiceUnavailable
|
|
}
|
|
}
|
|
|
|
return c.Status(httpStatus).JSON(fiber.Map{
|
|
"status": status,
|
|
"service": "boostai-backend",
|
|
"environment": h.environment,
|
|
"database": databaseStatus,
|
|
"timestamp": time.Now().UTC().Format(time.RFC3339),
|
|
})
|
|
}
|