Fork me on GitHub

Hello! Its time to add my question too, Can someone explain me how the compiled middlewares (:compile) work? For example, this web-app example defines a middleware-db this way:

(def middleware-db
  {:name ::db
   :compile (fn [{:keys [db]} _]
              (fn [handler]
                (fn [req]
                  (handler (assoc req :db db)))))})
My question is which are the 2 arguments from the fn {:keys [db]} _ ] and where they come from?


• 1st arg: the full route data • 2nd arg: router options


hope this helps


nice! Thanks 🙂

Jonas Svalin17:04:20

In bidi you can define your route and handler separately and then have them mapped through the bidi/make-handler function, for example:

(def route 
   [["/" :index]
    ["/ping" :ping])

(def handlers
 {:index ...
  :ping ...})

(bidi/make-handler route handlers)
In reitit implementations I’ve only seen the handlers defined in-line in the route-definition, such as:
(def routes
  [["/" {:get {:handler ...}}]
   ["/ping" {:get {:handler ...}}]])
Can I do the same in reitit as the bidi-example out of the box somehow, or would I have to write the equivalent to the make-handler function myself? I’ve inspected the reitit-ring/ring-handler function signature, but if I understand it correctly it does not have an overload that would support this. Does this functionality exist somewhere else that I’m missing?

Jonas Svalin07:04:37

Excellent, thanks for you help @ikitommi :thumbsup:

Jonas Svalin08:04:46

Perfectly solved my problem, much appreciated


Hey team, a bit of a noob question. I am writing my own custom exception / logging middleware. I put them at the end of reitit/router, etc A: wrap at end

(-> (ring/ring-handler (ring/router ...)
But, here’s the problem: My expectation is, since the middleware is wrapping ring/ring-handler, all the parsing of parameters, etc, should happen before it gets to wrap-logging
(defn wrap-logging [handler]
  (fn [req] ...))
But, it does not, the request in wrap-logging does not have :parameters `:session`, etc However, if I instead move this middleware into the :middleware key: A: wrap in :middleware
(defn api-routes []
   {:middleware [;; uncaught exceptions
                 ;; logging
                 ;; query-params & form-params
Then it does have those keys. This is surprising to me. I would expect that both A and B should have had :session and :`parameters` Am sure I’m misunderstanding something. If you have thoughts would love to hear!


In the first A, the logging is applied before the router is invoked. In ring, the middleware chain in run in reverse order. Inside reitit router, mw is run in reverse-reverse order, e.g. in "right" order.


Thanks for jumping in @ikitommi Would you mind expanding a bit deeper, on how A is being applied before the router is invoked? If it’s

(-> (ring/ring-handler (ring/router ...)
Wouldn’t this imply that first ring/router is run, than ring-handler, then ring-logging, etc? (or does ring/router doesn’t quite invoke it yet?