-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yogthos
committed
Nov 16, 2024
1 parent
73b774f
commit dce7e1d
Showing
3 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
libs/deps-template/resources/io/github/kit_clj/kit/versions.edn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |