Added :init and :destroy options
This commit is contained in:
parent
78f86118d7
commit
fb01783cfb
|
@ -12,9 +12,16 @@
|
||||||
(try-port port run-server)
|
(try-port port run-server)
|
||||||
(throw ex))))))
|
(throw ex))))))
|
||||||
|
|
||||||
|
(defn- setup-callbacks [{:keys [init destroy]}]
|
||||||
|
(if init (init))
|
||||||
|
(if destroy
|
||||||
|
(. (Runtime/getRuntime)
|
||||||
|
(addShutdownHook (Thread. destroy)))))
|
||||||
|
|
||||||
(defn serve
|
(defn serve
|
||||||
"Start a web server to run a handler."
|
"Start a web server to run a handler."
|
||||||
[handler & [{:as options}]]
|
[handler & [{:as options}]]
|
||||||
|
(setup-callbacks options)
|
||||||
(try-port (port options)
|
(try-port (port options)
|
||||||
(fn [port]
|
(fn [port]
|
||||||
(run-jetty
|
(run-jetty
|
||||||
|
|
|
@ -24,15 +24,23 @@
|
||||||
(testing "default port"
|
(testing "default port"
|
||||||
(with-server (test-server)
|
(with-server (test-server)
|
||||||
(is-server-running-on-port 3000)))
|
(is-server-running-on-port 3000)))
|
||||||
|
|
||||||
(testing "fallback default ports"
|
(testing "fallback default ports"
|
||||||
(with-server (test-server)
|
(with-server (test-server)
|
||||||
(with-server (test-server)
|
(with-server (test-server)
|
||||||
(is-server-running-on-port 3000)
|
(is-server-running-on-port 3000)
|
||||||
(is-server-running-on-port 3001))))
|
(is-server-running-on-port 3001))))
|
||||||
|
|
||||||
(testing "PORT environment variable"
|
(testing "PORT environment variable"
|
||||||
(with-env {"PORT" "4563"}
|
(with-env {"PORT" "4563"}
|
||||||
(with-server (test-server)
|
(with-server (test-server)
|
||||||
(is-server-running-on-port 4563))))
|
(is-server-running-on-port 4563))))
|
||||||
|
|
||||||
(testing ":port option"
|
(testing ":port option"
|
||||||
(with-server (test-server {:port 5463})
|
(with-server (test-server {:port 5463})
|
||||||
(is-server-running-on-port 5463))))
|
(is-server-running-on-port 5463)))
|
||||||
|
|
||||||
|
(testing ":init option"
|
||||||
|
(let [ran-init? (atom false)]
|
||||||
|
(with-server (test-server {:init #(reset! ran-init? true)})
|
||||||
|
(is @ran-init?)))))
|
||||||
|
|
Reference in a new issue