:middleware option replaced

:stacktraces? and :auto-reload? used instead.
This commit is contained in:
James Reeves 2012-05-24 10:27:31 +01:00
parent db54f5ede1
commit 205bf14aa5
2 changed files with 25 additions and 11 deletions

View file

@ -1,8 +1,6 @@
(ns ring.server.options (ns ring.server.options
"Functions to retrieve options and settings with sensible defaults" "Functions to retrieve options and settings with sensible defaults"
(:use ring.util.environment (:use ring.util.environment
ring.middleware.stacktrace
ring.middleware.reload
[clojure.core.incubator :only (-?>)])) [clojure.core.incubator :only (-?>)]))
(def dev-env? (def dev-env?
@ -22,8 +20,12 @@
[options] [options]
(:open-browser? options dev-env?)) (:open-browser? options dev-env?))
(defn middleware (defn auto-reload?
"True if the source files should be automatically reloaded."
[options] [options]
(or (:middleware options) (:auto-reload? options dev-env?))
(if dev-env?
[wrap-stacktrace wrap-reload]))) (defn stacktraces?
"True if stacktraces should be shown for exceptions raised by the handler."
[options]
(:stacktraces? options dev-env?))

View file

@ -2,6 +2,8 @@
"Functions to start a standalone Ring server." "Functions to start a standalone Ring server."
(:use ring.adapter.jetty (:use ring.adapter.jetty
ring.server.options ring.server.options
ring.middleware.stacktrace
ring.middleware.reload
[clojure.java.browse :only (browse-url)])) [clojure.java.browse :only (browse-url)]))
(defn- try-port (defn- try-port
@ -47,11 +49,20 @@
(try (.join server) (try (.join server)
(finally (if destroy (destroy)))))) (finally (if destroy (destroy))))))
(defn- add-stacktraces [handler options]
(if (stacktraces? options)
(wrap-stacktrace handler)
handler))
(defn- add-auto-reload [handler options]
(if (auto-reload? options)
(wrap-reload handler)
handler))
(defn- add-middleware [handler options] (defn- add-middleware [handler options]
(reduce (-> handler
(fn [h m] (m h)) (add-stacktraces options)
handler (add-auto-reload options)))
(middleware options)))
(defn serve (defn serve
"Start a web server to run a handler. Takes the following options: "Start a web server to run a handler. Takes the following options:
@ -60,7 +71,8 @@
:init - a function to run before the server starts :init - a function to run before the server starts
:destroy - a function to run after the server stops :destroy - a function to run after the server stops
:open-browser? - if true, open a web browser after the server starts :open-browser? - if true, open a web browser after the server starts
:middleware - a list of middleware functions to apply to the handler :stacktraces? - if true, display stacktraces when an exception is thrown
:auto-reload? - if true, automatically reload source files
If join? is false, a Server object is returned." If join? is false, a Server object is returned."
{:arglists '([handler] [handler options])} {:arglists '([handler] [handler options])}