refactor: dedupe HTTP middleware and tidy legacy docs (#107)#148
Conversation
Extract serverhttp.HandlerMiddleware for gatewayserver/https, warn on legacy grpc passthrough mode, and update p2p/legacy documentation. Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Code Review
This pull request refactors HTTP middleware handling by consolidating duplicate middleware logic from gatewayserver and https into a single, shared HandlerMiddleware in the serverhttp package. It also marks the grpcs package as deprecated and updates relevant documentation and tests. The review feedback highlights two key improvements: implementing a safe type assertion in the new middleware to prevent potential runtime panics, and ensuring the response body is closed in tests to avoid resource leaks.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| h := func(ctx context.Context, req lava.Request) (lava.Response, error) { | ||
| reqCtx := req.(*Request).Ctx | ||
| return NewResponse(reqCtx), reqCtx.Next() | ||
| } |
There was a problem hiding this comment.
The direct type assertion req.(*Request) is unsafe and can cause a runtime panic if any middleware in the chain wraps or customizes the lava.Request interface. To ensure robust defensive programming, use a safe type assertion with a comma-ok check and return a proper error (e.g., using fiber.NewError) if the assertion fails.
h := func(ctx context.Context, req lava.Request) (lava.Response, error) {
reqCtx, ok := req.(*Request)
if !ok {
return nil, fiber.NewError(fiber.StatusInternalServerError, "unexpected request type in middleware")
}
return NewResponse(reqCtx.Ctx), reqCtx.Ctx.Next()
}| resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/ping", nil)) | ||
| if err != nil { | ||
| t.Fatalf("handler: %v", err) | ||
| } |
There was a problem hiding this comment.
The response body returned by app.Test is not closed, which can leak resources (such as file descriptors or memory) during test execution. It is best practice to close the response body using defer resp.Body.Close() immediately after checking for errors.
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/ping", nil))
if err != nil {
t.Fatalf("handler: %v", err)
}
defer resp.Body.Close()
Summary
serverhttp.HandlerMiddlewareused by gatewayserver and https (refactor: gatewayserver 与 https 重复 httpRequest/httpResponse #107).servers/https/middleware.go→binder.go(Fiber binder setup only).grpc_passthrough: falselegacy dual-registration is used.servers/grpcs; fixdocs/design-p2p.mdstaleaaa.goreference.Note on pkg/lava/router.go
Keeping fiber/gRPC types in router interfaces is intentional — they match the underlying frameworks.
Test plan
go test ./servers/... -race -shortgo test ./... -race -shortMade with Cursor