-
Notifications
You must be signed in to change notification settings - Fork 0
/
articles.clj
250 lines (228 loc) · 12.1 KB
/
articles.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
(ns andrewslai.clj.http-api.articles
(:require [andrewslai.clj.api.articles :as articles-api]
[andrewslai.clj.api.authentication :as oidc]
[andrewslai.cljc.specs.articles] ;; Install specs
[camel-snake-kebab.core :as csk]
[camel-snake-kebab.extras :as cske]
[clojure.spec.alpha :as s]
[clojure.string :as string]
[compojure.api.meta :as compojure-meta]
[compojure.api.sweet :refer [context GET POST PUT]]
[ring.util.http-response :refer [not-found ok conflict]]
[taoensso.timbre :as log]))
(s/def ::message string?)
(s/def ::error-message (s/keys :req-un [::message]))
(defn remove-port
[hostname]
(first (string/split hostname #":")))
(comment
(string/split "localhost:5000" #":")
(string/split "localhost" #":")
)
(defn get-host
[request]
(-> request
(get-in [:headers "host"])
(remove-port)))
(defn ->article [article-url {:keys [body-params] :as request}]
(-> body-params
(select-keys [:article-name])
(assoc :article-url article-url
:hostname (get-host request)
:author (oidc/get-full-name (:identity request)))))
(defn ->commit [{:keys [body-params] :as request}]
(select-keys body-params [:branch-id :title :content :created-at :modified-at]))
(defmethod compojure-meta/restructure-param :swagger
[_ {request-spec :request :as swagger} acc]
(let [path (fn [spec] (str "#/components/schemas/" (name spec)))
ex-path (fn [spec] (str "#/components/examples/" (name spec)))
x (if request-spec
(-> swagger
(assoc :requestBody
{:content
{"application/json"
{:schema
{"$ref" (path request-spec)}
:examples
{(name request-spec) {"$ref" (ex-path request-spec)}}}}})
(assoc-in [:components :schemas (name request-spec)]
{:spec request-spec
:description "Automagically added"}))
swagger)]
(assoc-in acc [:info :public :swagger] x)))
(def articles-routes
;; HACK: I think the `context` macro may be broken, because it emits an s-exp
;; with let-bindings out of order: +compojure-api-request+ is referred to
;; before it is bound. This means that, to fix the bug, you'd need to either
;; reverse the order of the emitted bindings, or do what I did, and just
;; change the symbol =)
(context "/articles" +compojure-api-request+
:coercion :spec
:components [database]
:tags ["articles"]
(GET "/" request
:swagger {:summary "Retrieve all articles"
:produces #{"application/json"}
:security [{:andrewslai-pkce ["roles" "profile"]}]
:responses {200 {:description "A collection of all articles"
:schema :andrewslai.article/articles}}}
(ok (articles-api/get-articles database {:hostname (get-host request)})))
(context "/:article-url" [article-url]
(GET "/" request
:swagger {:summary "Retrieve a single article"
:produces #{"application/json"}
:security [{:andrewslai-pkce ["roles" "profile"]}]
:parameters {:path {:article-url :andrewslai.article/article-url}}
:responses {200 {:description "A single article"
:schema :andrewslai.article/article}}}
(if-let [article (articles-api/get-articles database {:article-url article-url})]
(ok article)
(not-found {:reason "Missing"})))
(context "/branches" _
:tags ["branches"]
(GET "/" []
:swagger {:summary "Retrieve all branches for a specific article"
:security [{:andrewslai-pkce ["roles" "profile"]}]
:produces #{"application/json"}}
(ok (articles-api/get-branches database {:article-url article-url})))
(context "/:branch-name" [branch-name]
(PUT "/" request
:swagger {:summary "Create an article branch"
:consumes #{"application/json"}
:produces #{"application/json"}
:security [{:andrewslai-pkce ["roles" "profile"]}]
:request :andrewslai.article/article
:responses {200 {:description "The article branch that was created"
:schema :andrewslai.article/article}
401 {:description "Unauthorized"
:schema ::error-message}}}
(try
(let [article (->article article-url request)
branch {:branch-name branch-name}]
(ok (doto (articles-api/create-branch! database article branch)
log/info)))
(catch Exception e
(log/error "Caught exception " e))))
;; Should probably be POST `publish-date`
(PUT "/publish" request
:swagger {:summary "Publish an article branch"
:produces #{"application/json"}
:security [{:andrewslai-pkce ["roles" "profile"]}]
:responses {200 {:description "The article branch that was created"
:schema :andrewslai.article/article}
401 {:description "Unauthorized"
:schema ::error-message}}}
(try
(log/infof "Publishing article %s and branch %s" article-url branch-name)
(let [[{:keys [branch-id]}] (articles-api/get-branches database {:branch-name branch-name
:article-url article-url})
result (articles-api/publish-branch! database branch-id)]
(ok result))
(catch Exception e
(log/error "Caught exception " e))))
(context "/versions" []
:tags ["versions"]
(POST "/" request
:swagger {:summary "Create a new version (commit) on a branch"
:consumes #{"application/json"}
:produces #{"application/json"}
:security [{:andrewslai-pkce ["roles" "profile"]}]
:request :andrewslai.article/article
:responses {200 {:description "The version that was created"
:schema :andrewslai.article/article}
401 {:description "Unauthorized"
:schema ::error-message}}}
(try
(let [commit (->commit request)
result (articles-api/new-version! database
{:branch-name branch-name
:hostname (get-host request)
:article-url article-url
:article-tags (get-in request [:body :article-tags] "thoughts")
:author (oidc/get-full-name (:identity request))}
commit)]
(log/info result)
(ok result))
(catch Exception e
(log/error "Caught exception " e)
(conflict (ex-message e))))))
)))))
(def branches-routes
;; HACK: I think the `context` macro may be broken, because it emits an s-exp
;; with let-bindings out of order: +compojure-api-request+ is referred to
;; before it is bound. This means that, to fix the bug, you'd need to either
;; reverse the order of the emitted bindings, or do what I did, and just
;; change the symbol =)
(context "/branches" +compojure-api-request+
:coercion :spec
:components [database]
:tags ["branches"]
(GET "/" request
:swagger {:summary "Retrieve all branches"
:security [{:andrewslai-pkce ["roles" "profile"]}]
:produces #{"application/json"}}
(let [query-params (select-keys (cske/transform-keys csk/->kebab-case-keyword (:query-params request))
[:article-id :article-url])
branches (articles-api/get-branches database (assoc query-params :hostname (get-host request)))]
(if (empty? branches)
(not-found {:reason "Missing"})
(ok branches))))
(POST "/" request
:swagger {:summary "Create a branch"
:consumes #{"application/json"}
:produces #{"application/json"}
:security [{:andrewslai-pkce ["roles" "profile"]}]
:request :andrewslai.article/article-branch
:responses {200 {:description "The branch that was created"
:schema :andrewslai.article/article-branch}
401 {:description "Unauthorized"
:schema ::error-message}}}
(try
(ok (articles-api/create-branch! database (assoc (:body-params request)
:hostname (get-host request)
:author (oidc/get-full-name (:identity request)))))
(catch Exception e
(log/error "Caught exception " e))))
(context "/:branch-id" [branch-id]
(GET "/versions" request
:tags ["versions"]
:swagger {:summary "Get versions"
:produces #{"application/json"}
:security [{:andrewslai-pkce ["roles" "profile"]}]
:responses {200 {:description "The version that was created"
:schema :andrewslai.article/article-branch}
401 {:description "Unauthorized"
:schema ::error-message}}}
(let [branches (articles-api/get-versions database {:branch-id (Integer/parseInt branch-id)})]
(if (empty? branches)
(not-found {:reason "Missing"})
(ok (reverse (sort-by :created-at branches)))))))))
(def compositions-routes
;; HACK: I think the `context` macro may be broken, because it emits an s-exp
;; with let-bindings out of order: +compojure-api-request+ is referred to
;; before it is bound. This means that, to fix the bug, you'd need to either
;; reverse the order of the emitted bindings, or do what I did, and just
;; change the symbol =)
(context "/compositions" +compojure-api-request+
:coercion :spec
:components [database]
:tags ["compositions"]
(GET "/" request
:swagger {:summary "Retrieve all published articles"
:description (str "This endpoint retrieves all published articles. "
"The endpoint is currently not paginated")
:produces #{"application/json"}
:responses {200 {:description "A collection of all published articles"
:schema :andrewslai.article/articles}}}
(log/infof "Getting compositions for host `%s`" (get-host request))
(ok (articles-api/get-published-articles database {:hostname (get-host request)})))
(GET "/:article-url" [article-url :as request]
:swagger {:summary "Retrieve a single published article"
:produces #{"application/json"}
:parameters {:path {:article-url :andrewslai.article/article-url}}
:responses {200 {:description "A single published article"
:schema :andrewslai.article/article}}}
(let [result (articles-api/get-published-articles database {:article-url article-url})]
(if (empty? result)
(not-found {:reason "Missing"})
(ok (first result)))))))