From fb01783cfbedc3d08c2b0343acbf5f1efdc47cf7 Mon Sep 17 00:00:00 2001 From: James Reeves Date: Sat, 11 Feb 2012 00:45:11 +0000 Subject: [PATCH] Added :init and :destroy options --- src/ring/server/standalone.clj | 7 +++++++ test/ring/server/test/standalone.clj | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/ring/server/standalone.clj b/src/ring/server/standalone.clj index 56ee52f..0d2a3df 100644 --- a/src/ring/server/standalone.clj +++ b/src/ring/server/standalone.clj @@ -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 diff --git a/test/ring/server/test/standalone.clj b/test/ring/server/test/standalone.clj index 528f276..4a776b9 100644 --- a/test/ring/server/test/standalone.clj +++ b/test/ring/server/test/standalone.clj @@ -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?)))))