feat(19.7-01): in-process dispatch marker and compiled handler lookup

- bouncer.Dispatch, WithDispatch, Dispatched: context-only pre-resolved
  principal marker honoured by Registry.Middleware for listed guard names
- cabana.ResolveBackend honours a backend-kind marker; additive
  ResolveBackendToken, ScopeLimitFor and BackendPrincipal resolvers
- surf.Router.Handler and compileHandlers: one compiled chain per route,
  shared by BuildRouter validation and compile
- smoke tests TestDispatchSmoke, TestDispatchedSmoke, TestDispatchMarkerSmoke
- READMEs and docs/services/{authentication,routing}.md updated
This commit is contained in:
Jakub Zych
2026-10-22 16:00:19 +02:00
parent a74a218cd9
commit 1bd91a4522
14 changed files with 522 additions and 11 deletions

View File

@@ -65,6 +65,9 @@ type Router struct {
defaultBytes int64
uploadBytes int64
built map[string]pact.Middleware
// handlers holds the compiled middleware chain of every route, keyed
// "METHOD /pattern", filled by compileHandlers; Handler reads it.
handlers map[string]http.Handler
app *backpack.App
adminPrefix string
adminMounted bool
@@ -351,17 +354,42 @@ func (r *Router) add(pluginID, prefix string, groupMW []string, method, path str
})
}
// compileHandlers wraps every registered route that has no compiled chain
// yet (constraints, named middleware, body limit, locale, recovery, CORS
// profile and the access-log pattern) and records it under "METHOD /pattern".
// BuildRouter calls it to validate the routes, compile reuses the chains and
// Handler hands one out for an in-process dispatch.
func (r *Router) compileHandlers() error {
if r == nil {
return fmt.Errorf("surf: router is nil")
}
if r.handlers == nil {
r.handlers = make(map[string]http.Handler)
}
for _, rt := range r.routes {
key := rt.method + " " + rt.path
if _, done := r.handlers[key]; done {
continue
}
h, err := r.wrap(rt)
if err != nil {
return err
}
r.handlers[key] = h
}
return nil
}
func (r *Router) compile() (http.Handler, error) {
if r.compileErr != nil {
return nil, r.compileErr
}
if err := r.compileHandlers(); err != nil {
return nil, err
}
handlers := make([]http.Handler, len(r.routes))
for i, rt := range r.routes {
h, err := r.wrap(rt)
if err != nil {
return nil, err
}
handlers[i] = h
handlers[i] = r.handlers[rt.method+" "+rt.path]
}
// Routes that ServeMux refuses side by side but Where constraints keep
// apart register as overlap families (overlap.go); the rest as before.
@@ -652,10 +680,8 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) {
r.pages.setAdminPrefix(admin.Prefix)
r.adminMounted = true
}
for _, rt := range r.routes {
if _, err := r.wrap(rt); err != nil {
return nil, err
}
if err := r.compileHandlers(); err != nil {
return nil, err
}
warnTurnstileWithoutSecret(app, turnstile, r.routes)
if err := r.addCORSPreflights(); err != nil {