Skip to content

Commit eec9c3d

Browse files
Move http-specific items out of system component
1 parent 59540d8 commit eec9c3d

File tree

12 files changed

+104
-57
lines changed

12 files changed

+104
-57
lines changed

components/http/deps.edn

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{:paths ["src" "resources"]
2+
:deps {luminus/ring-undertow-adapter {:mvn/version "1.3.1"}
3+
metosin/reitit-core {:mvn/version "0.7.0-alpha5"}
4+
metosin/reitit-middleware {:mvn/version "0.7.0-alpha5"}
5+
metosin/reitit-malli {:mvn/version "0.7.0-alpha5"}
6+
metosin/reitit-ring {:mvn/version "0.7.0-alpha5"}
7+
metosin/ring-http-response {:mvn/version "0.9.3"}}
8+
:aliases {:test {:extra-paths ["test"]
9+
:extra-deps {}}}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(ns com.vennbilling.http.interface
2+
(:require [com.vennbilling.http.undertow :as undertow]
3+
[com.vennbilling.http.ring :as ring]))
4+
5+
(defn serve
6+
"Starts up an HTTP server with a io.undertow config and ring handler. Returns an undertow server"
7+
[config handler]
8+
(undertow/serve config handler))
9+
10+
(defn new-ring-handler
11+
"Returns a ring handler function that wraps a ring router and can be used with an HTTP server."
12+
[router]
13+
(ring/new-handler router))
14+
15+
(defn new-ring-router
16+
"Returns a new ring router that can be used with an ring handler"
17+
([routes]
18+
(new-ring-router "" routes []))
19+
([base-path routes]
20+
(new-ring-router base-path routes []))
21+
([base-path routes middleware]
22+
(ring/new-router base-path routes middleware)))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(ns com.vennbilling.http.rest)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
(ns com.vennbilling.http.ring
2+
3+
(:require [muuntaja.core :as m]
4+
[reitit.coercion.malli :as malli]
5+
[reitit.ring :as ring]
6+
[reitit.ring.coercion :as coercion]
7+
[reitit.ring.middleware.exception :as exception]
8+
[reitit.ring.middleware.muuntaja :as muuntaja]
9+
[reitit.ring.middleware.parameters :as parameters]
10+
[ring.logger :as logger]))
11+
12+
(def ^:private default-middleware [;; query-params & form-params
13+
parameters/parameters-middleware
14+
;; content-negotiation
15+
muuntaja/format-negotiate-middleware
16+
;; encoding response body
17+
muuntaja/format-response-middleware
18+
;; exception handling
19+
exception/exception-middleware
20+
;; decoding request body
21+
muuntaja/format-request-middleware
22+
;; coercing response bodys
23+
coercion/coerce-response-middleware
24+
;; coercing request parameters
25+
coercion/coerce-request-middleware])
26+
27+
(def ^:private ring-router-config
28+
{:coercion malli/coercion
29+
:muuntaja m/instance})
30+
31+
(defn new-handler
32+
[router]
33+
(logger/wrap-with-logger
34+
(ring/ring-handler
35+
router
36+
(ring/create-default-handler))))
37+
38+
(defn new-router
39+
[base-path routes & middleware]
40+
(let [middlewares (vec (set (into default-middleware middleware)))
41+
config (assoc ring-router-config :middleware middlewares)]
42+
(ring/router [base-path config routes])))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(ns com.vennbilling.http.undertow
2+
(:require
3+
[ring.adapter.undertow :refer [run-undertow]]
4+
[com.vennbilling.logging.interface :as log]))
5+
6+
(defn serve
7+
[config handler]
8+
(let [handler-fn (atom (delay handler))]
9+
;; TODO: Dependency injection of any system/storage
10+
;; https://github.com/vennbilling/venn/issues/55
11+
(log/info "Starting undertow server")
12+
(run-undertow (fn [req] (@@handler-fn req)) config)))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(ns com.vennbilling.http.interface-test)

components/system/deps.edn

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
{:paths ["src" "resources"]
2-
:deps {;; configuration via edn files
3-
aero/aero {:mvn/version "1.1.6"}
2+
:deps {aero/aero {:mvn/version "1.1.6"}
43
integrant/integrant {:mvn/version "0.8.1"}
5-
6-
metosin/malli {:mvn/version "0.17.0"}
7-
8-
;; http
9-
luminus/ring-undertow-adapter {:mvn/version "1.3.1"}
10-
metosin/reitit-core {:mvn/version "0.7.0-alpha5"}
11-
metosin/reitit-middleware {:mvn/version "0.7.0-alpha5"}
12-
metosin/reitit-malli {:mvn/version "0.7.0-alpha5"}
13-
metosin/reitit-ring {:mvn/version "0.7.0-alpha5"}
14-
metosin/ring-http-response {:mvn/version "0.9.3"}}
4+
metosin/malli {:mvn/version "0.17.0"}}
155

166
:aliases {:test {:extra-paths ["test"]
177
:extra-deps {}}}}
Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,17 @@
11
(ns com.vennbilling.system.server
22
(:require
3-
[com.vennbilling.logging.interface :as log]
43
[integrant.core :as ig]
54
[malli.core :as ma]
6-
[muuntaja.core :as m]
7-
[reitit.coercion.malli :as malli]
8-
[reitit.ring :as ring]
9-
[reitit.ring.coercion :as coercion]
10-
[reitit.ring.middleware.exception :as exception]
11-
[reitit.ring.middleware.muuntaja :as muuntaja]
12-
[reitit.ring.middleware.parameters :as parameters]
13-
[ring.adapter.undertow :refer [run-undertow]]
14-
[ring.logger :as logger])
5+
6+
[com.vennbilling.logging.interface :as log]
7+
[com.vennbilling.http.interface :as http])
158

169
(:import
1710
[clojure.lang ExceptionInfo]))
1811

1912
(def ^:private ServerConfig
2013
:map)
2114

22-
(def ^:private router-config
23-
{:coercion malli/coercion
24-
:muuntaja m/instance
25-
:middleware [;; query-params & form-params
26-
parameters/parameters-middleware
27-
;; content-negotiation
28-
muuntaja/format-negotiate-middleware
29-
;; encoding response body
30-
muuntaja/format-response-middleware
31-
;; exception handling
32-
exception/exception-middleware
33-
;; decoding request body
34-
muuntaja/format-request-middleware
35-
;; coercing response bodys
36-
coercion/coerce-response-middleware
37-
;; coercing request parameters
38-
coercion/coerce-request-middleware]})
39-
4015
(defn with-http-server
4116
[routes]
4217
{:system/server {:handler (ig/ref :http/handler)
@@ -52,14 +27,10 @@
5227
[_ server-config]
5328
(try
5429
(let [valid (ma/coerce ServerConfig server-config)
55-
handler (atom (delay (:handler valid)))
30+
handler (:handler valid)
5631
undertow-opts (dissoc valid :handler)]
5732

58-
(log/info "Starting undertow server")
59-
60-
;; TODO: Dependency injection of localdb and serverdb
61-
;; https://github.com/vennbilling/venn/issues/55
62-
{:undertow (run-undertow (fn [req] (@@handler req)) undertow-opts)})
33+
{:undertow (http/serve undertow-opts handler)})
6334

6435
(catch ExceptionInfo e
6536
(log/exception e)
@@ -70,15 +41,10 @@
7041
(log/info "Stopping undertow server")
7142
(.stop undertow))
7243

73-
;; TODO These should probably live in a different component or namespace
7444
(defmethod ig/init-key :http/handler
7545
[_ {:keys [router]}]
76-
77-
(logger/wrap-with-logger
78-
(ring/ring-handler
79-
router
80-
(ring/create-default-handler))))
46+
(http/new-ring-handler router))
8147

8248
(defmethod ig/init-key :http/router
8349
[_ {:keys [routes]}]
84-
(ring/router ["" router-config routes]))
50+
(http/new-ring-router routes))

deps.edn

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
"--middleware" "[cider.nrepl/cider-middleware]"
1414
"-i"]}
1515

16-
:dev {:extra-deps {poly/spec {:local/root "components/spec"}
16+
:dev {:extra-deps {poly/customer {:local/root "components/customer"}
17+
poly/database {:local/root "components/database"}
1718
poly/healthcheck {:local/root "components/healthcheck"}
18-
poly/customer {:local/root "components/customer"}
19-
poly/system {:local/root "components/system"}
19+
poly/http {:local/root "components/http"}
2020
poly/logging {:local/root "components/logging"}
21-
poly/database {:local/root "components/database"}
21+
poly/spec {:local/root "components/spec"}
22+
poly/system {:local/root "components/system"}
2223

2324
poly/agent {:local/root "bases/agent"}
2425
poly/server {:local/root "bases/server"}

0 commit comments

Comments
 (0)