mirror of
https://github.com/hanzoai/o11y.git
synced 2026-10-15 12:58:33 +00:00
gorilla/mux, otelmux, gorilla/handlers and rs/cors leave go.mod. Every route the runtime serves is a zip route on a zip group: 233 from the apiserver tree, 134 from query-service, 367 total — the same 367 the mount publishes. pkg/http/routing is the one registration surface. It exists because a registration carries three facts zip has no slot for, and the tree this replaces recovered all three PER REQUEST by asking the router what it had just matched: the bound segments, the path template, and the route's declared resources. mux.CurrentRoute(req).GetHandler() was type-asserted back to handler.Handler in two separate packages to re-derive at request time what was already known once at boot. Stating them at registration deletes both lookups. The middleware chain is composed at the LEAF rather than registered ambient, because Audit keys on the route template and Resource resolves the route's declared resources — ambient middleware runs before the router has matched, so both would read an empty route. Per-leaf composition reproduces the order the old tree ran in exactly: match, then middleware, then handler. Duplicate registration now panics at boot; gorilla let the first one win. The Sentry ingest constraint becomes fiber's own guid — a UUID parse instead of a hand-written character class. Paths keep their public brace spelling in the source and in the table; the respelling happens in one function. The standalone server hands its listener to the app, so streams and the websocket upgrade run native fasthttp. Counted, not asserted: routes_test.go in the apiserver pins 233 against the real AddToRouter, routing_test.go pins the table against the router itself. This repo has already shipped 83 typed ops that reached no router while the suite stayed green — an uncalled func is legal Go, so arithmetic is the test. Co-authored-by: Hanzo Dev <[email protected]>
55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
package middleware
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/hanzoai/o11y/pkg/http/handler"
|
|
"github.com/hanzoai/o11y/pkg/types/coretypes"
|
|
)
|
|
|
|
// Resource resolves a route's declared ResourceDefs and stashes the result in
|
|
// the request context for authz and audit to read.
|
|
type Resource struct {
|
|
logger *slog.Logger
|
|
}
|
|
|
|
func NewResource(logger *slog.Logger) *Resource {
|
|
return &Resource{logger: logger.With(slog.String("pkg", pkgname))}
|
|
}
|
|
|
|
// For builds the middleware for a route with these declared resources.
|
|
//
|
|
// The defs are a fact about the ROUTE, so they arrive from the router that
|
|
// registered it. They used to be recovered per request — CurrentRoute(req) back
|
|
// to the route, GetHandler back to the handler, a type assertion back to the
|
|
// declaration — three hops to re-derive at request time what was already known
|
|
// once at boot. A route with nothing declared now gets no middleware at all
|
|
// rather than one that checks and returns.
|
|
func (middleware *Resource) For(defs []handler.ResourceDef) func(http.Handler) http.Handler {
|
|
if len(defs) == 0 {
|
|
return func(next http.Handler) http.Handler { return next }
|
|
}
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
// Buffer the body once so extractors can read it and the handler still sees a fresh reader.
|
|
var body []byte
|
|
if req.Body != nil {
|
|
body, _ = io.ReadAll(req.Body)
|
|
req.Body = io.NopCloser(bytes.NewReader(body))
|
|
}
|
|
|
|
resolved := handler.ResolveRequest(defs, coretypes.ExtractorContext{
|
|
Request: req,
|
|
RequestBody: body,
|
|
})
|
|
|
|
ctx := coretypes.NewContextWithResolvedResources(req.Context(), resolved)
|
|
next.ServeHTTP(rw, req.WithContext(ctx))
|
|
})
|
|
}
|
|
}
|