ring

pez 2021-03-27T14:18:57.064700Z

Total beginner question: In an example project I am testing I lose the REPL after having evaluated (run-jetty …). The server seems to work fine, but no more REPL for me. Anyone have a hint for me what I should check to see why it locks up like this?

dharrigan 2021-03-27T14:40:14.065200Z

Doesn't that block? If you're evaling from the repl, then the run-jetty will block the REPL thread

dharrigan 2021-03-27T14:40:30.065600Z

the normal thing to do is to (def server (run-jetty....)

dharrigan 2021-03-27T14:40:46.066Z

to store the server instance in a var which you can then close later

dharrigan 2021-03-27T14:41:20.066200Z

i.e., <https://github.com/dharrigan/strip-nils/blob/master/src/strip_nils/main.clj>

dharrigan 2021-03-27T14:41:25.066400Z

line 46

2021-03-27T14:48:56.066900Z

@pez there's a :join? option for run-jetty: https://ring-clojure.github.io/ring/ring.adapter.jetty.html

pez 2021-03-27T16:30:53.069700Z

Thanks! It was the :join? option I had missed. I took inspiration from the example @dharrigan provided and now have this:

(def ^:private server-ref (atom nil))

(defn start! []
  (if-let [server @server-ref]
    (log/warn "Server already running? (stop!) it first.")
    (do
      (api/init)
      (reset! server-ref
              (run-jetty api/app
                         {:port (Integer/valueOf
                                 (or (System/getenv "port")
                                     "6003")
                                 10)
                          :join? false})))))

(defn stop! []
  (if-let [server @server-ref]
    (do (api/destroy)
        (.stop server)
        (reset! server-ref nil))
    (log/warn "No server")))

(defn -main [&amp; _args]
  (start!))
Please let me know if it looks crazy to you. 😃