i'm trying to get more familiar with reitit, not doing that well.
in the following code, why does "/api" take an anonymous function, whereas "/admin" takes a vector within a vector?
(are they the same)?
(defn wrap [handler id]
(fn [request]
(update (handler request) :via (fnil conj '()) id)))
(def app
(ring/ring-handler
(ring/router
["/api" {:middleware [#(wrap % :api)]}
["/ping" handler]
["/admin" {:middleware [[wrap :admin]]}
["/db" {:middleware [[wrap :db]]
:delete {:middleware [[wrap :delete]]
:handler handler}}]]]
{:data {:middleware [[wrap :top]]}}) ;; all routes
(ring/create-default-handler)))
so i'm not entirely clear on what the double vector is doing. i see in the source of ring/router the example given is just {:middleware [wrap-whatever]} , in which case the extra vector is something to do with providing function arguments? It feels like novel syntax and it's not sinking in.
i see also there's something recursive happening, not thinking about that yet, have to start somewhere.
this code is from the blog article on reitit-ring. I couldn't read the preceeding article, as the link is dead.
Please read https://cljdoc.org/d/metosin/reitit/0.7.1/doc/ring/ring-router#middleware
ping @juhoteperi, link to blog dead? 🤔
it's the link from the words "first part" on this page https://www.metosin.fi/blog/reitit-ring i was referring to
further background info - I saw [[wrap-defaults site-defaults]], in ring-examples, but that also seemed to work when i removed the outer vector. I couldn't get request diffs to print between site-defaults middleware. I only saw the request and the final response. Does that require you to use data-driven-middleware? Are the reitit default middleware a replacement for ring-defaults? I didn't understand if that was the intention or if when you use reitit middleware instead you are assumed to be starting again at a lower level; revisit all assumptions & redesign stack from scratch, forgetting entirely about ring-defaults.
I've tried to add a ring-defaults stack using the :middleware key, just to get up and running, but none of the middleware seems to be being applied?
(def app
(ring/ring-handler
(ring/router
["/mail-settings" {:get show
:post update}]
{:reitit.middleware/transform print-request-diffs})
{:middleware
; this key takes a vector of:
; - [wrap-cookies wrap-params] normal middleware, w/o extra params beyond just handler
; - [[wrap-defaults site-defaults] wrap-params] middleware fn that takes extra params
; - data-driven middleware record or map
; - :blah keyword to look up in middleware registry
;[[wrap-defaults site-defaults]] ; style from reeves, in ring-examples.
[wrap-anti-forgery ; site-defaults made explicit
wrap-flash
[wrap-session {:flash true :cookie-attrs {:http-only true} :store default-session-store}]
wrap-keyword-params
wrap-nested-params
wrap-multipart-params
wrap-params
wrap-cookies
[wrap-resource {:resources "public"}]
wrap-content-type
[wrap-default-charset "utf-8"]
wrap-not-modified
[x/wrap-frame-options :sameorigin]
[x/wrap-content-type-options :nosniff]
wrap-forwarded-scheme
wrap-forwarded-remote-addr]}))
something very different is happening now i have moved this middleware stack as a router option within :data, instead of applying it at the outermost level on ring-handler, (which i chose guessing it might be the simplest choice). it seems to be working, still investigating.
hi, let’s keep this in a 🧵 please.
difference in default mw outside of reitit router means the mw is applied before any routing happens. e.g. for all requests
if you add the middleware into the router, they are applied after a route match.
so, if you have a mw, “if nothing matches, do x”, you should not use it inside a router.
IMO there should be a “batteries included” mw-stack (using ring-defaults) available to enable easier onboarding.