|
1 | 1 | (ns com.vennbilling.system.server |
2 | 2 | (:require |
3 | | - [com.vennbilling.logging.interface :as log] |
4 | 3 | [integrant.core :as ig] |
5 | 4 | [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]) |
15 | 8 |
|
16 | 9 | (:import |
17 | 10 | [clojure.lang ExceptionInfo])) |
18 | 11 |
|
19 | 12 | (def ^:private ServerConfig |
20 | 13 | :map) |
21 | 14 |
|
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 | | - |
40 | 15 | (defn with-http-server |
41 | 16 | [routes] |
42 | 17 | {:system/server {:handler (ig/ref :http/handler) |
|
52 | 27 | [_ server-config] |
53 | 28 | (try |
54 | 29 | (let [valid (ma/coerce ServerConfig server-config) |
55 | | - handler (atom (delay (:handler valid))) |
| 30 | + handler (:handler valid) |
56 | 31 | undertow-opts (dissoc valid :handler)] |
57 | 32 |
|
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)}) |
63 | 34 |
|
64 | 35 | (catch ExceptionInfo e |
65 | 36 | (log/exception e) |
|
70 | 41 | (log/info "Stopping undertow server") |
71 | 42 | (.stop undertow)) |
72 | 43 |
|
73 | | -;; TODO These should probably live in a different component or namespace |
74 | 44 | (defmethod ig/init-key :http/handler |
75 | 45 | [_ {: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)) |
81 | 47 |
|
82 | 48 | (defmethod ig/init-key :http/router |
83 | 49 | [_ {:keys [routes]}] |
84 | | - (ring/router ["" router-config routes])) |
| 50 | + (http/new-ring-router routes)) |
0 commit comments