reitit/doc/http/interceptors.md

60 lines
2 KiB
Markdown
Raw Normal View History

2018-09-07 21:05:55 +00:00
# Interceptors
2018-02-02 12:48:28 +00:00
Reitit has also support for [interceptors](http://pedestal.io/reference/interceptors) as an alternative to using middleware. Basic interceptor handling is implemented in `reitit.interceptor` package. There is no interceptor executor shipped, but you can use libraries like [Pedestal Interceptor](https://github.com/pedestal/pedestal/tree/master/interceptor) or [Sieppari](https://github.com/metosin/sieppari) to execute the chains.
2018-02-02 12:48:28 +00:00
2018-09-02 16:40:25 +00:00
## Reitit-http
2018-02-02 12:48:28 +00:00
```clj
2025-10-24 12:52:47 +00:00
[metosin/reitit-http "0.9.2-rc1"]
2018-09-02 16:40:25 +00:00
```
A module for http-routing using interceptors instead of middleware. Builds on top of the [`reitit-ring`](../ring/ring.md) module having all the same features.
2018-02-02 12:48:28 +00:00
2018-09-08 07:42:01 +00:00
The differences:
2018-02-02 12:48:28 +00:00
* `:interceptors` key used in route data instead of `:middleware`
2018-12-30 14:53:15 +00:00
* `reitit.http/http-router` requires an extra option `:executor` of type `reitit.interceptor/Executor` to execute the interceptor chain
* optionally, a routing interceptor can be used - it enqueues the matched interceptors into the context. See `reitit.http/routing-interceptor` for details.
## Simple example
```clj
(require '[reitit.ring :as ring])
(require '[reitit.http :as http])
(require '[reitit.interceptor.sieppari :as sieppari])
(defn interceptor [number]
{:enter (fn [ctx] (update-in ctx [:request :number] (fnil + 0) number))})
(def app
(http/ring-handler
(http/router
["/api"
{:interceptors [(interceptor 1)]}
["/number"
{:interceptors [(interceptor 10)]
:get {:interceptors [(interceptor 100)]
:handler (fn [req]
{:status 200
:body (select-keys req [:number])})}}]])
;; the default handler
(ring/create-default-handler)
;; executor
{:executor sieppari/executor}))
(app {:request-method :get, :uri "/"})
; {:status 404, :body "", :headers {}}
(app {:request-method :get, :uri "/api/number"})
; {:status 200, :body {:number 111}}
```
2018-08-22 18:55:03 +00:00
2018-09-08 07:42:01 +00:00
## Why interceptors?
2018-08-22 18:55:03 +00:00
2018-09-08 07:42:01 +00:00
* https://quanttype.net/posts/2018-08-03-why-interceptors.html
* https://www.reddit.com/r/Clojure/comments/9csmty/why_interceptors/