Skip to content

refactor: dedupe HTTP middleware and tidy legacy docs (#107)#148

Merged
kooksee merged 1 commit into
v2from
refactor/serverhttp-dedup-legacy
Jul 5, 2026
Merged

refactor: dedupe HTTP middleware and tidy legacy docs (#107)#148
kooksee merged 1 commit into
v2from
refactor/serverhttp-dedup-legacy

Conversation

@kooksee

@kooksee kooksee commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Extract shared serverhttp.HandlerMiddleware used by gatewayserver and https (refactor: gatewayserver 与 https 重复 httpRequest/httpResponse #107).
  • Rename servers/https/middleware.gobinder.go (Fiber binder setup only).
  • Warn (not debug) when grpc_passthrough: false legacy dual-registration is used.
  • Add deprecated package doc for servers/grpcs; fix docs/design-p2p.md stale aaa.go reference.

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 -short
  • go test ./... -race -short

Made with Cursor

Extract serverhttp.HandlerMiddleware for gatewayserver/https, warn on legacy grpc passthrough mode, and update p2p/legacy documentation.

Co-authored-by: Cursor <cursoragent@cursor.com>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +14 to +17
h := func(ctx context.Context, req lava.Request) (lava.Response, error) {
reqCtx := req.(*Request).Ctx
return NewResponse(reqCtx), reqCtx.Next()
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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()
	}

Comment on lines +33 to +36
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/ping", nil))
if err != nil {
t.Fatalf("handler: %v", err)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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()

@kooksee kooksee merged commit ad548f9 into v2 Jul 5, 2026
2 checks passed
@kooksee kooksee deleted the refactor/serverhttp-dedup-legacy branch July 5, 2026 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant