tourist/middleware
Pre/post middleware types and chain runners.
Since: v0.1.0 (use!); v0.2.0 (use-after!); v0.3.0 (use-around!)
use!
(use! [handler : (fn [Ctx])
register a middleware function.
| handler | (fn [Ctx] (Option Response)) -- may capture: | |
| (none) to continue, (some resp) to short-circuit |
:int -- middleware handle; consumed by tourist
(use! (fn [ctx]
(println (req-path ctx))
(none)))
Since: v0.2.0 (Ctx-based signature; req-based shape was v0.1.0)
mw-item?
(mw-item? [item : Item] :)
true if an item handle is a middleware, not a route.
| item | :int item handle (from use!, get!, post!, etc.) |
:bool
Since: v0.1.0
mw-call
(mw-call [mw : Item ctx : Ctx] :)
invoke a middleware on a request.
| mw | middleware Item from use! | |
| ctx | tourist ctx |
(Option Response) -- none to continue, some(resp) to short-circuit
Since: v0.1.0
mw-chain-run
(mw-chain-run [mws : ItemList ctx : Ctx] :)
run a cons list of middleware handles in order.
| mws | ItemList of middleware handles (null = empty) | |
| ctx | tourist ctx |
(Option Response) -- some(resp) from the first short-circuit, or none
(let [opt (mw-chain-run mw-list ctx)]
(when (some? opt) ...))
Since: v0.1.0
use-around!
(use-around! [handler : (fn [Ctx (fn [Ctx])
register a middleware that brackets the rest of the request.
| handler | (fn [Ctx (fn [Ctx] Response)] Response) -- may capture |
:int -- an item for tourist
(use-around! (fn [ctx next]
(let [t0 (now-ms)
resp (next ctx)]
(with-header resp "X-Elapsed-Ms" (int->cstr (- (now-ms) t0))))))
Since: v0.3.0
mwaround-item?
(mwaround-item? [item : Item] :)
true if an item handle is an around-middleware.
Since: v0.3.0
mwaround-call
(mwaround-call [mw : Item ctx : Ctx next : (fn [Ctx])
invoke one around-middleware with its `next`.
Since: v0.3.0
use-after!
(use-after! [handler : (fn [Ctx Response])
register a response-decoration middleware.
| handler | (fn [Ctx Response] Response) -- may capture; receives ctx + current | |
| response; returns the same handle or a replacement |
:int -- after-middleware handle; consumed by tourist
(use-after! (fn [ctx resp]
(ctx-add-header! ctx "X-Request-Id" "abc-123")
resp))
Since: v0.2.0
mwafter-item?
(mwafter-item? [item : Item] :)
true if an item handle is an after-middleware.
Since: v0.2.0
mwafter-call
(mwafter-call [mw : Item ctx : Ctx resp : int] :)
invoke a single after-middleware on (ctx, resp).
Since: v0.2.0
mwafter-chain-run
(mwafter-chain-run [mws : ItemList ctx : Ctx resp : int] :)
thread resp through the after-mw cons list.
| mws | :int cons list of after-middleware handles (0 = empty) | |
| ctx | :int tourist ctx | |
| resp | :int current response handle |
:int -- the final response handle after every after-mw has run.
Since: v0.2.0
Internal definitions
__mw-cons-head-- head Item of an ItemList cell.__mw-cons-tail-- tail ItemList of an ItemList cell.__mw-fn->int__mwaround-fn->int__mwafter-fn->int