Added ring.util.environment and PORT var

This commit is contained in:
James Reeves 2012-02-08 21:33:10 +00:00
parent f30d11a55e
commit 2dd1cf1038
3 changed files with 25 additions and 9 deletions

View file

@ -1,9 +1,12 @@
(ns ring.server.standalone
(:use [ring.adapter.jetty :only (run-jetty)]))
"Functions to start a standalone Ring server."
(:use ring.adapter.jetty
ring.util.environment))
(defn serve
"Start a web server to run a handler."
[handler & [{:as options}]]
(run-jetty
handler
(merge {:port 5000} options)))
(let [port (Integer. (*env* "PORT" "5000"))]
(run-jetty
handler
(merge {:port port} options))))

View file

@ -0,0 +1,11 @@
(ns ring.util.environment
"A namespace managing and reading environment variables.")
(def ^{:dynamic true, :doc "A map of environment variables."}
*env* (into {} (System/getenv)))
(defmacro with-env
"Merges the supplied map of environment variable into *env*."
[env-map & body]
`(binding [*env* (merge *env* ~env-map)]
~@body))

View file

@ -2,6 +2,7 @@
(:require [clj-http.client :as http])
(:use clojure.test
ring.server.standalone
ring.util.environment
ring.util.response))
(defmacro with-server [server & body]
@ -11,8 +12,9 @@
(finally (.stop server#)))))
(deftest serve-test
(let [handler (constantly (response "Hello World"))]
(with-server (serve handler {:join? false})
(let [resp (http/get "http://localhost:5000")]
(is (= (:status resp) 200))
(is (= (:body resp) "Hello World"))))))
(with-env {"PORT" "4563"}
(let [handler (constantly (response "Hello World"))]
(with-server (serve handler {:join? false})
(let [resp (http/get "http://localhost:4563")]
(is (= (:status resp) 200))
(is (= (:body resp) "Hello World")))))))