Keep var with clj-reload
When writing ring servers it's common pass a var down as handler while
developing to make the code reload-able, e.g. #'handler. But when
using clj-reload the whole
name-space is "replaced" (as in: a new name-space is created, and the old
one will GC eventually).
So the handler var the server holds is still fine, but it is the old
version. While clj-reload suggests some ways to handle this
situation, I wondered how to re-resolve a var, but found no build-in
way. So the following function makes sure to resolve the current
"version" of the given var:
(defn re-ressolving-var [v] {:pre [(var? v)]} (let [{vns :ns vname :name} (meta v)] (ns-resolve (create-ns (.getName vns)) vname)))
And with reitit:
(let [dev-mode? (get opts :dev-mode false) rh (if dev-mode? (reitit.ring/reloading-ring-handler #(re-ressolving-var handler)) (handler))] (jetty/run-jetty rh opts))))