So I have a simple webapp that has a main method that starts an http server. The dev setup that I want to achieve is basically something like using lein auto, but I want to stop and start my server and reload namespaces automatically on file change. This seems like something that should be straightforward and easy, but so far I haven't found any lein plugins or other ways to really achieve this.
Asked
Active
Viewed 374 times
1 Answers
3
I think what you're looking for is what I was looking for, a combination of tools.namespace and wrap-reload.
Here's what I came up with:
(ns your-project.core
(:require [clojure.tools.namespace.repl :as tn]
[org.httpkit.server :as http]
[ring.middleware.reload :refer [wrap-reload]]
[compojure.core :refer [defroutes GET]]
(defroutes create-app []
(GET "/" [] (fn [req] "hello world")
(defonce server (atom nil))
(defn start []
(let [app (create-app)]
(reset! server (http/run-server (wrap-reload app) {:port 3000}))
(println (str "Listening on port " 3000))))
(defn stop []
(when @server
(@server :timeout 100)
(reset! server nil)))
(defn restart []
(stop)
(tn/refresh :after 'your-project.core/start))

swlkr
- 210
- 5
- 11
-
Yep, this looks exactly like what I'm looking for. Thanks – user1622727 Apr 22 '17 at 12:46
-
1shouldnt you re-load it only in dev mode ? – danfromisrael Jan 01 '18 at 21:37
-
Yeah you’re right, I kind of expanded on this in my latest project, it’s far from perfect, but it attempts to distinguish between prod and dev https://github.com/swlkr/coast/blob/master/src/coast/server.clj – swlkr Jan 03 '18 at 05:30