Saturday, October 15, 2016

Hot Code Reloading Using http-kit and Lein

You can use the wrap-reload from ring.middleware.reload package. This reload the namespaces of modified files before each request.
;core.clj
(ns com.qlambda.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 com.qlambda.server.core)
Now start the server using
lein run
Go to http://localhost:8080 and you will see "hello world". Modify the message in the core.clj, reload the browser page and you will see the new message. Note that the you need to pass the route function as a var to wrap-reload using the #' reader macro for this to work.