Added ring.util.environment and PORT var
This commit is contained in:
parent
f30d11a55e
commit
2dd1cf1038
|
@ -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}]]
|
||||
(let [port (Integer. (*env* "PORT" "5000"))]
|
||||
(run-jetty
|
||||
handler
|
||||
(merge {:port 5000} options)))
|
||||
(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])
|
||||
(: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
|
||||
(with-env {"PORT" "4563"}
|
||||
(let [handler (constantly (response "Hello World"))]
|
||||
(with-server (serve handler {:join? false})
|
||||
(let [resp (http/get "http://localhost:5000")]
|
||||
(let [resp (http/get "http://localhost:4563")]
|
||||
(is (= (:status resp) 200))
|
||||
(is (= (:body resp) "Hello World"))))))
|
||||
(is (= (:body resp) "Hello World")))))))
|
||||
|
|
Reference in a new issue