Skip to content

Commit

Permalink
added jetty adapter, fixed http-kit adapter key
Browse files Browse the repository at this point in the history
  • Loading branch information
yogthos committed Sep 27, 2024
1 parent 29650b7 commit a8bbae9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
2 changes: 2 additions & 0 deletions build/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
hato/hato {:mvn/version "1.0.0"}
;; kit-http-kit
http-kit/http-kit {:mvn/version "2.7.0"}
;; kit-jetty
ring/ring-jetty-adapter {:mvn/version "1.12.2"}
;; kit-metrics
clj-commons/iapetos {:mvn/version "0.1.13"}
io.prometheus/simpleclient {:mvn/version "0.16.0"}
Expand Down
1 change: 1 addition & 0 deletions build/kit/sync_lib_deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"kit-sql-hikari"
"kit-sql-migratus"
"kit-http-kit"
"kit-jetty"
"kit-undertow"
"kit-xtdb"
"lein-template"])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{"kit-core" "1.0.8"
"kit-http-kit" "1.0.4"
"kit-http-kit" "1.0.5"
"kit-jetty" "1.0.0"
"kit-xtdb" "1.0.4"
"kit-generator" "0.2.3"
"kit-hato" "1.0.4"
Expand Down
10 changes: 5 additions & 5 deletions libs/kit-http-kit/src/kit/edge/server/http_kit.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@
(log/info "HTTP server stopped")
result))

(defmethod ig/expand-key :server/http-kit
(defmethod ig/expand-key :server/http
[k config]
{k (merge {:port 3000
:host "0.0.0.0"}
config)})

(defmethod ig/init-key :server/http-kit
(defmethod ig/init-key :server/http
[_ opts]
(let [handler (atom (delay (:handler opts)))]
{:handler handler
:server (start (fn [req] (@@handler req)) (dissoc opts :handler))}))

(defmethod ig/halt-key! :server/http-kit
(defmethod ig/halt-key! :server/http
[{:keys [timeout]} {:keys [server]}]
(stop server timeout))

(defmethod ig/suspend-key! :server/http-kit
(defmethod ig/suspend-key! :server/http
[_ {:keys [handler]}]
(reset! handler (promise)))

(defmethod ig/resume-key :server/http-kit
(defmethod ig/resume-key :server/http
[k opts old-opts old-impl]
(if (= (dissoc opts :handler) (dissoc old-opts :handler))
(do (deliver @(:handler old-impl) (:handler opts))
Expand Down
3 changes: 3 additions & 0 deletions libs/kit-jetty/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{:paths ["src"]
:deps {integrant/integrant {:mvn/version "0.9.0"}
ring/ring-jetty-adapter {:mvn/version "1.12.2"}}}
50 changes: 50 additions & 0 deletions libs/kit-jetty/src/kit/edge/server/jetty.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(ns kit.edge.server.jetty
(:require
[integrant.core :as ig]
[clojure.tools.logging :as log]
[ring.adapter.jetty :as jetty]))

(defn start [handler {:keys [host port] :as opts}]
(try
(log/info "starting HTTP server on port" port)
(jetty/run-jetty
handler
(-> opts
(assoc :join? false)
(dissoc :handler :init)))
(catch Throwable t
(log/error t (str "server failed to start on" host "port" port))
(throw t))))

(defn stop [http-server]
(let [result @(future (.stop http-server))]
(log/info "HTTP server stopped")
result))

(defmethod ig/expand-key :server/http
[k config]
{k (merge {:port 3000
:host "0.0.0.0"}
config)})

(defmethod ig/init-key :server/http
[_ opts]
(let [handler (atom (delay (:handler opts)))]
{:handler handler
:server (start (fn [req] (@@handler req)) (dissoc opts :handler))}))

(defmethod ig/halt-key! :server/http
[_ {:keys [server]}]
(stop server))

(defmethod ig/suspend-key! :server/http
[_ {:keys [handler]}]
(reset! handler (promise)))

(defmethod ig/resume-key :server/http
[k opts old-opts old-impl]
(if (= (dissoc opts :handler) (dissoc old-opts :handler))
(do (deliver @(:handler old-impl) (:handler opts))
old-impl)
(do (ig/halt-key! k old-impl)
(ig/init-key k opts))))

0 comments on commit a8bbae9

Please sign in to comment.