50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v3"
|
|
"gorm.io/gorm"
|
|
|
|
"optoant/config"
|
|
)
|
|
|
|
// HealthResponse is the structure returned by the /health endpoint.
|
|
type HealthResponse struct {
|
|
Status string `json:"status"`
|
|
Database string `json:"database"`
|
|
Config struct {
|
|
OpenAIBackend string `json:"openai_backend"`
|
|
Port string `json:"port"`
|
|
} `json:"config"`
|
|
}
|
|
|
|
// HealthHandler returns service status and basic config info.
|
|
// @Summary Health check
|
|
// @Description Returns service health status, database connectivity, and active configuration.
|
|
// @Tags health
|
|
// @Produce json
|
|
// @Success 200 {object} HealthResponse
|
|
// @Router /health [get]
|
|
func HealthHandler(cfg *config.Config, db *gorm.DB) fiber.Handler {
|
|
return func(c fiber.Ctx) error {
|
|
resp := HealthResponse{
|
|
Status: "ok",
|
|
}
|
|
resp.Config.OpenAIBackend = cfg.OpenAIBackend
|
|
resp.Config.Port = cfg.Port
|
|
|
|
// Check DB connectivity
|
|
if db != nil {
|
|
sqlDB, err := db.DB()
|
|
if err != nil || sqlDB.Ping() != nil {
|
|
resp.Database = "unreachable"
|
|
} else {
|
|
resp.Database = "ok"
|
|
}
|
|
} else {
|
|
resp.Database = "disabled"
|
|
}
|
|
|
|
return c.JSON(resp)
|
|
}
|
|
}
|