Skip to content

Commit 5998876

Browse files
authored
Merge pull request #3 from grmble/master
async ring handlers for http-kit
2 parents f74af5d + fca3e7d commit 5998876

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/target
2+
pom.xml
3+
/.lein-*
4+
profiles.clj
5+
/.env
6+
.nrepl-port
7+
/.clj-kondo/.cache/
8+
/.lsp/.cache/
9+
/.calva/output-window/

project.clj

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
(defproject luminus-http-kit "0.1.9"
1+
(defproject luminus-http-kit "0.2.0-SNAPSHOT"
22
:description "HTTP Kit adapter for Luminus"
33
:url "https://github.com/luminus-framework/luminus-http-kit"
44
:license {:name "Eclipse Public License"
55
:url "http://www.eclipse.org/legal/epl-v10.html"}
6-
:dependencies [[org.clojure/clojure "1.10.1"]
7-
[org.clojure/tools.logging "0.4.0"]
8-
[http-kit "2.5.0"]])
6+
:plugins [[lein-ancient "1.0.0-RC3"]]
7+
:dependencies [[org.clojure/clojure "1.11.1"]
8+
[org.clojure/tools.logging "1.2.4"]
9+
[http-kit "2.6.0"]])

src/luminus/http_server.clj

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@
22
(:require [clojure.tools.logging :as log]
33
[org.httpkit.server :as http-kit]))
44

5-
(defn start [{:keys [handler host port] :as opts}]
5+
;; i know nothing about http-kit, but this blog post has a conversion function
6+
;; https://www.booleanknot.com/blog/2016/07/15/asynchronous-ring.html
7+
;; with-channel is deprecated though, so i rewrote it to use as-channel
8+
(defn async-ring->httpkit2 [handler]
9+
(fn [request]
10+
(http-kit/as-channel
11+
request
12+
{:on-open (fn [ch]
13+
(handler request
14+
#(http-kit/send! ch %)
15+
(fn [_] (http-kit/close ch))))})))
16+
17+
(defn start [{:keys [handler host port async?] :as opts}]
618
(try
719
(log/info "starting HTTP server on port" port)
820
(http-kit/run-server
9-
handler
21+
(if async?
22+
(async-ring->httpkit2 handler)
23+
handler)
1024
(-> opts
1125
(assoc :legacy-return-value? false)
1226
(dissoc :handler :init)))

0 commit comments

Comments
 (0)