Skip to content

Commit 6ff1c63

Browse files
committed
Move some impl from vertx.web to vertx.http (code sharing).
1 parent d5a97c7 commit 6ff1c63

5 files changed

Lines changed: 145 additions & 300 deletions

File tree

src/vertx/core.clj

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,6 @@
5555
[]
5656
(Vertx/currentContext))
5757

58-
;; (defn blocking
59-
;; ([prm] (blocking prm (current-context)))
60-
;; ([prm ctx]
61-
;; (assert (instance? Context ctx) "the first argument should be instance of Context")
62-
;; (let [d (p/deferred)
63-
;; bh (reify Handler
64-
;; (handle [this po]
65-
;; (p/finally prm (fn [v e]
66-
;; (if e
67-
;; (.fail po e)
68-
;; (.complete po v))))))
69-
;; rh (reify Handler
70-
;; (handle [this ar]
71-
;; (if (.succeeded ar)
72-
;; (p/resolve! d (.result ar))
73-
;; (p/reject! d (.cause ar)))))]
74-
75-
;; (.executeBlocking ^Context ctx
76-
;; ^Handler bh
77-
;; ^Handler rh)
78-
;; d)))
79-
8058
(defn handle-on-context
8159
"Attaches the context (current if not explicitly provided) to the
8260
promise execution chain."

src/vertx/http.clj

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
[promesa.core :as p]
1212
[vertx.util :as vu])
1313
(:import
14+
java.util.Map$Entry
15+
clojure.lang.MapEntry
1416
io.vertx.core.Vertx
1517
io.vertx.core.Verticle
1618
io.vertx.core.Handler
1719
io.vertx.core.Future
20+
io.vertx.core.MultiMap
1821
io.vertx.core.Context
22+
io.vertx.core.buffer.Buffer
1923
io.vertx.core.http.HttpServer
2024
io.vertx.core.http.HttpServerRequest
2125
io.vertx.core.http.HttpServerResponse
@@ -26,6 +30,36 @@
2630

2731
;; --- Public Api
2832

33+
(declare -handle-response)
34+
(declare -handle-body)
35+
36+
(defn ->headers
37+
[^HttpServerRequest request]
38+
(let [headers (.headers request)
39+
it (.iterator ^MultiMap headers)]
40+
(loop [m (transient {})]
41+
(if (.hasNext it)
42+
(let [^Map$Entry me (.next it)
43+
key (.toLowerCase (.getKey me))
44+
val (.getValue me)]
45+
(recur (assoc! m key val)))
46+
(persistent! m)))))
47+
48+
(defn- ->request
49+
[^HttpServerRequest request]
50+
{:method (-> request .rawMethod .toLowerCase keyword)
51+
:path (.path request)
52+
:headers (->headers request)
53+
::request request
54+
::response (.response request)})
55+
56+
(defn handler
57+
[vsm f]
58+
(reify Handler
59+
(handle [this request]
60+
(let [ctx (->request request)]
61+
(-handle-response (f ctx) ctx)))))
62+
2963
(s/def :vertx.http/handler fn?)
3064
(s/def :vertx.http/host string?)
3165
(s/def :vertx.http/port pos?)
@@ -60,15 +94,48 @@
6094
(when port (.setPort opts port))
6195
opts))
6296

63-
(defn- fn->handler
64-
[f]
65-
(reify Handler
66-
(handle [_ request]
67-
(f request))))
68-
6997
(defn- resolve-handler
7098
[handler]
7199
(cond
72-
(fn? handler) (fn->handler handler)
100+
(fn? handler) (vu/fn->handler handler)
73101
(instance? Handler handler) handler
74102
:else (throw (ex-info "invalid handler" {}))))
103+
104+
(defprotocol IAsyncResponse
105+
(-handle-response [_ _]))
106+
107+
(defprotocol IAsyncBody
108+
(-handle-body [_ _]))
109+
110+
(extend-protocol IAsyncResponse
111+
java.util.concurrent.CompletionStage
112+
(-handle-response [data ctx]
113+
(p/then' data #(-handle-response % ctx)))
114+
115+
clojure.lang.IPersistentMap
116+
(-handle-response [data ctx]
117+
(let [status (or (:status data) 200)
118+
body (:body data)
119+
res (::response ctx)]
120+
(.setStatusCode ^HttpServerResponse res status)
121+
(-handle-body body res))))
122+
123+
(extend-protocol IAsyncBody
124+
(Class/forName "[B")
125+
(-handle-body [data res]
126+
(.end ^HttpServerResponse res (Buffer/buffer data)))
127+
128+
Buffer
129+
(-handle-body [data res]
130+
(.end ^HttpServerResponse res ^Buffer data))
131+
132+
nil
133+
(-handle-body [data res]
134+
(.putHeader ^HttpServerResponse res "content-length" "0")
135+
(.end ^HttpServerResponse res))
136+
137+
String
138+
(-handle-body [data res]
139+
(let [length (count data)]
140+
(.putHeader ^HttpServerResponse res "content-length" (str length))
141+
(.end ^HttpServerResponse res data))))

src/vertx/util/transit.clj

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/vertx/web.clj

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
[promesa.core :as p]
1111
[sieppari.core :as sp]
1212
[reitit.core :as rt]
13-
[vertx.http :as vxh]
13+
[vertx.http :as vh]
1414
[vertx.util :as vu])
1515
(:import
1616
clojure.lang.Keyword
@@ -32,18 +32,13 @@
3232
io.vertx.ext.web.handler.ResponseTimeHandler
3333
io.vertx.ext.web.handler.LoggerHandler))
3434

35-
;; --- Constants & Declarations
36-
37-
(declare -handle-response)
38-
(declare -handle-body)
39-
4035
;; --- Public Api
4136

4237
(s/def ::wrap-handler
4338
(s/or :fn fn?
4439
:vec (s/every fn? :kind vector?)))
4540

46-
(defn- make-ctx
41+
(defn- ->request
4742
[^RoutingContext routing-context]
4843
(let [^HttpServerRequest request (.request ^RoutingContext routing-context)
4944
^HttpServerResponse response (.response ^RoutingContext routing-context)
@@ -114,56 +109,18 @@
114109
time-response? true}
115110
:as options}]
116111
(let [rtr (rt/router routes options)
117-
hdr #(router-handler rtr %)]
112+
f #(router-handler rtr %)]
118113
(fn [^Router router]
119114
(let [^Route route (.route router)]
120115
(when time-response? (.handler route (ResponseTimeHandler/create)))
121116
(when log-requests? (.handler route (LoggerHandler/create)))
122-
123117
(.handler route (doto (BodyHandler/create true)
124118
(.setDeleteUploadedFilesOnEnd delete-uploads?)
125119
(.setUploadsDirectory upload-dir)))
126120
(.handler route (reify Handler
127121
(handle [_ context]
128-
(let [ctx (make-ctx context)]
129-
(-> (p/do! (hdr ctx))
130-
(p/then' #(-handle-response % ctx))
131-
(p/catch #(do (prn %) (.fail (:context ctx) %)))))))))
122+
(let [req (->request context)]
123+
(-> (p/do! (f req))
124+
(p/then' #(vh/-handle-response % req))
125+
(p/catch #(.fail (::routing-context req) %))))))))
132126
router))))
133-
134-
;; --- Impl
135-
136-
(defprotocol IAsyncResponse
137-
(-handle-response [_ _]))
138-
139-
(extend-protocol IAsyncResponse
140-
clojure.lang.IPersistentMap
141-
(-handle-response [data ctx]
142-
(let [status (or (:status data) 200)
143-
body (:body data)
144-
res (::response ctx)]
145-
(.setStatusCode ^HttpServerResponse res status)
146-
(-handle-body body res))))
147-
148-
(defprotocol IAsyncBody
149-
(-handle-body [_ _]))
150-
151-
(extend-protocol IAsyncBody
152-
(Class/forName "[B")
153-
(-handle-body [data res]
154-
(.end ^HttpServerResponse res (Buffer/buffer data)))
155-
156-
Buffer
157-
(-handle-body [data res]
158-
(.end ^HttpServerResponse res ^Buffer data))
159-
160-
nil
161-
(-handle-body [data res]
162-
(.putHeader ^HttpServerResponse res "content-length" "0")
163-
(.end ^HttpServerResponse res))
164-
165-
String
166-
(-handle-body [data res]
167-
(let [length (count data)]
168-
(.putHeader ^HttpServerResponse res "content-length" (str length))
169-
(.end ^HttpServerResponse res data))))

0 commit comments

Comments
 (0)