Fork me on GitHub
#reitit
<
2020-09-12
>
amarjeet16:09:11

Hi, I have a doubt on the order of middleware application. Lets consider the following code (take from https://cljdoc.org/d/metosin/reitit/0.5.5/doc/ring/transforming-middleware-chain):

(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]}
In this case, the order seems to be: first [wrap 1] , then [wrap 2] (based on the response body on the last line). But, in my code, the effect is reversed. Seems like I am missing something. Any help?