Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion internal/server/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package server
import (
"encoding/json"
"net/http"

"github.com/githubnext/gh-aw-mcpg/internal/logger"
)

var logHealth = logger.New("server:health")

// HealthResponse represents the JSON structure for the /health endpoint response
// as defined in MCP Gateway Specification section 8.1.1
type HealthResponse struct {
Expand All @@ -16,17 +20,22 @@ type HealthResponse struct {

// BuildHealthResponse constructs a HealthResponse from the unified server's status
func BuildHealthResponse(unifiedServer *UnifiedServer) HealthResponse {
logHealth.Print("Building health response")

// Get server status
serverStatus := unifiedServer.GetServerStatus()
logHealth.Printf("Retrieved status for %d servers", len(serverStatus))

// Determine overall health based on server status
overallStatus := "healthy"
for _, status := range serverStatus {
for serverName, status := range serverStatus {
if status.Status == "error" {
logHealth.Printf("Server error detected: server=%s, marking overall status as unhealthy", serverName)
overallStatus = "unhealthy"
break
}
}
logHealth.Printf("Overall health status determined: %s", overallStatus)

return HealthResponse{
Status: overallStatus,
Expand All @@ -40,10 +49,13 @@ func BuildHealthResponse(unifiedServer *UnifiedServer) HealthResponse {
// This function is used by both routed and unified modes to ensure consistent behavior
func HandleHealth(unifiedServer *UnifiedServer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logHealth.Printf("Health check request: method=%s, remote=%s", r.Method, r.RemoteAddr)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

response := BuildHealthResponse(unifiedServer)
logHealth.Printf("Health response: status=%s, servers=%d", response.Status, len(response.Servers))
json.NewEncoder(w).Encode(response)
}
}