first commit
This commit is contained in:
90
transports/bifrost-http/handlers/health.go
Normal file
90
transports/bifrost-http/handlers/health.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/fasthttp/router"
|
||||
"github.com/maximhq/bifrost/core/schemas"
|
||||
"github.com/maximhq/bifrost/transports/bifrost-http/lib"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// HealthHandler manages HTTP requests for health checks.
|
||||
type HealthHandler struct {
|
||||
config *lib.Config
|
||||
}
|
||||
|
||||
// NewHealthHandler creates a new health handler instance.
|
||||
func NewHealthHandler(config *lib.Config) *HealthHandler {
|
||||
return &HealthHandler{
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterRoutes registers the health-related routes.
|
||||
func (h *HealthHandler) RegisterRoutes(r *router.Router, middlewares ...schemas.BifrostHTTPMiddleware) {
|
||||
r.GET("/health", lib.ChainMiddlewares(h.getHealth, middlewares...))
|
||||
}
|
||||
|
||||
// getHealth handles GET /api/health - Get the health status of the server.
|
||||
func (h *HealthHandler) getHealth(ctx *fasthttp.RequestCtx) {
|
||||
// If DB pings are disabled, just return OK
|
||||
if h.config.ClientConfig.DisableDBPingsInHealth {
|
||||
SendJSON(ctx, map[string]any{"status": "ok", "components": map[string]any{"db_pings": "disabled"}})
|
||||
return
|
||||
}
|
||||
// Pinging config store
|
||||
reqCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
var errors []string
|
||||
var mu sync.Mutex
|
||||
var wg sync.WaitGroup
|
||||
|
||||
if h.config.ConfigStore != nil {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := h.config.ConfigStore.Ping(reqCtx); err != nil {
|
||||
mu.Lock()
|
||||
errors = append(errors, "config store not available")
|
||||
mu.Unlock()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Pinging log store
|
||||
if h.config.LogsStore != nil {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := h.config.LogsStore.Ping(reqCtx); err != nil {
|
||||
mu.Lock()
|
||||
errors = append(errors, "log store not available")
|
||||
mu.Unlock()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Pinging vector store
|
||||
if h.config.VectorStore != nil {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := h.config.VectorStore.Ping(reqCtx); err != nil {
|
||||
mu.Lock()
|
||||
errors = append(errors, "vector store not available")
|
||||
mu.Unlock()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if len(errors) > 0 {
|
||||
SendError(ctx, fasthttp.StatusServiceUnavailable, errors[0])
|
||||
return
|
||||
}
|
||||
SendJSON(ctx, map[string]any{"status": "ok", "components": map[string]any{"db_pings": "ok"}})
|
||||
}
|
||||
Reference in New Issue
Block a user