Added :init and :destroy options

This commit is contained in:
James Reeves 2012-02-11 00:45:11 +00:00
parent 78f86118d7
commit fb01783cfb
2 changed files with 16 additions and 1 deletions

View file

@ -12,9 +12,16 @@
(try-port port run-server)
(throw ex))))))
(defn- setup-callbacks [{:keys [init destroy]}]
(if init (init))
(if destroy
(. (Runtime/getRuntime)
(addShutdownHook (Thread. destroy)))))
(defn serve
"Start a web server to run a handler."
[handler & [{:as options}]]
(setup-callbacks options)
(try-port (port options)
(fn [port]
(run-jetty

View file

@ -24,15 +24,23 @@
(testing "default port"
(with-server (test-server)
(is-server-running-on-port 3000)))
(testing "fallback default ports"
(with-server (test-server)
(with-server (test-server)
(is-server-running-on-port 3000)
(is-server-running-on-port 3001))))
(testing "PORT environment variable"
(with-env {"PORT" "4563"}
(with-server (test-server)
(is-server-running-on-port 4563))))
(testing ":port option"
(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?)))))