diff --git a/ring-jetty-adapter/src/ring/adapter/jetty.clj b/ring-jetty-adapter/src/ring/adapter/jetty.clj index 1b3cc575..f94a9d81 100644 --- a/ring-jetty-adapter/src/ring/adapter/jetty.clj +++ b/ring-jetty-adapter/src/ring/adapter/jetty.clj @@ -20,6 +20,12 @@ (servlet/update-servlet-response response response-map) (.setHandled base-request true)))))) +(defn add-handler + "Adds a Ring handler to the provided Jetty Server instance." + [server handler] + (.setHandler server (proxy-handler handler)) + server) + (defn- ssl-context-factory "Creates a new SslContextFactory instance from a map of options." [options] @@ -58,14 +64,14 @@ (.addConnector server (ssl-connector options))) server)) -(defn ^Server run-jetty - "Start a Jetty webserver to serve the given handler according to the +(defn ^Server jetty-server + "Creates and returns a stopped Jetty Server instance according to the supplied options: + :handler - a Ring handler to serve :configurator - a function called with the Jetty Server instance :port - the port to listen on (defaults to 80) :host - the hostname to listen on - :join? - blocks the thread until server ends (defaults to true) :daemon? - use daemon threads (defaults to false) :ssl? - allow connections over HTTPS :ssl-port - the SSL port to listen on (defaults to 443, implies :ssl?) @@ -76,16 +82,39 @@ :max-threads - the maximum number of threads to use (default 50) :client-auth - SSL client certificate authenticate, may be set to :need, :want or :none (defaults to :none)" - [handler options] - (let [^Server s (create-server (dissoc options :configurator)) - ^QueuedThreadPool p (QueuedThreadPool. ^Integer (options :max-threads 50))] + [options] + (let [^Server s (create-server (dissoc options :handler :configurator)) + ^QueuedThreadPool p (QueuedThreadPool. ^Integer (options :max-threads 50)) + {:keys [handler]} options] (when (:daemon? options false) (.setDaemon p true)) - (doto s - (.setHandler (proxy-handler handler)) - (.setThreadPool p)) + (when handler + (add-handler s handler)) + (.setThreadPool s p) (when-let [configurator (:configurator options)] (configurator s)) + s)) + +(defn ^Server run-jetty + "Start a Jetty webserver to serve the given handler according to the + supplied options: + + :configurator - a function called with the Jetty Server instance + :port - the port to listen on (defaults to 80) + :host - the hostname to listen on + :join? - blocks the thread until server ends (defaults to true) + :daemon? - use daemon threads (defaults to false) + :ssl? - allow connections over HTTPS + :ssl-port - the SSL port to listen on (defaults to 443, implies :ssl?) + :keystore - the keystore to use for SSL connections + :key-password - the password to the keystore + :truststore - a truststore to use for SSL connections + :trust-password - the password to the truststore + :max-threads - the maximum number of threads to use (default 50) + :client-auth - SSL client certificate authenticate, may be set to :need, + :want or :none (defaults to :none)" + [handler options] + (let [^Server s (jetty-server (assoc options :handler handler))] (.start s) (when (:join? options true) (.join s))