A bract.ring wrapper is a function (fn [ring-handler context & args]) -> ring-handler
to wrap the handler (like
a middleware) using information in the context. This namespace includes such wrappers and some helper functions.
A bract.ring _wrapper_ is a function `(fn [ring-handler context & args]) -> ring-handler` to wrap the handler (like a middleware) using information in the context. This namespace includes such wrappers and some helper functions.
(add-uri-trailing-slash request)
Given a request map, add any missing trailing slash to the URI and return updated request map.
Given a request map, add any missing trailing slash to the URI and return updated request map.
(after & body)
Create a function (fn [arg]) -> arg
that returns the supplied argument after evaluating body of code.
Create a function `(fn [arg]) -> arg` that returns the supplied argument after evaluating body of code.
(distributed-trace-wrapper handler context)
(distributed-trace-wrapper handler context options)
Parse distributed trace HTTP headers and populate request with well configured attributes.
Option | Config key | Default config value |
---|---|---|
:trace-id-header | "bract.ring.trace.trace.id.header" | "x-trace-id" |
:parent-id-header | "bract.ring.trace.parent.id.header" | "x-trace-parent-id" |
:trace-id-required? | "bract.ring.trace.trace.id.req.flag" | false |
:trace-id-validator | "bract.ring.trace.trace.id.valid.fn" | (constantly nil) |
:trace-id-request-key | "bract.ring.trace.trace.id.req.key" | :trace-id |
:span-id-request-key | "bract.ring.trace.span.id.req.key" | :span-id |
:parent-id-request-key | "bract.ring.trace.parent.id.req.key" | :parent-id |
Parse distributed trace HTTP headers and populate request with well configured attributes. | Option | Config key | Default config value | |------------------------|----------------------------------------|-----------------------| |`:trace-id-header` |`"bract.ring.trace.trace.id.header"` |`"x-trace-id"` | |`:parent-id-header` |`"bract.ring.trace.parent.id.header"` |`"x-trace-parent-id"`| |`:trace-id-required?` |`"bract.ring.trace.trace.id.req.flag"`|`false` | |`:trace-id-validator` |`"bract.ring.trace.trace.id.valid.fn"`|`(constantly nil)` | |`:trace-id-request-key` |`"bract.ring.trace.trace.id.req.key"` |`:trace-id` | |`:span-id-request-key` |`"bract.ring.trace.span.id.req.key"` |`:span-id` | |`:parent-id-request-key`|`"bract.ring.trace.parent.id.req.key"`|`:parent-id` |
(doafter expr & body)
Evaluate supplied expression and return it after evaluating body of code.
Evaluate supplied expression and return it after evaluating body of code.
(health-check-response health-check-fns
health-http-codes
body-encoder
content-type)
Return health-check response.
Return health-check response.
(health-check-wrapper handler context)
(health-check-wrapper handler context options)
Given optional URIs (default: /health
), body encoder (default: EDN) and content type (default: application/edn
),
wrap specified Ring handler such that it responds to application health query when health endpoint is requested.
Option | Config key | Default config value |
---|---|---|
:uris | "bract.ring.health.check.uris" | ["/health" "/health/"] |
:body-encoder | "bract.ring.health.body.encoder" | [[clojure.core/pr-str]] |
:content-type | "bract.ring.health.content.type" | "application/edn" |
:http-codes | "bract.ring.health.http.codes" | {:critical 503 :degraded 500 :healthy 200} |
Given optional URIs (default: `/health`), body encoder (default: EDN) and content type (default: `application/edn`), wrap specified Ring handler such that it responds to application health query when health endpoint is requested. | Option | Config key | Default config value | |---------------|------------------------------------|--------------------------------------------| |`:uris` |`"bract.ring.health.check.uris"` |`["/health" "/health/"]` | |`:body-encoder`|`"bract.ring.health.body.encoder"`|[[clojure.core/pr-str]] | |`:content-type`|`"bract.ring.health.content.type"`|`"application/edn"` | |`:http-codes` |`"bract.ring.health.http.codes"` |`{:critical 503 :degraded 500 :healthy 200}`|
(info-endpoint-wrapper handler context)
(info-endpoint-wrapper handler context options)
Given Ring handler and Bract context, wrap the handler such that info (default: /info
and /info/
) URIs lead to
returning a runtime info response.
Option | Config key | Default config value |
---|---|---|
:uris | "bract.ring.info.endpoint.uris" | ["/info" "/info/"] |
:body-encoder | "bract.ring.info.body.encoder" | [[clojure.core/pr-str]] |
:content-type | "bract.ring.info.body.content.type" | "application/edn" |
Given Ring handler and Bract context, wrap the handler such that info (default: `/info` and `/info/`) URIs lead to returning a runtime info response. | Option | Config key | Default config value | |---------------|---------------------------------------|------------------------| |`:uris` |`"bract.ring.info.endpoint.uris"` |`["/info" "/info/"]`| |`:body-encoder`|`"bract.ring.info.body.encoder"` |[[clojure.core/pr-str]] | |`:content-type`|`"bract.ring.info.body.content.type"`|`"application/edn"` |
(info-response info-gen-fns body-encoder content-type)
Return /info response.
Return /info response.
(make-uri-prefix-matcher uri-prefix strip-prefix? uri-backup? backup-key)
Given a URI prefix, a flag to decide whether to strip the prefix and a key (nil
=disabled) to backup the old URI to,
return a function (fn [request]) -> request?
returning updated request on successful prefix match, nil
otherwise.
Given a URI prefix, a flag to decide whether to strip the prefix and a key (`nil`=disabled) to backup the old URI to, return a function `(fn [request]) -> request?` returning updated request on successful prefix match, `nil` otherwise. See: [[uri-prefix-match-wrapper]]
(opt-or-config opt-key config-f)
Assuming 'options and 'context symbols are bound to respective arguments, extract the value of specified option falling back to config.
Assuming 'options and 'context symbols are bound to respective arguments, extract the value of specified option falling back to config.
(params-normalize-wrapper handler context)
(params-normalize-wrapper handler context options)
Normalize the result of ring.middleware.params/wrap-params
middleware (that is, the request map with :params
) by
transforming each request params value. The wrap-params
middleware extracts params as string, but multiple values
for same param are turned into a vector of string - this middleware turns all param values into vectors of string
and applies normalizer to that vector.
Option | Config key | Default config value |
---|---|---|
:normalizer | "bract.ring.params.normalize.function" | [[clojure.core/identity]] |
Without this middleware/wrapper:
{"foo" "bar"
"baz" ["quux" "corge"]}
After using this middleware, where normalizer is clojure.core/first
:
{"foo" "bar"
"baz" "quux"}
Normalize the result of `ring.middleware.params/wrap-params` middleware (that is, the request map with `:params`) by transforming each request params value. The `wrap-params` middleware extracts params as string, but multiple values for same param are turned into a vector of string - this middleware turns all param values into vectors of string and applies normalizer to that vector. | Option | Config key | Default config value | |-------------|------------------------------------------|--------------------------| |`:normalizer`|`"bract.ring.params.normalize.function"`|[[clojure.core/identity]] | Without this middleware/wrapper: ```edn {"foo" "bar" "baz" ["quux" "corge"]} ``` After using this middleware, where normalizer is `clojure.core/first`: ```edn {"foo" "bar" "baz" "quux"} ```
(ping-endpoint-wrapper handler context)
(ping-endpoint-wrapper handler context options)
Given Ring handler and Bract context, wrap the handler such that ping (default: /ping
and /ping/
) URIs lead to
returning a ping response.
Option | Config key | Default config value |
---|---|---|
:uris | "bract.ring.ping.endpoint.uris" | ["/ping" "/ping/"] |
:body | "bract.ring.ping.endpoint.body" | "pong" |
:content-type | "bract.ring.ping.content.type" | "text/plain" |
Given Ring handler and Bract context, wrap the handler such that ping (default: `/ping` and `/ping/`) URIs lead to returning a ping response. | Option | Config key | Default config value | |---------------|-----------------------------------|------------------------| |`:uris` |`"bract.ring.ping.endpoint.uris"`|`["/ping" "/ping/"]`| |`:body` |`"bract.ring.ping.endpoint.body"`|`"pong"` | |`:content-type`|`"bract.ring.ping.content.type"` |`"text/plain"` |
(remove-uri-trailing-slash request)
Given a request map, remove any trailing slash from the URI and return updated request map.
Given a request map, remove any trailing slash from the URI and return updated request map.
(request-update-wrapper handler context request-updater)
Given Ring handler, Bract context and request-updater function (fn [request]) -> request
, wrap the handler such
that the request is updated before the Ring handler is applied to it.
Given Ring handler, Bract context and request-updater function `(fn [request]) -> request`, wrap the handler such that the request is updated before the Ring handler is applied to it.
(traffic-drain-wrapper handler context)
(traffic-drain-wrapper handler context options)
Given a deref'able shutdown state and a boolean flag to respond with connection-close HTTP header, wrap specified Ring handler to respond with HTTP 503 (in order to drain current traffic) when the system is shutting down.
Option | Config key | Default config value |
---|---|---|
:conn-close? | "bract.ring.traffic.conn.close.flag" | true |
Given a deref'able shutdown state and a boolean flag to respond with connection-close HTTP header, wrap specified Ring handler to respond with HTTP 503 (in order to drain current traffic) when the system is shutting down. | Option | Config key | Default config value | |--------------|----------------------------------------|-----------------------| |`:conn-close?`|`"bract.ring.traffic.conn.close.flag"`| `true` |
(traffic-log-wrapper handler context)
Log traffic to the Ring handler.
Option | Value type | Description | Default |
---|---|---|---|
:request-logger | (fn [request]) | Request logger function | No-op |
:response-logger | (fn [request response ^double duration-millis]) | Response logger function | No-op |
:exception-logger | (fn [request exception ^double duration-millis]) | Exception logger function | No-op |
See: [[ring-mware/traffic-log-middleware]]
Log traffic to the Ring handler. | Option | Value type | Description | Default | |-------------------|--------------------------------------------------|---------------------------|---------| |`:request-logger` |`(fn [request])` | Request logger function | No-op | |`:response-logger` |`(fn [request response ^double duration-millis])` | Response logger function | No-op | |`:exception-logger`|`(fn [request exception ^double duration-millis])`| Exception logger function | No-op | See: [[ring-mware/traffic-log-middleware]]
(unexpected->500-wrapper handler context)
(unexpected->500-wrapper handler context options)
Wrap given Ring handler such that if it returns unexpected Ring response (invalid/malformed response or exception) then return HTTP 500 Ring response.
Option | Config key | Default config value |
---|---|---|
:on-bad-response | "bract.ring.unexpected.response.fn" | bract.ring.util/bad-response->500 |
:on-exception | "bract.ring.unexpected.exception.fn" | bract.ring.util/exception->500 |
Wrap given Ring handler such that if it returns unexpected Ring response (invalid/malformed response or exception) then return HTTP 500 Ring response. | Option | Config key | Default config value | |------------------|----------------------------------------|-------------------------------------| |`:on-bad-response`|`"bract.ring.unexpected.response.fn"` |[[bract.ring.util/bad-response->500]]| |`:on-exception` |`"bract.ring.unexpected.exception.fn"`|[[bract.ring.util/exception->500]] |
(uri-prefix-match-wrapper handler context)
(uri-prefix-match-wrapper handler context options)
Given a Ring handler, a Bract context, a URI prefix, a flag to decide whether to strip the prefix and a key (nil
implies no-backup) to backup the old URI to, return updated Ring handler that matches prefix and proceeds on success
or returns HTTP 400 on no match.
Option | Config key | Default config value |
---|---|---|
:uri-prefix | "bract.ring.uri.prefix.match.token" | (required config) |
:strip-prefix? | "bract.ring.uri.prefix.strip.flag" | true |
:backup-uri? | "bract.ring.uri.prefix.backup.flag" | true |
:backup-key | "bract.ring.uri.prefix.backup.key" | :original-uri |
Given a Ring handler, a Bract context, a URI prefix, a flag to decide whether to strip the prefix and a key (`nil` implies no-backup) to backup the old URI to, return updated Ring handler that matches prefix and proceeds on success or returns HTTP 400 on no match. | Option | Config key | Default config value | |----------------|---------------------------------------|----------------------| |`:uri-prefix` |`"bract.ring.uri.prefix.match.token"`| (required config) | |`:strip-prefix?`|`"bract.ring.uri.prefix.strip.flag"` | `true` | |`:backup-uri?` |`"bract.ring.uri.prefix.backup.flag"`| `true` | |`:backup-key` |`"bract.ring.uri.prefix.backup.key"` | `:original-uri` | See: [[make-uri-prefix-matcher]]
(uri-trailing-slash-wrapper handler context)
(uri-trailing-slash-wrapper handler context options)
Wrap handler such that the trailing slash is either added (if missing) or removed (if present) depending upon the specified optional action (:add/:remove).
Option | Config key | Default config value |
---|---|---|
:action | "bract.ring.uri.trailing.slash.action" | :remove |
Wrap handler such that the trailing slash is either added (if missing) or removed (if present) depending upon the specified optional action (:add/:remove). | Option | Config key | Default config value | |---------------|------------------------------------------|----------------------| |`:action` |`"bract.ring.uri.trailing.slash.action"`|`:remove` |
(when-wrapper-enabled pred handler context & body)
Given a predicate function, handler and context, evaluate body of code only when (pred config)
returns truthy,
return the handler otherwise. The config is extracted from context before applying the predicate to it.
Given a predicate function, handler and context, evaluate body of code only when `(pred config)` returns truthy, return the handler otherwise. The config is extracted from context before applying the predicate to it.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close