pedestal

simongray 2021-05-20T08:15:17.003300Z

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.

danieroux 2021-05-20T08:24:09.003800Z

(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))

simongray 2021-05-20T08:24:42.004100Z

I think that was it, @danie! Thanks.

😊 1
simongray 2021-05-20T08:26:01.004900Z

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?

simongray 2021-05-20T08:31:06.005900Z

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

danieroux 2021-05-20T08:31:31.006400Z

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

simongray 2021-05-20T08:35:10.006800Z

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

simongray 2021-05-20T08:36:48.007900Z

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

simongray 2021-05-20T08:38:00.008400Z

what does service-handler/service do?

simongray 2021-05-20T08:38:35.008900Z

that’s just the service map?

danieroux 2021-05-20T08:43:24.009600Z

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

danieroux 2021-05-20T08:43:30.009900Z

That may work?

danieroux 2021-05-20T08:45:07.010300Z

hmm, probably not.

simongray 2021-05-20T08:46:54.010600Z

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

simongray 2021-05-20T08:47:24.011200Z

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

simongray 2021-05-20T08:47:25.011400Z

thanks for you help!

simongray 2021-05-20T08:48:50.011900Z

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})

danieroux 2021-05-20T08:52:01.012200Z

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

danieroux 2021-05-20T08:53:07.013500Z

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!

simongray 2021-05-20T08:55:43.013700Z

Ok, gonna try that out

simongray 2021-05-20T08:55:47.014Z

thanks a lot for taking the time

simongray 2021-05-20T09:29:42.015200Z

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