TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/go-chi/chi/llms.txt
Use this file to discover all available pages before exploring further.
Routes interface exposes chi’s routing tree for inspection and testing without making an actual HTTP request. It is embedded in the Router interface, so every chi router automatically satisfies it. The docgen sub-package uses this interface to generate human-readable API documentation straight from a live router.
Routes Interface
chi.go
Routes() []Route
mux.go
Route values. Each entry describes one registered pattern, the HTTP methods it handles, and — for mounted sub-routers — the sub-router itself.
[]Route
A traversable slice of all registered routes in the trie. The order reflects the internal trie walk.
walk_routes.go
walk_routes_recursive.go
Middlewares() Middlewares
mux.go
Use(). For inline sub-routers this reflects only the middlewares added at that scope level.
Middlewares
The ordered slice of middleware functions; may be empty but never nil.
inspect.go
Match
chi.go / mux.go
rctx with URL parameters and matched patterns — exactly like a real request, but without executing the handler.
A routing context to receive match results (URL params, route patterns). Create a fresh one with
chi.NewRouteContext() to avoid state pollution.The HTTP method to match, e.g.
"GET".The URL path to test, e.g.
"/users/42".bool
true when a matching handler was found; false otherwise.router_test.go
Find
chi.go / mux.go
Match, it populates rctx without executing any handler. Returns an empty string when nothing matches.
A routing context to receive match state.
The HTTP method to search for.
The URL path to search for.
string
The matched route pattern, e.g.
"/users/{id}", or "" if no match.router_test.go
Route Struct
Each element of the[]Route slice returned by Routes() has the following shape:
tree.go
The full URL pattern for this route node, e.g.
"/users/{id}".A map from HTTP method strings (e.g.
"GET", "POST") to their registered http.Handler. The key "*" represents a catch-all for any method.Non-nil for routes created via
Mount or Route. Contains the mounted sub-router, allowing recursive traversal of the entire routing tree.Middlewares Type
chi.go
Middlewares is a named slice type for standard middleware functions. Beyond acting as a plain slice, it carries the Handler and HandlerFunc methods from chain.go that let you compose a full http.Handler from the stack without attaching the chain to a router.
Chain — Composing Middleware Outside a Router
Chain and the Middlewares methods in chain.go allow you to build a reusable middleware stack that can be applied to individual handlers without going through a Mux.
Chain
chain.go
Middlewares type and returns them, enabling the .Handler and .HandlerFunc methods.
The ordered middleware functions to include in the chain. Executed left-to-right.
Middlewares
A
Middlewares slice ready to have a final handler attached.chain_example.go
Middlewares.Handler
chain.go
*ChainHandler by wrapping h with the full middleware stack. Middlewares execute in the order they were passed to Chain.
The final endpoint handler to wrap.
http.Handler
A
*ChainHandler that, when served, runs the entire middleware chain before calling h.chain_example.go
Middlewares.HandlerFunc
chain.go
Handler but accepts a http.HandlerFunc for convenience.
The final endpoint handler function to wrap.
http.Handler
A
*ChainHandler wrapping the provided function with the middleware stack.chain_example.go
ChainHandler
Chain/Handler/HandlerFunc all produce a *ChainHandler:
chain.go
The unwrapped final handler, accessible for inspection or testing without running the middleware stack.
The middleware functions in use by this chain, in execution order.
ChainHandler itself satisfies http.Handler via ServeHTTP, which delegates to the private chain field (the fully composed handler).
WalkFunc
tree.go
WalkFunc is the callback type passed to Walk. It is called once per unique method-and-route combination in the router tree, including routes inside mounted sub-routers. Returning a non-nil error from the callback stops the walk and causes Walk to return that error.
The HTTP method for this route entry, e.g.
"GET" or "POST".The full route pattern from the root, e.g.
"/api/v1/users/{id}".The registered endpoint handler (after the middleware chain is stripped).
All middleware functions that apply to this route, aggregated from the root through all sub-routers.
Walk
tree.go
r, calling walkFn for every registered method-and-route pair. Sub-routers mounted via Mount or Route are traversed recursively. This is the primary mechanism for route introspection and API documentation generation.
The router to walk. Any value that satisfies the
Routes interface — typically a *Mux.The callback to invoke for each method-and-route pair.
error
The first non-nil error returned by
walkFn, or nil if the walk completes without error.walk_example.go
The
docgen package at github.com/go-chi/chi/v5/docgen provides production-ready helpers that walk the Routes tree and emit Markdown, JSON, or other formats. It relies entirely on the Routes interface and Walk — no reflection or code generation required.Example: Manual Route Traversal
For cases where you need direct access to theRoute struct (e.g. to inspect sub-routers or handlers individually), iterate Routes() recursively:
traverse_example.go