Data driven routes functionality in Calfpath.
Data driven routes functionality in Calfpath.
(assoc-kv-middleware route main-key-finder assoc-map)
Given a route, if the route contains the main key then ensure that it also has the associated key/value pairs.
Given a route, if the route contains the main key then ensure that it also has the associated key/value pairs.
(assoc-route-to-request-middleware route)
(assoc-route-to-request-middleware route route-key)
Given a route, decorate the handler such that the request has the route under specified key (:route
by default)
at runtime.
Given a route, decorate the handler such that the request has the route under specified key (`:route` by default) at runtime.
(compile-routes routes)
(compile-routes routes
{:keys [easy? tidy? tidy-threshold uri? uri-key fallback-400?
show-uris-400? full-uri-key uri-prefix-400 params-key
method? method-key fallback-405? trailing-slash
lift-uri? ring-handler? ring-handler-key]
:or {tidy-threshold 1
full-uri-key :full-uri
method? true
trailing-slash false
tidy? true
method-key :method
uri? true
params-key :path-params
fallback-405? true
lift-uri? true
show-uris-400? true
fallback-400? true
uri-key :uri
easy? true}
:as options})
Given a collection of routes, supplement them with required entries and finally return a routes collection.
Kwarg | Type | Description |
---|---|---|
:easy? | boolean | allow easy defnition of routes that translate into regular routes |
:tidy? | boolean | optimize URI routes by automatically reorganizing routes |
:tidy-threshold | integer | similar routes more than this number will be grouped together |
:uri? | boolean | true if URI templates should be converted to matchers |
:uri-key | non-nil | the key to be used to look up the URI template in a route |
:params-key | non-nil | the key to put URI params under in the request map |
:trailing-slash | keyword | Trailing-slash action to perform on URIs - :add or :remove - nil (default) has no effect |
:fallback-400? | boolean | whether to add a fallback route to respond with HTTP status 400 for unmatched URIs |
:show-uris-400? | boolean | whether to add URI templates in the HTTP 400 response (see :fallback-400?) |
:full-uri-key | non-nil | the key to be used to populate full-uri for reporting HTTP 400 (see :show-uris-400?) |
:uri-prefix-400 | string? | the URI prefix to use when showing URI templates in HTTP 400 (see :show-uris-400?) |
:method? | boolean | true if HTTP methods should be converted to matchers |
:method-key | non-nil | the key to be used to look up the method key/set in a route |
:fallback-405? | boolean | whether to add a fallback route to respond with HTTP status 405 for unmatched methods |
:lift-uri? | boolean | whether lift URI attributes from mixed routes and move the rest into nested routes |
See: dispatch
, make-dispatcher
(Clojure/JVM only), make-index
Given a collection of routes, supplement them with required entries and finally return a routes collection. ### Options | Kwarg | Type | Description | |-----------------|-------|----------------------------------------------------------------------------------------| |`:easy?` |boolean|allow easy defnition of routes that translate into regular routes | |`:tidy?` |boolean|optimize URI routes by automatically reorganizing routes | |`:tidy-threshold`|integer|similar routes more than this number will be grouped together | |`:uri?` |boolean|true if URI templates should be converted to matchers | |`:uri-key` |non-nil|the key to be used to look up the URI template in a route | |`:params-key` |non-nil|the key to put URI params under in the request map | |`:trailing-slash`|keyword|Trailing-slash action to perform on URIs - :add or :remove - nil (default) has no effect| |`:fallback-400?` |boolean|whether to add a fallback route to respond with HTTP status 400 for unmatched URIs | |`:show-uris-400?`|boolean|whether to add URI templates in the HTTP 400 response (see :fallback-400?) | |`:full-uri-key` |non-nil|the key to be used to populate full-uri for reporting HTTP 400 (see :show-uris-400?) | |`:uri-prefix-400`|string?|the URI prefix to use when showing URI templates in HTTP 400 (see :show-uris-400?) | |`:method?` |boolean|true if HTTP methods should be converted to matchers | |`:method-key` |non-nil|the key to be used to look up the method key/set in a route | |`:fallback-405?` |boolean|whether to add a fallback route to respond with HTTP status 405 for unmatched methods | |`:lift-uri?` |boolean|whether lift URI attributes from mixed routes and move the rest into nested routes | See: [[dispatch]], [[make-dispatcher]] (Clojure/JVM only), [[make-index]]
(conj-fallback-400 routes)
(conj-fallback-400 routes {:keys [show-uris? uri-finder uri-prefix] :as opts})
Given a route vector append a matcher that always matches, and a handler that returns HTTP 400 response.
Given a route vector append a matcher that always matches, and a handler that returns HTTP 400 response.
(conj-fallback-405 routes {:keys [allowed-methods method-finder] :as opts})
Given a route vector append a matcher that always matches, and a handler that returns HTTP 405 response.
Given a route vector append a matcher that always matches, and a handler that returns HTTP 405 response.
(conj-fallback-match routes handler)
Given a route vector append a matcher that always matches with a corresponding specified handler.
Given a route vector append a matcher that always matches with a corresponding specified handler.
(dispatch routes request)
(dispatch routes request f)
(dispatch routes request respond raise)
Given a vector of routes, recursively walk the routes evaluating the specified Ring request with each matcher.
Invoke corresponding handler on successful match.
Synopsis:
0. A route is a map {:matcher (fn [request]) -> request?
; :matcher is a required key
:nested vector of child routes ; either :handler or :nested key must be present
:handler Ring handler for route}
See: compile-routes
, make-dispatcher
(Clojure/JVM only)
Given a vector of routes, recursively walk the routes evaluating the specified Ring request with each matcher. Invoke corresponding handler on successful match. Synopsis: 0. A route is a map {:matcher `(fn [request]) -> request?` ; :matcher is a required key :nested vector of child routes ; either :handler or :nested key must be present :handler Ring handler for route} 1. A matcher is (fn [request]) that returns a potentially-updated request on successful match, nil otherwise. 2. Handler is a Ring handler (fn [request] [request respond raise]) that responds to a Ring request. See: [[compile-routes]], [[make-dispatcher]] (Clojure/JVM only)
(easy-routes routes uri-key method-key)
Allow easy, recursively applied, route definition as in the following examples:
Definition | Translated into |
---|---|
{"/docs/:id/info" [...]} | {:uri "/docs/:id/info" :nested [...]} |
{"/docs/:id/info" (fn [req])} | {:uri "/docs/:id/info" :handler (fn [req])} |
{:delete (fn [request])} | {:method :delete :handler (fn [request])} |
{["/docs/:id" :get] (fn [req])} | {:uri "/docs/:id" :method :get :handler (fn [req])} |
Allow easy, recursively applied, route definition as in the following examples: | Definition | Translated into | |------------------------------------|-------------------------------------------------------| |`{"/docs/:id/info" [...]}` |`{:uri "/docs/:id/info" :nested [...]}` | |`{"/docs/:id/info" (fn [req])}` |`{:uri "/docs/:id/info" :handler (fn [req])}` | |`{:delete (fn [request])}` |`{:method :delete :handler (fn [request])}` | |`{["/docs/:id" :get] (fn [req])}` |`{:uri "/docs/:id" :method :get :handler (fn [req])}`|
(ensure-matchex route matchex)
Given a route not containing the :matchex
key, assoc specified matchex into the route. If the route already
contains :matchex
then leave it intact.
Given a route not containing the `:matchex` key, assoc specified matchex into the route. If the route already contains `:matchex` then leave it intact.
(lift-key-middleware route lift-keys conflict-keys)
Given a route, lift keys and one or more conflict keys, if the route contains both any of the lift-keys and any of the conflict-keys then extract the lift keys such that all other attributes are moved into a nested route.
Given a route, lift keys and one or more conflict keys, if the route contains both any of the lift-keys and any of the conflict-keys then extract the lift keys such that all other attributes are moved into a nested route.
(make-dispatcher routes)
(make-dispatcher routes
{:keys [uri-key method-key]
:or {uri-key :uri method-key :method}
:as options})
Given a collection of routes return a Ring handler function that matches specified request and invokes
corresponding route handler on successful match, returns nil otherwise.
Synopsis:
0. A route is a map {:matcher (fn [request]) -> request?
; :matcher is a required key
:matchex (fn [request-sym]) -> matcher
; optional (enabled by default)
:nested routes-vector ; either :handler or :nested key must be present
:handler Ring handler}
See: compile-routes
, dispatch
Given a collection of routes return a Ring handler function that matches specified request and invokes corresponding route handler on successful match, returns nil otherwise. Synopsis: 0. A route is a map {:matcher `(fn [request]) -> request?` ; :matcher is a required key :matchex `(fn [request-sym]) -> matcher` ; optional (enabled by default) :nested routes-vector ; either :handler or :nested key must be present :handler Ring handler} 1. A matcher is (fn [request]) that returns a potentially-updated request on successful match, nil otherwise. 2. A matchex is (fn [request-sym]) that returns expression to eval instead of calling matcher. The matchex is used only when :matcher is also present. Expr should return a value similar to matcher. 3. A matchex is for optimization only, which may be disabled by setting a false or nil value for the :matchex key. 4. Handler is a Ring handler (fn [request] [request respond raise]) that responds to a Ring request. See: [[compile-routes]], [[dispatch]]
(make-ensurer k f)
Given a key and factory fn (accepts route and other args, returns new route), create a route updater fn that applies f to the route only when it does not contain the key.
Given a key and factory fn (accepts route and other args, returns new route), create a route updater fn that applies f to the route only when it does not contain the key.
(make-index routes)
(make-index routes options)
Given a collection of routes, index them returning a map {:id route-template}.
Options:
Kwarg | Description |
---|---|
:index-key | The index key in given routes, default :id |
:uri-key | The URI key in given routes, default :uri |
:method-key | HTTP method key in given routes, default :method |
Given a collection of routes, index them returning a map {:id route-template}. Options: | Kwarg | Description | |-------------|--------------------------------------------------| |`:index-key` |The index key in given routes, default `:id` | |`:uri-key` |The URI key in given routes, default `:uri` | |`:method-key`|HTTP method key in given routes, default `:method`| See: [[compile-routes]], [[template->request]]
(make-method-matcher route method-finder)
Given a route not containing the :matcher
key and containing HTTP-method keyword (or keyword set) as value
(found by method-finder), create a method matcher and add it under the :matcher
key. If the route already
contains the :matcher
key or if it does not contain HTTP-method keyword/set then the route is left intact. When
adding matcher also add matchex unless the :matchex
key already exists.
Given a route not containing the `:matcher` key and containing HTTP-method keyword (or keyword set) as value (found by method-finder), create a method matcher and add it under the `:matcher` key. If the route already contains the `:matcher` key or if it does not contain HTTP-method keyword/set then the route is left intact. When adding matcher also add matchex unless the `:matchex` key already exists.
(make-updater k f)
Given a key and updater fn (accepts route and other args, returns new route), create a route updater fn that applies f to the route only when it contains the key.
Given a key and updater fn (accepts route and other args, returns new route), create a route updater fn that applies f to the route only when it contains the key.
(make-uri-matcher route uri-finder params-key)
Given a route not containing the :matcher
key and containing URI-pattern string as value (found by uri-finder),
create a URI matcher and add it under the :matcher
key. If the route already contains the :matcher
key or if it
does not contain URI-pattern then the route is left intact. When adding matcher also add matchex unless the
:matchex
key already exists.
Given a route not containing the `:matcher` key and containing URI-pattern string as value (found by uri-finder), create a URI matcher and add it under the `:matcher` key. If the route already contains the `:matcher` key or if it does not contain URI-pattern then the route is left intact. When adding matcher also add matchex unless the `:matchex` key already exists.
(prewalk-routes routes parent-route f & args)
Given a bunch of routes, update every route (recursively) with f, which receives parent route as second arg.
Given a bunch of routes, update every route (recursively) with f, which receives parent route as second arg.
(realize-uri uri-template
{:keys [uri-params uri-prefix uri-suffix] :as options})
Given a vector of the form ['/users' :user-id '/profile/' :profile '/'] fill in the param values returning a URI.
See: template->request
Given a vector of the form ['/users' :user-id '/profile/' :profile '/'] fill in the param values returning a URI. See: [[template->request]]
(routes->wildcard-tidy routes)
(routes->wildcard-tidy routes
{:keys [tidy-threshold uri-key]
:or {tidy-threshold 1 uri-key :uri}
:as options})
Given a bunch of routes, segment them by prefix URI-tokens into a branched structure for faster match.
Given a bunch of routes, segment them by prefix URI-tokens into a branched structure for faster match.
(template->request request-template)
(template->request request-template
{:keys [uri-params uri-prefix uri-suffix] :as options})
Given a request template, realize the attributes to create a minimal Ring request.
A request template may be found from a reverse index built using make-index
.
Options:
Kwarg | Description |
---|---|
:uri-params | map of URI param values, e.g. {:lid 5 :rid 18} |
:uri-prefix | URI prefix string, e.g. "https://myapp.com/" |
:uri-suffix | URI suffix string, e.g. "?q=beer&country=in" |
See: make-index
Given a request template, realize the attributes to create a minimal Ring request. A request template may be found from a reverse index built using [[make-index]]. Options: | Kwarg | Description | |-------------|------------------------------------------------| |`:uri-params`|map of URI param values, e.g. `{:lid 5 :rid 18}`| |`:uri-prefix`|URI prefix string, e.g. `"https://myapp.com/"`| |`:uri-suffix`|URI suffix string, e.g. `"?q=beer&country=in"`| See: [[make-index]]
(trailing-slash-middleware route uri-key action)
Given a route, URI key and action (keyword :add
or :remove
) edit the URI to have or not have a trailing slash
if the route has a URI pattern. Leave the route unchanged if it has no URI pattern.
Given a route, URI key and action (keyword `:add` or `:remove`) edit the URI to have or not have a trailing slash if the route has a URI pattern. Leave the route unchanged if it has no URI pattern.
(update-each-route routes f & args)
Given a bunch of routes, update every route (recursively) with f.
Given a bunch of routes, update every route (recursively) with f.
(update-fallback-400 routes)
(update-fallback-400 routes uri-finder opts)
Update routes by appending a fallback HTTP-400 route only when all routes have :uri key.
Update routes by appending a fallback HTTP-400 route only when all routes have :uri key.
(update-fallback-405 routes method-finder)
(update-fallback-405 routes method-finder opts)
Update routes by appending a fallback HTTP-405 route when all routes have :method key.
Update routes by appending a fallback HTTP-405 route when all routes have :method key.
(update-in-each-route routes reference-key f)
Given a bunch of routes, update every route (recursively) containing specified attribute with the given wrapper. The wrapper fn f is invoked with the old attribute value, and the returned value is updated into the route.
Given a bunch of routes, update every route (recursively) containing specified attribute with the given wrapper. The wrapper fn f is invoked with the old attribute value, and the returned value is updated into the route.
(update-routes routes f & args)
Given a bunch of routes, update every route-collection (recursively) with f.
Given a bunch of routes, update every route-collection (recursively) with f.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close