Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking down the ring middleware scenario

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)))
like image 794
sova Avatar asked Feb 20 '26 07:02

sova


1 Answers

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 :-/

like image 139
Arthur Ulfeldt Avatar answered Feb 22 '26 01:02

Arthur Ulfeldt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!