Skip to content

Commit

Permalink
added jdk-http server edge
Browse files Browse the repository at this point in the history
  • Loading branch information
Yogthos committed Nov 16, 2024
1 parent 73b774f commit dce7e1d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{"kit-core" "1.0.9"
"kit-http-kit" "1.0.5"
"kit-jetty" "1.0.0"
"kit-jdk-http" "1.0.0"
"kit-jetty" "1.0.0"
"kit-xtdb" "1.0.4"
"kit-generator" "0.2.3"
"kit-hato" "1.0.4"
Expand Down
3 changes: 3 additions & 0 deletions libs/kit-jdk-http/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"}
com.github.igrishaev/ring-jdk-adapter {:mvn/version "0.1.0"}}}
45 changes: 45 additions & 0 deletions libs/kit-jdk-http/src/kit/edge/server/jdk.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
(ns kit.edge.server.jdk
(:require
[integrant.core :as ig]
[clojure.tools.logging :as log]
[ring.adapter.jdk :as jdk]))

(defn start [handler {:keys [host port] :as opts}]
(try
(log/info "starting HTTP server on port" port)
(jdk/server handler (dissoc opts :handler :init))
(catch Throwable t
(log/error t (str "server failed to start on" host "port" port))
(throw t))))

(defn stop [http-server]
(jdk/stop http-server)
(log/info "HTTP server stopped"))

(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 dce7e1d

Please sign in to comment.