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
10 changes: 10 additions & 0 deletions internal/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,37 @@ import (
"github.com/githubnext/gh-aw-mcpg/internal/logger"
)

var logHandlers = logger.New("server:handlers")

// handleOAuthDiscovery returns a handler for OAuth discovery endpoint
// Returns 404 since the gateway doesn't use OAuth
func handleOAuthDiscovery() http.Handler {
logHandlers.Print("Creating OAuth discovery handler")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logHandlers.Printf("OAuth discovery request: remote=%s, method=%s, path=%s", r.RemoteAddr, r.Method, r.URL.Path)
log.Printf("[%s] %s %s - OAuth discovery (not supported)", r.RemoteAddr, r.Method, r.URL.Path)
http.NotFound(w, r)
})
}

// handleClose returns a handler for graceful shutdown endpoint (spec 5.1.3)
func handleClose(unifiedServer *UnifiedServer) http.Handler {
logHandlers.Print("Creating close handler")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logHandlers.Printf("Close request received: remote=%s, method=%s, path=%s", r.RemoteAddr, r.Method, r.URL.Path)
log.Printf("[%s] %s %s", r.RemoteAddr, r.Method, r.URL.Path)
logger.LogInfo("shutdown", "Close endpoint called, remote=%s", r.RemoteAddr)

// Only accept POST requests
if r.Method != http.MethodPost {
logHandlers.Printf("Close request rejected: invalid method=%s", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}

// Check if already closed (idempotency - spec 5.1.3)
if unifiedServer.IsShutdown() {
logHandlers.Print("Gateway already shutdown, returning 410 Gone")
logger.LogWarn("shutdown", "Close endpoint called but gateway already closed, remote=%s", r.RemoteAddr)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusGone) // 410 Gone
Expand All @@ -43,7 +51,9 @@ func handleClose(unifiedServer *UnifiedServer) http.Handler {
}

// Initiate shutdown and get server count
logHandlers.Print("Initiating gateway shutdown")
serversTerminated := unifiedServer.InitiateShutdown()
logHandlers.Printf("Shutdown completed: servers_terminated=%d", serversTerminated)

// Return success response (spec 5.1.3)
w.Header().Set("Content-Type", "application/json")
Expand Down