Skip to content
Alexander Turok edited this page Feb 24, 2020 · 3 revisions

As far as route ordering, FYI:

(def my-routes
  (compojure.api.sweet/api
    :middleware [print-middleware-3 print-middleware-4]
    (compojure.api.sweet/GET "/1/hello" request
                             :middleware [print-middleware-5 print-middleware-6]
                             (ring.util.response/response "Hello, World!"))))

(def my-handler
  (-> my-routes
      print-middleware-2
      print-middleware-1))

(let [request (mock/request :get "/1/hello")]
  (my-handler request))

[1] Receiving Request 
[2] Receiving Request 
[3] Receiving Request
[4] Receiving Request
[5] Receiving Request
[6] Receiving Request
[6] Returning Response
[5] Returning Response
[4] Returning Response
[3] Returning Response
[2] Returning Response
[1] Returning Response
=> {:status 200, :headers {}, :body "Hello, World!"}

Three places to apply middleware:

  1. On a particular route (#5,6 in the above example). These get executed immediately after the handler + middleware injected by compojure.core.make-route;
  2. On the whole app level (#3,4 in the above example). These get executed after the route handler + compojure.core middleware + route-level middleware;
  3. On the whole app level (#1,2 in the above example). These get executed after the route handler + compojure.core middleware + route-level middleware + app-level middleware + the middleware injected by compojure.api.sweet/api.