reitit

https://cljdoc.org/d/metosin/reitit/ https://github.com/metosin/reitit/
WonderLastking 2021-04-06T13:30:45.045100Z

Hello! Its time to add my question too, Can someone explain me how the compiled middlewares (:compile) work? For example, this web-app example https://github.com/PrestanceDesign/usermanager-reitit-integrant-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?

ikitommi 2021-04-06T13:38:56.046200Z

here’s the guide: https://cljdoc.org/d/metosin/reitit/0.5.12/doc/ring/compiling-middleware

ikitommi 2021-04-06T13:39:20.046500Z

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

ikitommi 2021-04-06T13:39:45.046700Z

hope this helps

WonderLastking 2021-04-06T14:42:17.047300Z

nice! Thanks 🙂

Jonas Svalin 2021-04-06T17:22:20.061400Z

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 Svalin 2021-04-07T07:50:37.066Z

Excellent, thanks for you help @ikitommi :thumbsup:

Jonas Svalin 2021-04-07T08:04:46.066200Z

Perfectly solved my problem, much appreciated

2021-04-06T17:22:58.062200Z

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 ...)
    wrap-logging
    wrap-uncaught-exception)
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 []
  ["/api"
   {:middleware [;; uncaught exceptions
                 middleware/wrap-uncaught-exception
                 ;; logging
                 middleware/wrap-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!

ikitommi 2021-04-06T18:26:45.062600Z

There is the Expand protocol you can override, see https://cljdoc.org/d/metosin/reitit/0.5.12/doc/basics/route-data#customizing-expansion

ikitommi 2021-04-06T18:33:32.063200Z

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.

2021-04-06T19:48:35.064600Z

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 ...)
    wrap-logging
    wrap-uncaught-exception)
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?