integrant

zackteo 2021-11-08T02:38:10.002500Z

Hello, when I am using integrant, I have a config.edn listing down all the configuration details. May I know what is best practice when my routes in my ring-server needs to reference some of these details?

zackteo 2021-11-08T07:41:58.004400Z

Maybe the part that would be helpful is knowing how to change more complicated ring routes into integrant components 🤔

zackteo 2021-11-08T08:53:41.008700Z

Like if I have

(def config
  {:adapter/jetty {:port 8080, :handler (ig/ref :handler/all)}
   :handler/greet1 {:name "Alice"}
   :handler/greet2 {:name "Bobby"}})
(defmethod ig/init-key :adapter/jetty [_ {:keys [handler] :as opts}]
  (jetty/run-jetty handler (-> opts (dissoc :handler) (assoc :join? false))))

(defmethod ig/init-key :handler/greet1 [_ {:keys [name]}]
  (fn [_] (resp/response (str "Hello " name))))

(defmethod ig/init-key :handler/greet2 [_ {:keys [name]}]
  (fn [_] (resp/response (str "Hello " name))))
I don't really know how to do a :handler/all for this with compojure

2021-11-08T08:55:36.009600Z

You need use a router for :handler/all. e.g. reitit, compojure, ataraxy

2021-11-08T08:58:43.010400Z

So something along these lines:

2021-11-08T08:58:46.010700Z

(require '[reitit.core :as r])

(defmethod ig/init-key :my/router
  [_ {:keys [routes]}]
  (r/router
   routes))

(def config
  {:adapter/jetty {:port 8080, :handler (ig/ref :my/router)}
   :my/router {:routes
               [["/greet1" (ig/ref :handler/greet1)]
                ["/greet2" (ig/ref :handler/greet2)]]}
   :handler/greet1 {:name "Alice"}
   :handler/greet2 {:name "Bobby"}})

2021-11-08T08:58:50.011Z

https://github.com/metosin/reitit

2021-11-08T08:59:30.011800Z

(Not tested)

zackteo 2021-11-08T09:00:24.013100Z

yeap supposing I use compojure. Typically it will be something like

(defroutes routes-handler
  (GET "/greet1" [] (greet1)
  (GET "/greet2" [] (greet2))
how might this be turned to integrant then 🤔

2021-11-08T09:01:15.013600Z

I'm not too familiar with compojure myself, but I assume it creates a var named routes-handler

2021-11-08T09:01:28.014Z

So you could create an ig/init-key that returns that var

2021-11-08T09:02:02.014500Z

Hmm

2021-11-08T09:02:22.015100Z

That might not work actually, since it's a macro and needs the handlers at compile time

2021-11-08T09:03:40.015600Z

Here is an article about compojure-api + integrant https://the-frey.github.io/2017/12/14/compojure-and-integrant

2021-11-08T09:03:44.015900Z

Not sure if that's the same thing

zackteo 2021-11-08T09:04:18.016900Z

Oh yeap just found that right before you replied but couldn't quite understand it since they left the compojure part out

2021-11-08T09:04:50.017500Z

Yeah doesn't seem to be very helpful 😅

zackteo 2021-11-08T09:05:06.018Z

Tho there's a sample project - https://github.com/the-frey/compojure-api-integrant/blob/master/src/compojure_api_integrant/handler.clj#L9 but I don't really understand where the api function (?) came from

2021-11-08T09:05:17.018300Z

I personally try to avoid macro based libs. But that's just me

zackteo 2021-11-08T09:06:30.018700Z

Right - hmmm

zackteo 2021-11-08T09:07:08.019400Z

I think the plan might be to move to reitit soon

zackteo 2021-11-08T09:07:45.020200Z

but guess trying to avoid doing both that and refactoring of integrant stuff concurrently

zackteo 2021-11-08T09:08:08.020500Z

but maybe that's the simpler way 🤔

2021-11-08T09:08:26.021100Z

You could first move to reitit, and then to integrant. If you want to do it in steps

2021-11-08T09:08:47.021500Z

You can also ask yourself if you really need to move at all 😄

zackteo 2021-11-08T09:11:34.024100Z

oh for the integrant case - am already using it but currently the routes aren't being handled by integrant to handle the dependencies, so a hack-ish way to copy the integrant state was previously used - am trying to fix that currently haha

zackteo 2021-11-08T09:12:44.025400Z

am not sure if im also slightly confused - because ^ is also to get integrant-repl is work correctly (and i read that integrrant and integrant-repl aren't exactly the same)

zackteo 2021-11-08T09:14:23.026100Z

also I realise that in the guide compojure being used is https://github.com/metosin/compojure-api and am currently using https://github.com/weavejester/compojure

2021-11-08T09:19:48.026900Z

Ok so those are two different things

2021-11-08T09:19:57.027100Z

😂

zackteo 2021-11-08T09:20:19.027800Z

yeap hahaha

2021-11-08T09:20:21.027900Z

integrant-repl is an extension on integrant. Not sure what you mean by "they are not the same"

zackteo 2021-11-08T09:21:19.028500Z

but think it is incomplete

2021-11-08T09:21:55.028700Z

Hmm

2021-11-08T09:22:35.029400Z

Well it just makes developing your app with integrant easier

2021-11-08T09:23:03.029700Z

It's also something you generally only want in development

zackteo 2021-11-08T09:25:09.029900Z

Right

zackteo 2021-11-08T09:26:20.030800Z

I guess for clearer context - in my project integrant-repl is already being used but running go doesn't setup the full system currently have run another function that copies integrant-repl.state/system into an atom. which the routes access using stuff like get-in to take values from

zackteo 2021-11-08T09:29:43.031900Z

But anyway thanks for your help @kevin.van.rooijen ! 😄 Maybe I'll start with the refactoring to reitit

2021-11-08T09:32:01.032200Z

You're welcome! And good luck!

2021-11-08T14:02:58.034400Z

I’m on my phone but I did this by putting the compojure “app” into my integrant state, and then any time I changed a route I (halt!) and (go) to restart the app

2021-11-08T14:03:16.035Z

When I’m on my computer I can show what I’ve done with actual code