Added ring.util.environment and PORT var
This commit is contained in:
parent
f30d11a55e
commit
2dd1cf1038
|
@ -1,9 +1,12 @@
|
||||||
(ns ring.server.standalone
|
(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
|
(defn serve
|
||||||
"Start a web server to run a handler."
|
"Start a web server to run a handler."
|
||||||
[handler & [{:as options}]]
|
[handler & [{:as options}]]
|
||||||
(run-jetty
|
(let [port (Integer. (*env* "PORT" "5000"))]
|
||||||
handler
|
(run-jetty
|
||||||
(merge {:port 5000} options)))
|
handler
|
||||||
|
(merge {:port port} options))))
|
||||||
|
|
11
src/ring/util/environment.clj
Normal file
11
src/ring/util/environment.clj
Normal 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))
|
|
@ -2,6 +2,7 @@
|
||||||
(:require [clj-http.client :as http])
|
(:require [clj-http.client :as http])
|
||||||
(:use clojure.test
|
(:use clojure.test
|
||||||
ring.server.standalone
|
ring.server.standalone
|
||||||
|
ring.util.environment
|
||||||
ring.util.response))
|
ring.util.response))
|
||||||
|
|
||||||
(defmacro with-server [server & body]
|
(defmacro with-server [server & body]
|
||||||
|
@ -11,8 +12,9 @@
|
||||||
(finally (.stop server#)))))
|
(finally (.stop server#)))))
|
||||||
|
|
||||||
(deftest serve-test
|
(deftest serve-test
|
||||||
(let [handler (constantly (response "Hello World"))]
|
(with-env {"PORT" "4563"}
|
||||||
(with-server (serve handler {:join? false})
|
(let [handler (constantly (response "Hello World"))]
|
||||||
(let [resp (http/get "http://localhost:5000")]
|
(with-server (serve handler {:join? false})
|
||||||
(is (= (:status resp) 200))
|
(let [resp (http/get "http://localhost:4563")]
|
||||||
(is (= (:body resp) "Hello World"))))))
|
(is (= (:status resp) 200))
|
||||||
|
(is (= (:body resp) "Hello World")))))))
|
||||||
|
|
Reference in a new issue