first commit
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user