-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
262 lines (250 loc) · 9.2 KB
/
server.js
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
251
252
253
254
255
256
257
258
259
260
261
262
const http = require('http');
const config = require('./config'); // eslint-disable-line
const { generateWebsiteInfos, generateAllCaches } = require('./libs/cacheFilesGenerators');
const logger = require('./libs/logger');
const sendRes = require('./libs/sendRes');
const commentController = require('./controllers/commentController');
const notificationController = require('./controllers/notificationController');
generateWebsiteInfos().catch(logger.error);
generateAllCaches().catch(logger.error);
http.createServer((req, res) => {
if (config.manageCors) {
res.setHeader('Access-Control-Allow-Origin', config.siteUrl);
if (req.method === 'OPTIONS') {
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE, PATCH');
res.setHeader('Access-Control-Allow-Headers', 'uploader-chunk-number,uploader-chunks-total,uploader-file-id');
res.setHeader('Access-Control-Max-Age', '86400'); // 24hrs
res.writeHead(204, 'No Content');
res.end();
return;
}
}
/**
* SERVED FROM CACHE
*
* @api { GET } /infos/site.json Get global site infos
* @apiName getSiteInfos
* @apiGroup Infos
*
* @apiSuccess (200) { JSON } comments Article's comments with commenters infos
*
* @apiSuccessExample Success-Response:
* HTTP/2.0 200 OK
* {
* {
* "md5_admin_email": "140338da61f6172949f863b39e61afae",
* "commentsCount": {
* "hello-npm": 15,
* …
* }
* }
* }
*/
/**
* SERVED FROM CACHE
*
* @api { GET } /article/:slug Get comments for a page
* @apiName getArticleComments
* @apiGroup Comments
*
* @apiParam { String } slug The page's slug for which the comment was posted (suffixed by .json)
*
* @apiSuccess (200) { JSON } comments Article's comments with commenters infos
*
* @apiSuccessExample Success-Response:
* HTTP/2.0 200 OK
* [
* {
* "id": 960,
* "parent_id": null,
* "name": "Lugdanum",
* "md5_email": "23d8cf593dab275ab59ea3a8dcfb6eef",
* "submitted_at": "2019-03-16T21:41:57.000Z",
* "comment": "…"
* },
* {
* "id": 961,
* "parent_id": 960,
* "name": "Buzut",
* "md5_email": "140338da61f6172949f863b39e61afae",
* "submitted_at": "2019-03-16T23:42:25.000Z"",
* "comment": "…"
* }
* ]
*
* @apiError { text/plain } typeErr paramX param must be a typeY
*
* @apiError { text/plain } nameErr name must be between 3 and 20 chars
*
* @apiError { text/plain } emailErr email must be an email string
*
* @apiError { text/plain } commentErr comment cannot be longer than 3000 chars
*
* @apiError { text/plain } unknowToken The provided token is invalid
*
* @apiErrorExample Error-Response:
* HTTP/2.0 404 Not Found
*/
/**
* @api { POST } /comment/ Post a new comment
* @apiName postComment
* @apiGroup Comments
*
* @apiParam { String } slug The page's slug for which the comment is posted
*
* @apiParam { String{3..20} } name
*
* @apiParam { String } email
*
* @apiParam { String{..3000} } comment
*
* @apiParam { Integer\String } [parent_id] An integer or a string containing an interger that represents the parent_id
*
* @apiParam { Boolean\String } [notify=true] A bool or string [true|false] stating if the commenter whish to be notified of following comments in this thread
*
* @apiSuccess (201) { Null } null
*
* @apiSuccessExample Success-Response:
* HTTP/2.0 201 Created
*
* @apiError { text/plain } typeErr paramX param must be a typeY
*
* @apiError { text/plain } nameErr name must be between 3 and 20 chars
*
* @apiError { text/plain } emailErr email must be an email string
*
* @apiError { text/plain } emailMXErr Your email seems unreachable
*
* @apiError { text/plain } commentErr comment cannot be longer than 3000 chars
*
* @apiError { text/plain } parentErr parent_id must be a valid article id
*
* @apiErrorExample Error-Response:
* HTTP/2.0 400 Bad Request
* typeErr paramX param must be a typeY
*/
if (req.url === '/comment/' && req.method === 'POST') return commentController.addComment(req, res);
/**
* @api { POST } /comment/validate/:comment_id Validate a comment
* @apiName validateComment
* @apiGroup Comments
*
* @apiParam { String } comment_id Passed in url
*
* @apiParam { String } user_secret As received in the validation email
*
* @apiSuccess (204) { Null } null
*
* @apiSuccessExample Success-Response:
* HTTP/2.0 204 No Content
*
* @apiError { text/plain } userSecretErr user_secret should be 18 chars
*
* @apiError { text/plain } mismatch Either comment_id or user_secret don’t match
*
* @apiErrorExample Error-Response:
* HTTP/2.0 404 Not Found
*
* @apiErrorExample Error-Response:
* HTTP/2.0 400 Bad Request
* userSecretErr user_secret should be 18 chars
*/
const validateCommentUrl = RegExp('/comment/validate/([0-9]+)').exec(req.url);
if (validateCommentUrl && req.method === 'POST') return commentController.approveComment(req, res, validateCommentUrl[1]);
// for patch and delete comment
const modifyCommentUrl = RegExp('/comment/([0-9]+)').exec(req.url);
/**
* @api { PATCH } /comment/:comment_id Update a comment
* @apiName updateComment
* @apiGroup Comments
*
* @apiParam { String } comment_id Passed in url
*
* @apiParam { String } user_secret As received in the validation email
*
* @apiParam { String{..3000} } comment
*
* @apiSuccess (204) { Null } null
*
* @apiSuccessExample Success-Response:
* HTTP/2.0 204 No Content
*
* @apiError { text/plain } userSecretErr user_secret should be 18 chars
*
* @apiError { text/plain } commentErr comment cannot be longer than 3000 chars
*
* @apiError { text/plain } mismatch Either comment_id or user_secret don’t match
*
* @apiErrorExample Error-Response:
* HTTP/2.0 404 Not Found
*
* @apiErrorExample Error-Response:
* HTTP/2.0 400 Bad Request
* userSecretErr user_secret should be 18 chars
*/
if (modifyCommentUrl && req.method === 'PATCH') return commentController.updateComment(req, res, modifyCommentUrl[1]);
/**
* @api { DELETE } /comment/:comment_id Delete a comment
* @apiName deleteComment
* @apiGroup Comments
*
* @apiParam { String } comment_id Passed in url
*
* @apiParam { String } user_secret As received in the validation email
*
* @apiSuccess (204) { Null } null
*
* @apiSuccessExample Success-Response:
* HTTP/2.0 204 No Content
*
* @apiError { text/plain } userSecretErr user_secret should be 18 chars
*
* @apiError { text/plain } mismatch Either comment_id or user_secret don’t match
*
* @apiError { text/plain } authErr User is not allowed to delete this comment
*
* @apiErrorExample Error-Response:
* HTTP/2.0 404 Not Found
*
* @apiErrorExample Error-Response:
* HTTP/2.0 400 Bad Request
* userSecretErr user_secret should be 18 chars
*/
if (modifyCommentUrl && req.method === 'DELETE') return commentController.deleteComment(req, res, modifyCommentUrl[1]);
/**
* @api { PATCH } /notification/article/:article_id Update notification behaviour
* @apiName updateNotification
* @apiGroup Notifications
*
* @apiParam { String } article_id Passed in url
*
* @apiParam { String } user_id As received in the notification email
*
* @apiParam { String } user_secret As received in the notification email
*
* @apiSuccess (204) { Null } null
*
* @apiSuccessExample Success-Response:
* HTTP/2.0 204 No Content
*
* @apiError { text/plain } typeErr paramX param must be a typeY
*
* @apiError { text/plain } userSecretErr user_secret should be 18 chars
*
* @apiError { text/plain } mismatch Either comment_id, user_id or user_secret don’t match
*
* @apiErrorExample Error-Response:
* HTTP/2.0 404 Not Found
*
* @apiErrorExample Error-Response:
* HTTP/2.0 400 Bad Request
* userSecretErr user_secret should be 18 chars
*/
const validateNotification = RegExp('/notification/article/([0-9]+)').exec(req.url);
if (validateNotification && req.method === 'PATCH') return notificationController.updateSubscription(req, res, validateNotification[1]);
// unknown route
return sendRes(res, 404, 'Resource Not Found');
})
.listen(config.port, () => {
logger.info(`Listening for requests on post: ${config.port}`);
});