Skip to content

Commit

Permalink
refactored server handler with ServerContextHandler protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
onionpancakes committed Jul 19, 2024
1 parent 1f91fd1 commit c4839cb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,3 @@
(doseq [h (:handlers config)]
(.insertHandler handler h)))
handler))

(defn as-servlet-context-handler
{:tag ServletContextHandler}
[this]
(if (map? this)
(servlet-context-handler this)
(servlet-context-handler {:routes [["/*" this]]})))
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,43 @@
[org.eclipse.jetty.util.thread ThreadPool]))

(defprotocol ServerHandler
(^Handler as-server-handler [this]))

(def ^:dynamic *default-as-context-handler*
'dev.onionpancakes.serval.jetty.impl.ee10.servlet/as-servlet-context-handler)

(defn as-context-handler
{:tag ContextHandler}
[this]
(if (instance? ContextHandler this)
this
(let [as-context-handler-fn (requiring-resolve *default-as-context-handler*)]
(as-context-handler-fn this))))

(defn make-handler-from-context-route
[[context-path handler :as this]]
{:pre [(== (count this) 2)
(string? context-path)]}
(doto (as-context-handler handler)
(.setContextPath context-path)))

(defn make-handler-from-context-routes
[context-routes]
(let [handlers (mapv make-handler-from-context-route context-routes)]
(impl.handlers/context-handler-collection {:handlers handlers})))
(^Handler as-handler [this]))

(defprotocol ServerContextHandler
(^ContextHandler as-context-handler [this]))

(def ^:dynamic *default-context-handler-fn*
'dev.onionpancakes.serval.jetty.impl.ee10.servlet/servlet-context-handler)

(extend-protocol ServerContextHandler
clojure.lang.IPersistentVector
(as-context-handler [[path handler :as this]]
(when-not (== (count this) 2)
(throw (IllegalArgumentException. "Context route vector must be [path handler] pair.")))
(doto (as-context-handler handler)
(.setContextPath path)))
clojure.lang.IPersistentMap
(as-context-handler [this]
(let [context-handler-fn (requiring-resolve *default-context-handler-fn*)]
(context-handler-fn this)))
ContextHandler
(as-context-handler [this] this)
Object
(as-context-handler [this]
(as-context-handler {:routes [["/*" this]]})))

(extend-protocol ServerHandler
clojure.lang.IPersistentVector
(as-server-handler [this]
(make-handler-from-context-routes this))
(as-handler [this]
(let [handlers (mapv as-context-handler this)]
(impl.handlers/context-handler-collection {:handlers handlers})))
clojure.lang.IPersistentMap
(as-handler [this]
(as-context-handler this))
Handler
(as-server-handler [this] this)
(as-handler [this] this)
Object
(as-server-handler [this]
(as-handler [this]
(as-context-handler this))
nil
(as-server-handler [_] nil))
Expand Down Expand Up @@ -115,7 +119,7 @@
(into-array ServerConnector)
(.setConnectors server)))
(when (contains? config :handler)
(.setHandler server (as-server-handler (:handler config))))
(.setHandler server (as-handler (:handler config))))
(when (contains? config :request-log)
(.setRequestLog server (:request-log config)))
server)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,44 @@
(is (= (:status resp) 200))
(is (= (:body resp) "bar")))))

(deftest test-server-handler-context-routes
(with-config {:connectors [{:protocol :http :port 42000}]
:handler [["/foo" {:routes [["/*" (fn [_ _ response]
(srv/write-body response "foo"))]]}]
["/bar" {:routes [["/*" (fn [_ _ response]
(srv/write-body response "bar"))]]}]
["/baz" {:context-route "/foo"
:routes [["/*" (fn [_ _ response]
(srv/write-body response "baz"))]]}]]}
(let [resp (send "http://localhost:42000/foo")]
(is (= (:status resp) 200))
(is (= (:body resp) "foo")))
(let [resp (send "http://localhost:42000/bar")]
(is (= (:status resp) 200))
(is (= (:body resp) "bar")))
(let [resp (send "http://localhost:42000/baz")]
(is (= (:status resp) 200))
(is (= (:body resp) "baz")))))

(deftest test-server-handler-multiple-handlers-mixed
(with-config {:connectors [{:protocol :http :port 42000}]
:handler [(fn [_ _ response]
(srv/write-body response "foo"))
{:context-path "/bar"
:routes [["/*" (fn [_ _ response]
(srv/write-body response "bar"))]]}
["/baz" {:routes [["/*" (fn [_ _ response]
(srv/write-body response "baz"))]]}]]}
(let [resp (send "http://localhost:42000")]
(is (= (:status resp) 200))
(is (= (:body resp) "foo")))
(let [resp (send "http://localhost:42000/bar")]
(is (= (:status resp) 200))
(is (= (:body resp) "bar")))
(let [resp (send "http://localhost:42000/baz")]
(is (= (:status resp) 200))
(is (= (:body resp) "baz")))))

(deftest test-queued-thread-pool
(let [config {:min-threads 1
:max-threads 2
Expand Down

0 comments on commit c4839cb

Please sign in to comment.