Fork me on GitHub
#reitit
<
2023-04-10
>
rickheere03:04:52

I use integrant, among other thing it sets up a s3 cliënt. What is the "best" way to get the s3 cliënt into a handler function? Do I pass it down with closures? Do I attach it to the request with middleware? How do you deal with it?

valtteri08:04:43

Both are valid options. Personally I prefer passing dependencies down explicitly because then it’s very clear where the deps are coming from. I usually have a function that receives the dependencies from integrant and returns a reitit router. Here’s one example: https://github.com/lipas-liikuntapaikat/lipas/blob/master/webapp/src/clj/lipas/backend/handler.clj#L51 https://github.com/lipas-liikuntapaikat/lipas/blob/master/webapp/src/clj/lipas/backend/system.clj#L27

rickheere08:04:57

That looks great, thanks for the help!

dharrigan16:04:37

As an alternative, and as alluded to at the start, I perfer to stick my dependencies in an app-config that is injected into the requst via middleware:

dharrigan16:04:58

I then pull out the app-config from the request and anything within the map

dharrigan16:04:15

Both are ways of doing the same thing 😉

rickheere18:04:02

This is roughly how I had it before but I injected the clients separately, but al in one map is a good idea as well. Thanks!

theequalizer7314:04:51

Hi Friends, I’m migrating one API Rest from compojure-api to reitit and I have a question. I’ve been thinking about creating a new version of the API for reitit, refactoring the routes and adding swagger. My question is, can I run the old routes with compojure-api and the new ones with reitit at the same time?

DrLjótsson20:04:38

Yes you can! I have done the exact same thing.

DrLjótsson20:04:22

I put the reitit routes early in the ring middleware chain and check if they return a response. If not, I pass the request on to the next handler.

theequalizer7322:04:03

mmmm I’ll try that tmr, can I reach out to you?

theequalizer7313:04:35

Can you share a snippet?

DrLjótsson19:04:17

Very schematically it looks like this (haven't confirmed this actually works but I think it should)

(def routes
  ["reitit/routes/here"])

(def reitit-handler
  (reitit-ring/ring-handler
   (reitit-ring/router
    routes)))

(defn reitit-middleware
  [handler]
  (fn [request]
    (or (reitit-handler request)
        (handler request))))

(def app
  (-> your-compojure-routes-middleware
      reitit-middleware
      ;; other middleware
      ))

DrLjótsson20:04:12

Also check out reloading-ring-handler in the latest release of reitit.

theequalizer7321:04:28

Thanks, I’m going to take a look!

👍 2