Search code examples
clojurereloadcompojure

Reloading Clojure Code/Routes - Issue with using the symbol vs. var


I'm new to Clojure, and trying to get a few simple web routes set up. I want the routes to reload all associated code in development, but not in production.

I was only able to get this to work using the var's for the routes, not the actual symbols. Can someone explain if I'm doing this wrong? If not, why is the var required?

(def app-handler
  (let [formats [:json-kw :edn :yaml-kw :yaml-in-html :transit-json :transit-msgpack]
        wrapped-api (wrap-restful-format #'routes/api-routes :formats formats)
        combined-routes (compojure.core/routes wrapped-api #'routes/html-routes)
        with-defaults (wrap-defaults combined-routes api-defaults)]
    (if (is-dev?)
      ; Development
      (wrap-reload with-defaults)
      ; Production
      with-defaults)))

(Note #'routes/api-routes and #'routes/html-routes above).


Solution

  • In a manner described in more detail in another answer, the server ends up capturing your route functions when they are passed in, and if you provide the var, this will ensure that the server uses any updated definitions.

    This is considered the normal way to provide your route or handler function during development, so that you can see updated definitions without having to restart your web server process.