router
Jump to function (12) ›
router/CompiledSymfonyRouter#
(CompiledSymfonyRouter normalized-routes matcher generator indexed-routes)
Creates a new CompiledSymfonyRouter struct.
router/CompiledSymfonyRouter?#
(CompiledSymfonyRouter? x)
Checks if x is an instance of the CompiledSymfonyRouter struct.
router/SymfonyRouter#
(SymfonyRouter normalized-routes route-collection matcher generator)
Creates a new SymfonyRouter struct.
router/SymfonyRouter?#
(SymfonyRouter? x)
Checks if x is an instance of the SymfonyRouter struct.
router/compiled-router#
(compiled-router raw-routes & [options])
Like router but performs Symfony's route compilation at macro-expansion
time. The resulting router uses Symfony's CompiledUrlMatcher/Generator
which are ~3x faster than their dynamic counterparts for large route
tables.
Because compilation runs during macro expansion, raw-routes must be a
literal vector at the call site, it cannot be built from runtime values.
Use router if your routes are dynamic.
Example:
(compiled-router [["/ping" {:get {:handler pong}}]])
router/flatten-routes#
(flatten-routes raw-routes path-prefix common-data)
Flattens nested routes to a vector of [path data] tuples.
Parent paths are concatenated with their children's paths and parent data
maps are deep-merged into their children's data. path-prefix and
common-data are merged into every route and let you mount a subtree under
a prefix or share data across all routes.
Example:
(flatten-routes ["/api" {:middleware [:auth]}
["/ping" {:handler :ping}]] "" {})
; => [["/api/ping" {:middleware [:auth] :handler :ping}]]
router/generate#
(generate this route-name parameter)
Generate a url for a route
router/handler#
(handler router & [options])
Builds a request -> response function from a router.
Matching flow per request:
- Resolve the request's path via
match-by-path. - Look up the handler for the request method in the precompiled dispatch
table, falling back to the route's method-agnostic
:handler. - Invoke the handler with the request enriched with
[:attributes :match]and[:attributes :route-data]. Anilresponse triggers the:not-acceptablehandler.
Options:
| key | description |
|---|---|
:middleware | global middleware applied to every matched route |
:default-handler | fallback used for any 404/405/406 case not covered by a specific override |
:not-found | handler invoked when no route matches (HTTP 404) |
:method-not-allowed | handler invoked when the path matches but no handler exists for the request method (HTTP 405) |
:not-acceptable | handler invoked when a matched handler returns nil (HTTP 406) |
Dispatch is precompiled at handler construction time, so per-request work is reduced to two hash-map lookups.
Example:
(handler (router [["/ping" {:get {:handler pong}}]])
{:middleware [logging-mw]
:not-found (fn [_] {:status 404 :body "nope"})})
router/match-by-name#
(match-by-name this route-name)
Matches a route given a route name. Returns nil if route can't be found.
router/match-by-path#
(match-by-path this path)
Matches a route given a path. Returns nil if path doesn't match.
router/router#
(router raw-routes & [options])
Builds a dynamic Router from a nested route tree.
Routes are described as [path data children*] where data is an optional
hash-map. Children inherit their parent's path prefix and have parent data
deep-merged into their own. data may contain:
:handler, a 1-argrequest -> responsefunction (matches any method):middleware, a vector of(fn [handler request])wrappers:name, a keyword used for URL generation viamatch-by-name/generate- method keys (
:get,:post, …), nested maps with their own:handlerand:middlewarethat apply only for that HTTP method
options accepts :path (prefix prepended to every route) and :data
(merged into every route's data).
Example:
(router [["/ping" {:get {:handler pong}}]])
router/routes#
(routes this)
Returns all registered routes as a vector of [path data] tuples.