Liking cljdoc? Tell your friends :D

Transforming the Middleware Chain

There is an extra option in ring-router (actually, in the underlying middleware-router): :reitit.middleware/transform to transform the middleware chain per endpoint. Value should be a function or a vector of functions that get a vector of compiled middleware and should return a new vector of middleware.

Example Application

(require '[reitit.ring :as ring])
(require '[reitit.middleware :as middleware])

(defn wrap [handler id]
  (fn [request]
    (handler (update request ::acc (fnil conj []) id))))

(defn handler [{::keys [acc]}]
  {:status 200, :body (conj acc :handler)})

(def app
  (ring/ring-handler
    (ring/router
      ["/api" {:middleware [[wrap 1] [wrap 2]]}
       ["/ping" {:get {:middleware [[wrap 3]]
                       :handler handler}}]])))

(app {:request-method :get, :uri "/api/ping"})
; {:status 200, :body [1 2 3 :handler]}

Reversing the Middleware Chain

(def app
  (ring/ring-handler
    (ring/router
      ["/api" {:middleware [[wrap 1] [wrap 2]]}
       ["/ping" {:get {:middleware [[wrap 3]]
                       :handler handler}}]]
      {::middleware/transform reverse})))

(app {:request-method :get, :uri "/api/ping"})
; {:status 200, :body [3 2 1 :handler]}

Interleaving Middleware

(def app
  (ring/ring-handler
    (ring/router
      ["/api" {:middleware [[wrap 1] [wrap 2]]}
       ["/ping" {:get {:middleware [[wrap 3]]
                       :handler handler}}]]
      {::middleware/transform #(interleave % (repeat [wrap :debug]))})))

(app {:request-method :get, :uri "/api/ping"})
; {:status 200, :body [1 :debug 2 :debug 3 :debug :handler]}

Printing Request Diffs

[metosin/reitit-middleware "0.5.2"]

Using reitit.ring.middleware.dev/print-request-diffs transformation, the request diffs between each middleware are printed out to the console. To use it, add the following router option:

:reitit.middleware/transform reitit.ring.middleware.dev/print-request-diffs

Sample output:

Ring Request Diff

Can you improve this documentation? These fine people already did:
Tommi Reiman, Miikka Koskinen & Alexander Kiel
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close