We can use the wrap-reload
from ring.middleware.reload
package. This reload the namespaces of modified files before each request.
;core.clj
(ns net.jsloop.server.core
(:use [compojure.core :only [defroutes GET]]
[compojure.handler :only [site]]
[ring.middleware.reload :only [wrap-reload]]
org.httpkit.server))
(defroutes app-routes
(GET "/" [] "hello world"))
(defn -main []
(run-server (wrap-reload (site #'app-routes)) {:port 8080})
(println "Server started."))
;project.clj
(defproject server "1.0.0"
:dependencies [[org.clojure/clojure "1.8.0"]
[ring "1.5.0"]
[compojure "1.5.1"]
[http-kit "2.2.0"]]
:main net.jsloop.server.core)
Now start the server using:
lein run
Go to http://localhost:8080 and we will see "hello world"
. Modify the message in the core.clj
, reload the browser page and we will see the new message. Note that we need to pass the route function as a var to wrap-reload using the #'
reader macro for this to work.