Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to get a Jetty Server instance without starting it #73

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions ring-jetty-adapter/src/ring/adapter/jetty.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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?)
Expand All @@ -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))
Expand Down