-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_server.clj
174 lines (159 loc) · 6.72 KB
/
http_server.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
(ns common-clj.components.http-server.http-server
(:require [com.stuartsierra.component :as component]
[common-clj.coercion :refer [coerce] :as coercion]
[common-clj.components.config.protocol :as config.protocol]
[common-clj.components.http-server.protocol
:as
http-server.protocol
:refer
[HttpServer]]
[common-clj.json :refer [json->string]]
[common-clj.schemata.http-server :as schemata.http]
[io.pedestal.http :as http]
[io.pedestal.http.body-params :refer [body-params]]
[io.pedestal.interceptor :refer [interceptor]]
[io.pedestal.interceptor.error :as error-int]
[schema.core :as s]
[common-clj.lib.utils :refer [map-vals]]
[common-clj.humanize :as humanize]
[schema.utils :as s-utils])
(:import (java.io StringWriter PrintWriter)))
(def default-coercers coercion/default-coercers)
(defn ok [body]
{:status 200
:body body})
(defn wrap-handler [handler components]
(fn [request]
(let [{:keys [status body]} (handler request components)]
{:status status
:body body})))
(def content-type
(interceptor
{:name ::content-type
:leave (fn [context]
(assoc-in context
[:response :headers]
{"Content-Type" "application/json"}))}))
(defn body-coercer
[routes {:keys [override-coercers]}]
(interceptor
{:name ::json-coercer
:enter (fn [{:keys [request route] :as context}]
(let [{:keys [json-params]} request
{:keys [route-name]} route
{:keys [request-schema]} (route-name routes)
coerced-body (when request-schema
(coerce request-schema json-params (or override-coercers default-coercers)))]
(assoc-in context [:request :body] coerced-body)))
:leave (fn [{:keys [response route] :as context}]
(let [{:keys [body]} response
{:keys [route-name]} route
{:keys [response-schema]} (route-name routes)
coerced-body (json->string body)]
(s/validate response-schema body)
(assoc-in context [:response :body] coerced-body)))}))
(defn path-params-coercer
[routes {:keys [override-coercers]}]
(interceptor
{:name ::path-params-coercer
:enter (fn [{:keys [request route] :as context}]
(let [{:keys [path-params]} request
{:keys [route-name]} route
{:keys [path-params-schema]} (route-name routes)
coerced-path-params (when path-params-schema
(coerce path-params-schema path-params (or override-coercers default-coercers)))]
(if coerced-path-params
(assoc-in context [:request :path-params] coerced-path-params)
context)))}))
(defn query-params-coercer
[routes {:keys [override-coercers]}]
(interceptor
{:name ::query-params-coercer
:enter (fn [{:keys [request route] :as context}]
(let [{:keys [query-params]} request
{:keys [route-name]} route
{:keys [query-params-schema]} (route-name routes)
coerced-query-params (when query-params-schema
(coerce query-params-schema query-params (or override-coercers default-coercers)))]
(if coerced-query-params
(assoc-in context [:request :query-params] coerced-query-params)
context)))}))
(defn error-interceptor [{:keys [override-humanizer]} env]
(error-int/error-dispatch
[ctx ex]
[{:type :schema-tools.coerce/error}]
(let [values (->> ex ex-data :value)
humanizer (or override-humanizer humanize/humanize)
error-map (->> ex
ex-data
:error
(map-vals #(humanize/explain % humanizer))
(into {}))]
(assoc ctx :response {:status 400 :body (json->string {:error error-map})}))
:else
(let [sw (StringWriter.)
pw (PrintWriter. sw)
e (->> ex ex-data :exception)
_ (.printStackTrace e pw)
response (if (not= :prod env)
{:error "Internal Server Error"
:stack-trace (str sw)}
{:error "Internal Server Error"})]
(assoc ctx :response {:status 500 :body (json->string response)}))))
(defn interceptors [routes overrides env]
[content-type
(error-interceptor overrides env)
(body-params)
(body-coercer routes overrides)
(path-params-coercer routes overrides)
(query-params-coercer routes overrides)])
(s/defn routes->pedestal
[routes :- schemata.http/Routes
overrides :- (s/maybe schemata.http/Overrides)
env :- s/Keyword
components]
(into
#{}
(map
(fn [[route-name {:keys [path method handler]}]]
[path
method
(conj (interceptors routes overrides env) (wrap-handler handler components))
:route-name route-name]))
routes))
(defonce ^:private server (atom nil))
(s/defrecord HttpServerImpl [routes overrides]
component/Lifecycle
(start [{:keys [config] :as components}]
(let [env (config.protocol/get-env config)
pedestal-routes (routes->pedestal routes overrides env components)
service (http-server.protocol/create-server components)]
(println "env: " env)
(println "class env: " (class env))
(when (not= :test env)
(println "starting server")
(http/start service))
(reset! server service)
(-> components
(assoc :service service)
(assoc :pedestal-routes pedestal-routes))))
(stop [component]
(http/stop @server)
component)
HttpServer
(create-server [{:keys [config] :as components}]
(let [env (config.protocol/get-env config)
{:keys [http-port]} (config.protocol/get-config config)]
(http/create-server
{::http/routes (routes->pedestal routes overrides env components)
::http/allowed-origins {:creds true :allowed-origins (constantly true)}
::http/host "0.0.0.0"
::http/type :jetty
::http/port http-port
::http/join? false}))))
(s/defn new-http-server
([routes :- schemata.http/Routes]
(new-http-server routes nil))
([routes :- schemata.http/Routes
overrides :- (s/maybe schemata.http/Overrides)]
(map->HttpServerImpl {:routes routes :overrides overrides})))