Skip to content

httpapi: add support for gg/pathrouter - #366

Open
majewsky wants to merge 6 commits into
masterfrom
httpapi-pathrouter
Open

majewsky wants to merge 6 commits into
masterfrom
httpapi-pathrouter

Conversation

@majewsky

@majewsky majewsky commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

See sapcc/keppel#781 for reasoning: To support gg/pathrouter, this extends httpapi.Composer to allow bypassing the main gorilla/mux router and registering TryHandlers directly. If there are any, request routing will try them first before going into the gorilla/mux router (which is important both because the gorilla/mux router forces a 404 response when it does not match anything, so any other option must be exhausted first, and also because going into a gorilla/mux router means paying the price of matching regexes for the routes contained within).

This is split into several small commits that I recommend reviewing individually.

We do not need to register another middleware for this if we already
have `type middleware` (which also should be renamed to clarify that it
is the outermost middleware instead of just _any_ middleware).
Also clarify its role by adding code comments.
Right now, this change is pretty nonsensical because there are no other
options, but the next commit will add a non-gorilla/mux option.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

EndpointNamer now executes before routing, breaking route-context-based endpoint labeling.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds gg/pathrouter support alongside gorilla/mux, prioritizing lightweight TryHandler routing.

Changes:

  • Adds lazy mux creation and ordered TryHandler dispatch.
  • Refactors middleware composition and routing tests.
  • Upgrades go.xyrillian.de/gg to v1.17.0.
File Description
httpapi/​api.go Adds the TryHandler API.
httpapi/​compose.go Dispatches try-handlers before mux.
httpapi/​middleware.go Refactors instrumentation middleware.
httpapi/​httpapi_test.go Tests both routing mechanisms.
go.mod Updates the gg dependency.
go.sum Updates dependency checksums.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread httpapi/middleware.go Outdated
I cannot find any uses of it in all known downstream users. If there are
any that I could not find, they can be replaced by a pseudo-API like this:

```go
func (endpointNamer) AddTo(c *httpapi.Composer) {
  c.Router().Use(func (next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      IdentifyEndpoint(r, mux.CurrentRoute(r).GetName()) // replace as appropriate
      next.ServeHTTP(w, r)
    })
  }
}
```

I chose not to retain EndpointNamer because its semantics conflict with
the existence of TryHandlers into which we cannot introspect.
EndpointNamer is specified as executing after gorilla/mux has made a routing
decision, so we cannot run it in any of the middlewares (before the
routing decision). But that's the only places where we can interact with
a request that ends up being answered by a TryHandler.

It would only have been possible to keep EndpointNamer, but document it
as specifically only applying to gorilla/mux routes, which is a weird
unexpected restriction. This dependency should rather be made explicit
by using a pseudo-API as in the code snippet above.
@majewsky
majewsky force-pushed the httpapi-pathrouter branch from 814629d to 4d56f00 Compare October 5, 2026 11:15
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

Merging this branch will decrease overall coverage

Impacted Packages Coverage Δ 🤖
github.com/sapcc/go-bits/httpapi 90.96% (-0.61%) 👎
github.com/sapcc/go-bits/liquidapi 17.04% (-0.25%) 👎

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/sapcc/go-bits/httpapi/api.go 88.89% (+2.22%) 252 (+42) 224 (+42) 28 👍
github.com/sapcc/go-bits/httpapi/compose.go 81.58% (-2.63%) 532 434 (-14) 98 (+14) 👎
github.com/sapcc/go-bits/httpapi/middleware.go 93.83% (-0.29%) 1134 (-56) 1064 (-56) 70 👎

Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code.

Changed unit test files

  • github.com/sapcc/go-bits/httpapi/httpapi_test.go

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.

2 participants