Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d90bbcb

Browse files
committed
chore(middleware): use huma middleware
1 parent 911109d commit d90bbcb

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

lib/httpapi/server.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -189,23 +189,6 @@ func NewServer(ctx context.Context, config ServerConfig) (*Server, error) {
189189
})
190190
router.Use(corsMiddleware.Handler)
191191

192-
// Add SSE middleware to prevent proxy buffering
193-
sseMiddleware := func(next http.Handler) http.Handler {
194-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
195-
if strings.HasSuffix(r.URL.Path, "/events") || strings.HasSuffix(r.URL.Path, "/screen") {
196-
// Disable proxy buffering for SSE endpoints
197-
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
198-
w.Header().Set("Pragma", "no-cache")
199-
w.Header().Set("Expires", "0")
200-
w.Header().Set("X-Accel-Buffering", "no") // nginx
201-
w.Header().Set("X-Proxy-Buffering", "no") // generic proxy
202-
w.Header().Set("Connection", "keep-alive")
203-
}
204-
next.ServeHTTP(w, r)
205-
})
206-
}
207-
router.Use(sseMiddleware)
208-
209192
humaConfig := huma.DefaultConfig("AgentAPI", version.Version)
210193
humaConfig.Info.Description = "HTTP API for Claude Code, Goose, and Aider.\n\nhttps://github.com/coder/agentapi"
211194
api := humachi.New(router, humaConfig)
@@ -280,6 +263,19 @@ func hostAuthorizationMiddleware(allowedHosts []string, badHostHandler http.Hand
280263
}
281264
}
282265

266+
// sseMiddleware creates middleware that prevents proxy buffering for SSE endpoints
267+
func sseMiddleware(ctx huma.Context, next func(huma.Context)) {
268+
// Disable proxy buffering for SSE endpoints
269+
ctx.SetHeader("Cache-Control", "no-cache, no-store, must-revalidate")
270+
ctx.SetHeader("Pragma", "no-cache")
271+
ctx.SetHeader("Expires", "0")
272+
ctx.SetHeader("X-Accel-Buffering", "no") // nginx
273+
ctx.SetHeader("X-Proxy-Buffering", "no") // generic proxy
274+
ctx.SetHeader("Connection", "keep-alive")
275+
276+
next(ctx)
277+
}
278+
283279
func (s *Server) StartSnapshotLoop(ctx context.Context) {
284280
s.conversation.StartSnapshotLoop(ctx)
285281
go func() {
@@ -316,6 +312,7 @@ func (s *Server) registerRoutes() {
316312
Path: "/events",
317313
Summary: "Subscribe to events",
318314
Description: "The events are sent as Server-Sent Events (SSE). Initially, the endpoint returns a list of events needed to reconstruct the current state of the conversation and the agent's status. After that, it only returns events that have occurred since the last event was sent.\n\nNote: When an agent is running, the last message in the conversation history is updated frequently, and the endpoint sends a new message update event each time.",
315+
Middlewares: []func(huma.Context, func(huma.Context)){sseMiddleware},
319316
}, map[string]any{
320317
// Mapping of event type name to Go struct for that event.
321318
"message_update": MessageUpdateBody{},
@@ -328,6 +325,7 @@ func (s *Server) registerRoutes() {
328325
Path: "/internal/screen",
329326
Summary: "Subscribe to screen",
330327
Hidden: true,
328+
Middlewares: []func(huma.Context, func(huma.Context)){sseMiddleware},
331329
}, map[string]any{
332330
"screen": ScreenUpdateBody{},
333331
}, s.subscribeScreen)

0 commit comments

Comments
 (0)