-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathserver.clj
193 lines (180 loc) · 8.3 KB
/
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
(ns example.server
(:require [reitit.ring :as ring]
[reitit.http :as http]
[reitit.coercion.spec]
[reitit.swagger :as swagger]
[reitit.swagger-ui :as swagger-ui]
[reitit.openapi :as openapi]
[reitit.http.coercion :as coercion]
[reitit.dev.pretty :as pretty]
[reitit.interceptor.sieppari :as sieppari]
[reitit.http.interceptors.parameters :as parameters]
[reitit.http.interceptors.muuntaja :as muuntaja]
[reitit.http.interceptors.exception :as exception]
[reitit.http.interceptors.multipart :as multipart]
;; Uncomment to use
; [reitit.http.interceptors.dev :as dev]
; [reitit.http.spec :as spec]
; [spec-tools.spell :as spell]
[ring.adapter.jetty :as jetty]
[aleph.http :as aleph]
[muuntaja.core :as m]
[clojure.java.io :as io]
[clojure.spec.alpha :as s]
[spec-tools.core :as st]
[sieppari.async.manifold]
[manifold.deferred :as d]))
(s/def ::x int?)
(s/def ::y int?)
(s/def ::total pos-int?)
(s/def ::seed string?)
(s/def ::results
(st/spec
{:spec (s/and int? #(< 0 % 100))
:description "between 1-100"
:swagger/default 10
:reason "invalid number"}))
(def app
(http/ring-handler
(http/router
[["/swagger.json"
{:get {:no-doc true
:swagger {:info {:title "my-api"
:description "swagger-docs with reitit-http"
:version "0.0.1"}
;; used in /secure APIs below
:securityDefinitions {"auth" {:type :apiKey
:in :header
:name "Example-Api-Key"}}}
:handler (swagger/create-swagger-handler)}}]
["/openapi.json"
{:get {:no-doc true
:openapi {:info {:title "my-api"
:description "openapi3-docs with reitit-http"
:version "0.0.1"}
;; used in /secure APIs below
:components {:securitySchemes {"auth" {:type :apiKey
:in :header
:name "Example-Api-Key"}}}}
:handler (openapi/create-openapi-handler)}}]
["/files"
{:tags #{"files"}}
["/upload"
{:post {:summary "upload a file"
:parameters {:multipart {:file multipart/temp-file-part}}
:responses {200 {:body {:name string?, :size int?}}}
:handler (fn [{{{:keys [file]} :multipart} :parameters}]
{:status 200
:body {:name (:filename file)
:size (:size file)}})}}]
["/download"
{:get {:summary "downloads a file"
:swagger {:produces ["image/png"]}
:responses {200 {:description "an image"
:content {"image/png" {:schema any?}}}}
:handler (fn [_]
{:status 200
:headers {"Content-Type" "image/png"}
:body (io/input-stream
(io/resource "reitit.png"))})}}]]
["/async"
{:get {:tags #{"async"}
:summary "fetches random users asynchronously over the internet"
:parameters {:query (s/keys :req-un [::results] :opt-un [::seed])}
:responses {200 {:body any?}}
:handler (fn [{{{:keys [seed results]} :query} :parameters}]
(d/chain
(aleph/get
"https://randomuser.me/api/"
{:query-params {:seed seed, :results results}})
:body
(partial m/decode "application/json")
:results
(fn [results]
{:status 200
:body results})))}}]
["/math"
{:tags #{"math"}}
["/plus"
{:get {:summary "plus with data-spec query parameters"
:parameters {:query {:x int?, :y int?}}
:responses {200 {:body {:total pos-int?}}}
:handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200
:body {:total (+ x y)}})}
:post {:summary "plus with data-spec body parameters"
:parameters {:body {:x int?, :y int?}}
:responses {200 {:body {:total int?}}}
:handler (fn [{{{:keys [x y]} :body} :parameters}]
{:status 200
:body {:total (+ x y)}})}}]
["/minus"
{:get {:summary "minus with clojure.spec query parameters"
:parameters {:query (s/keys :req-un [::x ::y])}
:responses {200 {:body (s/keys :req-un [::total])}}
:handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200
:body {:total (- x y)}})}
:post {:summary "minus with clojure.spec body parameters"
:parameters {:body (s/keys :req-un [::x ::y])}
:responses {200 {:body (s/keys :req-un [::total])}}
:handler (fn [{{{:keys [x y]} :body} :parameters}]
{:status 200
:body {:total (- x y)}})}}]]
["/secure"
{:tags #{"secure"}
:openapi {:security [{"auth" []}]}
:swagger {:security [{"auth" []}]}}
["/get"
{:get {:summary "endpoint authenticated with a header"
:responses {200 {:body {:secret string?}}
401 {:body {:error string?}}}
:handler (fn [request]
;; In a real app authentication would be handled by middleware
(if (= "secret" (get-in request [:headers "example-api-key"]))
{:status 200
:body {:secret "I am a marmot"}}
{:status 401
:body {:error "unauthorized"}}))}}]]]
{;:reitit.interceptor/transform dev/print-context-diffs ;; pretty context diffs
;;:validate spec/validate ;; enable spec validation for route data
;;:reitit.spec/wrap spell/closed ;; strict top-level validation
:exception pretty/exception
:data {:coercion reitit.coercion.spec/coercion
:muuntaja m/instance
:interceptors [;; swagger feature
swagger/swagger-feature
;; openapi feature
openapi/openapi-feature
;; query-params & form-params
(parameters/parameters-interceptor)
;; content-negotiation
(muuntaja/format-negotiate-interceptor)
;; encoding response body
(muuntaja/format-response-interceptor)
;; exception handling
(exception/exception-interceptor)
;; decoding request body
(muuntaja/format-request-interceptor)
;; coercing response bodys
(coercion/coerce-response-interceptor)
;; coercing request parameters
(coercion/coerce-request-interceptor)
;; multipart
(multipart/multipart-interceptor)]}})
(ring/routes
(swagger-ui/create-swagger-ui-handler
{:path "/"
:config {:validatorUrl nil
:urls [{:name "swagger", :url "swagger.json"}
{:name "openapi", :url "openapi.json"}]
:urls.primaryName "openapi"
:operationsSorter "alpha"}})
(ring/create-default-handler))
{:executor sieppari/executor}))
(defn start []
(jetty/run-jetty #'app {:port 3000, :join? false, :async true})
;(aleph/start-server (aleph/wrap-ring-async-handler #'app) {:port 3000})
(println "server running in port 3000"))
(comment
(start))