|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | +) |
| 7 | + |
| 8 | +// HealthResponse represents the JSON structure for the /health endpoint response |
| 9 | +// as defined in MCP Gateway Specification section 8.1.1 |
| 10 | +type HealthResponse struct { |
| 11 | + Status string `json:"status"` // "healthy" or "unhealthy" |
| 12 | + SpecVersion string `json:"specVersion"` // MCP Gateway Specification version |
| 13 | + GatewayVersion string `json:"gatewayVersion"` // Gateway implementation version |
| 14 | + Servers map[string]ServerStatus `json:"servers"` // Map of server names to their health status |
| 15 | +} |
| 16 | + |
| 17 | +// BuildHealthResponse constructs a HealthResponse from the unified server's status |
| 18 | +func BuildHealthResponse(unifiedServer *UnifiedServer) HealthResponse { |
| 19 | + // Get server status |
| 20 | + serverStatus := unifiedServer.GetServerStatus() |
| 21 | + |
| 22 | + // Determine overall health based on server status |
| 23 | + overallStatus := "healthy" |
| 24 | + for _, status := range serverStatus { |
| 25 | + if status.Status == "error" { |
| 26 | + overallStatus = "unhealthy" |
| 27 | + break |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return HealthResponse{ |
| 32 | + Status: overallStatus, |
| 33 | + SpecVersion: MCPGatewaySpecVersion, |
| 34 | + GatewayVersion: gatewayVersion, |
| 35 | + Servers: serverStatus, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// HandleHealth returns an http.HandlerFunc that handles the /health endpoint |
| 40 | +// This function is used by both routed and unified modes to ensure consistent behavior |
| 41 | +func HandleHealth(unifiedServer *UnifiedServer) http.HandlerFunc { |
| 42 | + return func(w http.ResponseWriter, r *http.Request) { |
| 43 | + w.Header().Set("Content-Type", "application/json") |
| 44 | + w.WriteHeader(http.StatusOK) |
| 45 | + |
| 46 | + response := BuildHealthResponse(unifiedServer) |
| 47 | + json.NewEncoder(w).Encode(response) |
| 48 | + } |
| 49 | +} |
0 commit comments