/
http.clj
183 lines (157 loc) · 6.03 KB
/
http.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
175
176
177
178
179
180
181
182
; Copyright 2013 Relevance, Inc.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0)
; which can be found in the file epl-v10.html at the root of this distribution.
;
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
;
; You must not remove this notice, or any other, from this software.
(ns io.pedestal.service.http
"Namespace which ties all the pedestal components together in a
sensible default way to make a full blown application."
(:require [io.pedestal.service.http.route :as route]
[io.pedestal.service.http.ring-middlewares :as middlewares]
[io.pedestal.service.interceptor :as interceptor]
[io.pedestal.service.http.servlet :as servlet]
[io.pedestal.service.http.impl.servlet-interceptor :as servlet-interceptor]
[io.pedestal.service.http.cors :as cors]
[ring.util.mime-type :as ring-mime]
[ring.util.response :as ring-response]
[clojure.string :as string]
[cheshire.core :as json]
[io.pedestal.service.log :as log])
(:import (java.io OutputStreamWriter)))
;; edn and json response formats
(defn- print-fn
[prn-fn]
(fn [output-stream]
(with-open [writer (OutputStreamWriter. output-stream)]
(binding [*out* writer]
(prn-fn))
(.flush writer))))
(defn- data-response
[f content-type]
(ring-response/content-type
(ring-response/response (print-fn f))
content-type))
(defn edn-response
"Return a Ring response that will print the given `obj` to the HTTP output stream in EDN format."
[obj]
(data-response #(pr obj) "application/edn;charset=UTF-8"))
(defn json-print
"Print object as JSON to *out*"
[obj]
(json/generate-stream obj *out*))
(defn json-response
"Return a Ring response that will print the given `obj` to the HTTP output stream in JSON format."
[obj]
(data-response #(json-print obj) "application/json;charset=UTF-8"))
;; interceptors
(interceptor/defon-request log-request
[request]
(log/info :msg (format "%s %s"
(string/upper-case (name (:request-method request)))
(:uri request)))
request)
(defn- response?
"A valid response is any map that includes an integer :status
value."
[resp]
(and (map? resp)
(integer? (:status resp))))
(interceptor/defafter not-found
"An interceptor that returns a 404 when routing failed to resolve a route."
[context]
(if-not (servlet-interceptor/response-sent? context)
(if-not (response? (:response context))
(assoc context :response (ring-response/not-found "Not Found"))
context)
context))
(interceptor/defon-response html-body
[response]
(let [body (:body response)
content-type (get-in response [:headers "Content-Type"])]
(if (and (string? body) (not content-type))
(ring-response/content-type response "text/html;charset=UTF-8")
response)))
(interceptor/defon-response json-body
[response]
(let [body (:body response)
content-type (get-in response [:headers "Content-Type"])]
(if (and (coll? body) (not content-type))
(-> response
(ring-response/content-type "application/json;charset=UTF-8")
(assoc :body (print-fn #(json-print body))))
response)))
(defn default-interceptors
[{routes ::routes
file-path ::file-path
resource-path ::resource-path
method-param-name ::method-param-name
allowed-origins ::allowed-origins
ext-mime-types ::mime-types
:or {file-path nil
resource-path "public"
method-param-name :_method
ext-mime-types {}}
:as service-map}]
(assoc service-map ::interceptors
(cond-> []
true (conj log-request)
(not (nil? allowed-origins)) (conj (cors/allow-origin allowed-origins))
true (conj not-found)
true (conj (middlewares/content-type {:mime-types ext-mime-types}))
true (conj route/query-params)
true (conj (route/method-param method-param-name))
(not (nil? resource-path)) (conj (middlewares/resource resource-path))
(not (nil? file-path)) (conj (middlewares/file file-path))
true (conj (route/router routes)))))
(defn dev-interceptors
[service-map]
(update-in service-map [::interceptors]
#(vec (->> %
(cons cors/dev-allow-origin)
(cons servlet-interceptor/exception-debug)))))
(defn service-fn
[{interceptors ::interceptors
:as service-map}]
(assoc service-map ::service-fn
(servlet-interceptor/http-interceptor-service-fn interceptors)))
(defn servlet
[{service-fn ::service-fn
:as service-map}]
(assoc service-map ::servlet
(servlet/servlet :service service-fn)))
(defn create-servlet
[{interceptors ::interceptors
:as options}]
(cond-> options
(nil? interceptors) default-interceptors
true service-fn
true servlet))
(defn- service-map->server-options
[service-map]
(let [server-keys [::host ::port ::join? ::jetty-options]]
(into {} (map (fn [[k v]] [(keyword (name k)) v]) (select-keys service-map server-keys)))))
(defn- server-map->service-map
[server-map]
(into {} (map (fn [[k v]] [(keyword "io.pedestal.service.http" (name k)) v]) server-map)))
(defn server
[{servlet ::servlet
type ::type
:or {type :jetty}
:as service-map}]
(let [server-ns (symbol (str "io.pedestal.service.http." (name type)))
server-fn (do (require server-ns)
(resolve (symbol (name server-ns) "server")))
server-map (server-fn servlet (service-map->server-options service-map))]
(merge service-map (server-map->service-map server-map))))
(defn create-server
[options]
(log/init-java-util-log)
(-> options
create-servlet
server))
(defn start [service-map] ((::start-fn service-map)))
(defn stop [service-map] ((::stop-fn service-map)))