Skip to content

refactor: unify middleware application for API routes and tests#1074

Merged
burma-shave merged 2 commits into
mainfrom
refactor/unify-middleware-production-test-paths
Jun 16, 2026
Merged

refactor: unify middleware application for API routes and tests#1074
burma-shave merged 2 commits into
mainfrom
refactor/unify-middleware-production-test-paths

Conversation

@Ahmedhossamdev

@Ahmedhossamdev Ahmedhossamdev commented Jun 15, 2026

Copy link
Copy Markdown
Member

Fixes: #1045

Summary

Production and test middleware stacks had drifted apart. SetupAPIRoutes was only used in tests, which led to VersionValidationMiddleware being tested but never running in production (#1004).

Changes

  • Added GtfsExpiryMiddleware and VersionValidationMiddleware to cmd/api.CreateServer.
  • Moved SetupAPIRoutes to routes_test.go since it's only used by tests.
  • Ensured production and test behavior are aligned for API middleware.

Summary by CodeRabbit

  • Refactor
    • Restructured middleware application to improve request handling efficiency. API-specific validation and compression are now applied more selectively based on request routes, reducing overhead on non-API endpoints.

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@Ahmedhossamdev, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 58 minutes and 1 second. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a9f88e57-f957-40ea-82b8-1e33988643ce

📥 Commits

Reviewing files that changed from the base of the PR and between 357d9ff and 9beb305.

📒 Files selected for processing (2)
  • cmd/api/app.go
  • internal/restapi/routes_test.go
📝 Walkthrough

Walkthrough

SetupAPIRoutes is removed from production code in routes.go and recreated as a test-only method in routes_test.go. Concurrently, CreateServer in app.go gains an apiHandler sub-pipeline that applies GtfsExpiryMiddleware and VersionValidationMiddleware exclusively to /api/ requests before compression wrapping.

Changes

Middleware Chain Reconciliation

Layer / File(s) Summary
Production API middleware pipeline
cmd/api/app.go
CreateServer now builds an apiHandler from the base mux and applies GtfsExpiryMiddleware and VersionValidationMiddleware to it before compression, scoping those middlewares to the /api/ prefix only.
SetupAPIRoutes moved to test code
internal/restapi/routes.go, internal/restapi/routes_test.go
SetupAPIRoutes is removed from production routes.go and added as a test-only method in routes_test.go, preserving the full middleware chain (GtfsExpiry → VersionValidation → Freshness → Compression) for test consumers only.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: unifying middleware application for API routes and tests by resolving divergence between production and test middleware stacks.
Linked Issues check ✅ Passed The PR fully addresses both requirements from issue #1045: it audits and reconciles middleware chains by adding GtfsExpiryMiddleware and VersionValidationMiddleware to production, and moves SetupAPIRoutes to routes_test.go.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the objectives: middleware application refactoring in app.go, removing SetupAPIRoutes from routes.go, and relocating it to routes_test.go with test-only helpers.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/unify-middleware-production-test-paths

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

Copy link
Copy Markdown

Performance Smoke Test Results

Status: PASSED

Metric Value
p(95) latency 1.7 ms
Error rate 0.00%
Total requests 337
Req/sec 11.1

Smoke test config: 5 VUs x 30s. Thresholds: p(95) < 300ms, error rate < 1%.

Full results uploaded as workflow artifact: k6-smoke-summary.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
cmd/api/app.go (1)

178-178: ⚡ Quick win

Update comment to reflect actual wrapping target.

The comment says "Apply global compression around the entire mux" but the code wraps apiHandler (which is the mux with API-specific middleware already applied), not the bare mux.

📝 Suggested comment update
-	// Apply global compression around the entire mux
+	// Apply compression to the API handler (mux with API-specific middleware)
 	compressedMux := restapi.CompressionMiddleware(apiHandler)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cmd/api/app.go` at line 178, The comment for the CompressionMiddleware call
at the assignment to compressedMux incorrectly states that compression is being
applied around "the entire mux", but the actual code wraps apiHandler (which is
already the mux with API-specific middleware applied). Update the comment to
accurately describe what is being wrapped, specifying that the compression
middleware is being applied around apiHandler rather than the bare mux.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/restapi/routes_test.go`:
- Around line 15-20: The middleware chain in the test setup has
CompressionMiddleware and api.FreshnessMiddleware in the wrong order compared to
production. Swap the positions of these two middleware assignments so that
api.FreshnessMiddleware wraps CompressionMiddleware (making it the outer layer),
matching the production order documented in cmd/api/app.go. Additionally, update
the comment on line 6-7 to correctly reflect the actual production middleware
order with Freshness as the outermost layer wrapping Compression.

---

Nitpick comments:
In `@cmd/api/app.go`:
- Line 178: The comment for the CompressionMiddleware call at the assignment to
compressedMux incorrectly states that compression is being applied around "the
entire mux", but the actual code wraps apiHandler (which is already the mux with
API-specific middleware applied). Update the comment to accurately describe what
is being wrapped, specifying that the compression middleware is being applied
around apiHandler rather than the bare mux.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 311fbbbd-9568-4c02-ba43-ab02662a6ce5

📥 Commits

Reviewing files that changed from the base of the PR and between 9898b8f and 357d9ff.

📒 Files selected for processing (3)
  • cmd/api/app.go
  • internal/restapi/routes.go
  • internal/restapi/routes_test.go
💤 Files with no reviewable changes (1)
  • internal/restapi/routes.go

Comment thread internal/restapi/routes_test.go Outdated
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

Performance Smoke Test Results

Status: PASSED

Metric Value
p(95) latency 2.0 ms
Error rate 0.00%
Total requests 333
Req/sec 11.0

Smoke test config: 5 VUs x 30s. Thresholds: p(95) < 300ms, error rate < 1%.

Full results uploaded as workflow artifact: k6-smoke-summary.

@burma-shave burma-shave merged commit 3a807e1 into main Jun 16, 2026
9 checks passed
@burma-shave burma-shave deleted the refactor/unify-middleware-production-test-paths branch June 16, 2026 22: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.

Audit and reconcile prod vs test middleware chains; move SetupAPIRoutes to test code

2 participants