Add :browser-uri option

New option to append a path to the target URI when opening a browser.
This commit is contained in:
Joe Littlejohn 2013-01-28 11:41:54 +00:00
parent 8d4718af06
commit 34dcf6a481
3 changed files with 18 additions and 5 deletions

View file

@ -54,6 +54,11 @@ The following options are supported:
True if you want a browser to be opened to the server. Defaults to True if you want a browser to be opened to the server. Defaults to
true in development mode, false in production mode. true in development mode, false in production mode.
* `:browser-uri` -
A path to append to the target URL if opening a browser (default
none). The full URI will be constructed like:
`http://{host}:{port}{browser-uri}`
* `:stacktrace?` - * `:stacktrace?` -
True if you want a stacktrace to be displayed in the browser when True if you want a stacktrace to be displayed in the browser when
an exception is raised. Default to true in development, false in an exception is raised. Default to true in development, false in

View file

@ -1,7 +1,8 @@
(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
[clojure.core.incubator :only (-?>)])) [clojure.core.incubator :only (-?>)])
(:require [clojure.string :as str]))
(def dev-env? (def dev-env?
(not (*env* "LEIN_NO_DEV"))) (not (*env* "LEIN_NO_DEV")))
@ -20,6 +21,12 @@
[options] [options]
(:open-browser? options dev-env?)) (:open-browser? options dev-env?))
(defn browser-uri
"The path to browse to when opening a browser"
[options]
(-> (str "/" (:browser-uri options))
(str/replace #"^/+" "/")))
(defn auto-reload? (defn auto-reload?
"True if the source files should be automatically reloaded." "True if the source files should be automatically reloaded."
[options] [options]

View file

@ -34,9 +34,9 @@
(.getHost) (.getHost)
(or "localhost"))) (or "localhost")))
(defn- open-browser-to [server] (defn- open-browser-to [server options]
(browse-url (browse-url
(str "http://" (server-host server) ":" (server-port server)))) (str "http://" (server-host server) ":" (server-port server) (browser-uri options))))
(defmacro ^{:private true} in-thread (defmacro ^{:private true} in-thread
"Execute the body in a new thread and return the Thread object." "Execute the body in a new thread and return the Thread object."
@ -78,6 +78,7 @@
: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
:browser-uri - the path to browse to when opening a browser
:stacktraces? - if true, display stacktraces when an exception is thrown :stacktraces? - if true, display stacktraces when an exception is thrown
:auto-reload? - if true, automatically reload source files :auto-reload? - if true, automatically reload source files
:auto-refresh? - if true, automatically refresh browser when source changes :auto-refresh? - if true, automatically refresh browser when source changes
@ -99,7 +100,7 @@
thread (add-destroy-hook server destroy)] thread (add-destroy-hook server destroy)]
(println "Started server on port" (server-port server)) (println "Started server on port" (server-port server))
(if (open-browser? options) (if (open-browser? options)
(open-browser-to server)) (open-browser-to server options))
(if join? (if join?
(.join thread)) (.join thread))
server))))) server)))))