3
votes

Ring is super sleek and has some pretty sensible defaults for middleware(s).

When I made a new app through leiningen (lein) I ended up with something like this in my router/handler

(def app
  (wrap-defaults app-routes site-defaults))

https://github.com/ring-clojure/ring-defaults

Now I want to add more middleware (cemerick/friend) so I can do things like authentication for logins.

So, how would I translate the above into something more reminiscent of the ring middleware "stack," like at the bottom of the page https://github.com/ring-clojure/ring-defaults/blob/master/src/ring/middleware/defaults.clj

(def app
  (-> handler  
    (wrap-anti-forgery)
    (wrap-flash)
    (wrap-session)
    (wrap-keyword-params)
    (wrap-resource)
    (wrap wrap-file)))
1

1 Answers

5
votes

because ring just uses function composition for middleware you can simply wrap friend around the call to wrap defaults:

(def app 
  (my-additional-middleware 
    (wrap-defaults app-routes site-defaults)
  arguments to my additional middleware))

or you can thread it (for instance when you have several middlewares):

(def app
  (-> (wrap-defaults app-routes site-defaults)
      (friend-stuff arg arg)
      (other-middleware arg arg arg))

Getting the order of the middleware right is still up to you :-/