Fork me on GitHub
#pedestal
<
2021-05-20
>
simongray08:05:17

Someone here once showed me some trick to have the routes (and their interceptor chains) update more dynamically in the REPL when the underlying code changes, but I forgot what it was. I think it was some combination of characters like ’# and perhaps a few more lines of supporting code. Anybody know what I may be thinking of? Currently, I have to restart the service every time I want to see updated interceptor chains.

danieroux08:05:09

(defn- route-fn
  "Use the deref of the namespace to get hot-reloaded namespaces."
  []
  (route/expand-routes
    (deref #'your.ns/routes)))

(-> service-handler/service
    ; hot-reloaded-fn ftw!
    (assoc ::http/routes route-fn))

simongray08:05:42

I think that was it, @danie! Thanks.

😊 4
simongray08:05:01

One question though: My routes are generated inside a function taking a config map:

(defn routes
  [conf]
  (route/expand-routes
    (set/union (example-routes conf)
               (sp.routes/all conf))))
I’m not sure how to represent that. your.ns/routes in your example is just a var representing a data structure, right?

simongray08:05:06

I think it’s the deref that’s tripping me up

danieroux08:05:31

What is your ::http/routes? Because whatever that is, you can deref?

simongray08:05:10

(defn service-map
  [conf]
  {::http/routes (routes conf)
   ::http/type   :jetty
   ::http/port   8080})

simongray08:05:48

And then in my start I deref an atom containing the overall conf, pass that to the function creating the service map and pass the created service map to http/create-server

simongray08:05:00

what does service-handler/service do?

simongray08:05:35

that’s just the service map?

danieroux08:05:24

{::http/routes ((deref #'routes) conf)}

danieroux08:05:30

That may work?

danieroux08:05:07

hmm, probably not.

simongray08:05:54

Ended up doing this

(defn routes
  [conf]
  (route/expand-routes
    (set/union ((deref #'example-routes) conf)
               ((deref #'sp.routes/all) conf))))

(defn service-map
  [conf]
  {::http/routes #(routes conf)
   ::http/type   :jetty
   ::http/port   8080})
and it seems to work

simongray08:05:24

(reloading the config doesn’t need to be dynamic, just the code changes)

simongray08:05:25

thanks for you help!

simongray08:05:50

but this also seems to work, so I’m unsure what deref’ing gets me:

(defn routes
  [conf]
  (route/expand-routes
    (set/union (example-routes conf)
               (sp.routes/all conf))))

(defn service-map
  [conf]
  {::http/routes #(routes conf)
   ::http/type   :jetty
   ::http/port   8080})

danieroux08:05:01

{::http/routes #((deref #'routes) conf)}

danieroux08:05:07

That’s what I would have suggested. The deref so that it gets the latest installed routes in the ns, on every invocation. And if what you have works, it works!

simongray08:05:43

Ok, gonna try that out

simongray08:05:47

thanks a lot for taking the time

simongray09:05:42

Can confirm that #((deref #'routes) conf) works great! Vars continue to confuddle me 😛