Skip to content

Commit 7a65327

Browse files
committed
Remove duplication
Signed-off-by: David Gageot <[email protected]>
1 parent 978f830 commit 7a65327

File tree

1 file changed

+28
-33
lines changed

1 file changed

+28
-33
lines changed

cmd/docker-mcp/internal/gateway/transport.go

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"sync"
1010

1111
"github.com/mark3labs/mcp-go/server"
12+
13+
"github.com/docker/mcp-gateway/cmd/docker-mcp/internal/health"
1214
)
1315

1416
func (g *Gateway) startStdioServer(ctx context.Context, mcpServer *server.MCPServer, stdin io.Reader, stdout io.Writer) error {
@@ -17,19 +19,12 @@ func (g *Gateway) startStdioServer(ctx context.Context, mcpServer *server.MCPSer
1719

1820
func (g *Gateway) startSseServer(ctx context.Context, mcpServer *server.MCPServer, ln net.Listener) error {
1921
mux := http.NewServeMux()
22+
mux.Handle("/health", healthHandler(&g.health))
23+
mux.Handle("/", redirectHandler("/sse"))
24+
2025
sseServer := server.NewSSEServer(mcpServer)
21-
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
22-
http.Redirect(w, r, "/sse", http.StatusTemporaryRedirect)
23-
})
2426
mux.Handle("/sse", sseServer.SSEHandler())
2527
mux.Handle("/message", sseServer.MessageHandler())
26-
mux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
27-
if g.health.IsHealthy() {
28-
w.WriteHeader(http.StatusOK)
29-
} else {
30-
w.WriteHeader(http.StatusServiceUnavailable)
31-
}
32-
})
3328
httpServer := &http.Server{
3429
Handler: mux,
3530
}
@@ -42,17 +37,10 @@ func (g *Gateway) startSseServer(ctx context.Context, mcpServer *server.MCPServe
4237

4338
func (g *Gateway) startStreamingServer(ctx context.Context, mcpServer *server.MCPServer, ln net.Listener) error {
4439
mux := http.NewServeMux()
45-
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
46-
http.Redirect(w, r, "/mcp", http.StatusTemporaryRedirect)
47-
})
40+
mux.Handle("/health", healthHandler(&g.health))
41+
mux.Handle("/", redirectHandler("/mcp"))
42+
4843
mux.Handle("/mcp", server.NewStreamableHTTPServer(mcpServer))
49-
mux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
50-
if g.health.IsHealthy() {
51-
w.WriteHeader(http.StatusOK)
52-
} else {
53-
w.WriteHeader(http.StatusServiceUnavailable)
54-
}
55-
})
5644
httpServer := &http.Server{
5745
Handler: mux,
5846
}
@@ -65,13 +53,12 @@ func (g *Gateway) startStreamingServer(ctx context.Context, mcpServer *server.MC
6553
}
6654

6755
func (g *Gateway) startCentralStreamingServer(ctx context.Context, newMCPServer func() *server.MCPServer, ln net.Listener, configuration Configuration) error {
56+
mux := http.NewServeMux()
57+
mux.Handle("/health", healthHandler(&g.health))
58+
mux.Handle("/", redirectHandler("/mcp"))
59+
6860
var lock sync.Mutex
6961
handlersPerSelectionOfServers := map[string]*server.StreamableHTTPServer{}
70-
71-
mux := http.NewServeMux()
72-
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
73-
http.Redirect(w, r, "/mcp", http.StatusTemporaryRedirect)
74-
})
7562
mux.HandleFunc("/mcp", func(w http.ResponseWriter, r *http.Request) {
7663
serverNames := r.Header.Get("x-mcp-servers")
7764
if len(serverNames) == 0 {
@@ -98,14 +85,6 @@ func (g *Gateway) startCentralStreamingServer(ctx context.Context, newMCPServer
9885

9986
handler.ServeHTTP(w, r)
10087
})
101-
102-
mux.HandleFunc("/health", func(w http.ResponseWriter, _ *http.Request) {
103-
if g.health.IsHealthy() {
104-
w.WriteHeader(http.StatusOK)
105-
} else {
106-
w.WriteHeader(http.StatusServiceUnavailable)
107-
}
108-
})
10988
httpServer := &http.Server{
11089
Handler: mux,
11190
}
@@ -125,3 +104,19 @@ func parseServerNames(serverNames string) []string {
125104
}
126105
return names
127106
}
107+
108+
func redirectHandler(target string) http.HandlerFunc {
109+
return func(w http.ResponseWriter, r *http.Request) {
110+
http.Redirect(w, r, target, http.StatusTemporaryRedirect)
111+
}
112+
}
113+
114+
func healthHandler(state *health.State) http.HandlerFunc {
115+
return func(w http.ResponseWriter, _ *http.Request) {
116+
if state.IsHealthy() {
117+
w.WriteHeader(http.StatusOK)
118+
} else {
119+
w.WriteHeader(http.StatusServiceUnavailable)
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)