-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
338 lines (309 loc) · 11.4 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
'use strict'
const express = require('express')
const app = express()
const compression = require('compression')
const passport = require('passport')
const helmet = require('helmet')
// const csrf = require('csurf')
const { Strategy, ExtractJwt } = require('passport-jwt')
// const elasticsearchQueue = require('./processFiles/searchQueue')
// const epubQueue = require('./processFiles/index')
const cache = require('./utils/cache')
const errorHandling = require('./routes/_middleware/error-handling')
const { metricsQueue } = require('./utils/metrics')
const { fileDeleteQueue } = require('./utils/file-delete')
// Routes
// Reader
const whoamiRoute = require('./routes/readers/whoami') // GET /whoami
const readersRoute = require('./routes/readers/reader-post') // POST /readers
const readerGetRoute = require('./routes/readers/reader-get') // GET /readers/:id
const readerPutRoute = require('./routes/readers/reader-put') // PUT /readers/:id
const readerDeleteRoute = require('./routes/readers/reader-delete') // DELETE /readers/:id
// Sources
const sourcePostRoute = require('./routes/sources/source-post') // POST /sources
const sourcePatchRoute = require('./routes/sources/source-patch') // PATCH /sources/:id
const sourceDeleteRoute = require('./routes/sources/source-delete') // DELETE /sources/:id
const sourcePutTagRoute = require('./routes/sources/source-put-tag') // PUT /sources/:sourceId/tags/:tagId
const sourceDeleteTagRoute = require('./routes/sources/source-delete-tag') // DELETE /sources/:sourceId/tags/:tagId
const sourceGetRoute = require('./routes/sources/source-get') // GET /sources/:id
const readActivityPostRoute = require('./routes/sources/readActivity-post') // POST /sources/:id/readActivity
const sourceBatchUpdate = require('./routes/sources/source-batchUpdate') // PATCH /sources/batchUpdate
// Search
// const searchRoute = require('./routes/search') // not working!
// Tags
const getTagsRoute = require('./routes/tags/tags-get') // GET /tags
const tagPostRoute = require('./routes/tags/tag-post') // POST /tags
const tagPutRoute = require('./routes/tags/tag-put') // PUT /tags/:id
const tagDeleteRoute = require('./routes/tags/tag-delete') // DELETE /tags/:id
// Library
const readerLibraryRoute = require('./routes/sources/library-get') // GET /library
// Notes
const readerNotesRoute = require('./routes/notes/readerNotes-get') // GET /notes
const getNoteRoute = require('./routes/notes/note-get') // GET /notes/:id
const notePutTagRoute = require('./routes/notes/note-put-tag') // PUT /notes/:noteId/tags/:tagId
const noteDeleteTagRoute = require('./routes/notes/note-delete-tag') // DELETE /notes/:noteId/tags/:tagId
const notePostRoute = require('./routes/notes/note-post') // POST /notes
const noteDeleteRoute = require('./routes/notes/note-delete') // DELETE /notes/:id
const notePutRoute = require('./routes/notes/note-put') // PUT /notes/:id
// NoteRelations
const noteRelationPostRoute = require('./routes/noteRelations/noteRelation-post') // POST /noteRelations
const noteRelationPutRoute = require('./routes/noteRelations/noteRelation-put') // PUT /noteRelations/:id
const noteRelationDeleteRoute = require('./routes/noteRelations/noteRelation-delete') // DELETE /noteRelations/:id
// NoteContexts
const noteContextPostRoute = require('./routes/noteContexts/noteContext-post') // POST /noteContexts
const noteContextPutRoute = require('./routes/noteContexts/noteContext-put') // PUT /noteContexts/:id
const noteContextDeleteRoute = require('./routes/noteContexts/noteContext-delete') // DELETE /noteContexts/:id
const noteContextAddNoteRoute = require('./routes/noteContexts/noteContext-addNote') // POST /noteContexts/:id/notes
const noteContextGetRoute = require('./routes/noteContexts/noteContext-get') // GET /noteContexts/:id
const noteContextsGetRoute = require('./routes/noteContexts/noteContexts-get')
// outlines
const outlineGetRoute = require('./routes/outlines/outline-get') // GET /outlines/:id
const outlinePostRoute = require('./routes/outlines/outline-post') // POST /outlines
const outlineDeleteRoute = require('./routes/outlines/outline-delete') // DELETE /outlines/:id
const outlinePutRoute = require('./routes/outlines/outline-put') // PUT /outlines:id
const outlineAddNoteRoute = require('./routes/outlines/outline-addNote') // POST /outlines/:id/notes
const outlineDeleteNoteRoute = require('./routes/outlines/outline-deleteNote') // DELETE /outlines/:id/notes/:noteId
const outlinePatchNoteRoute = require('./routes/outlines/outline-patchNote') // PATCH /outlines/:id/notes/:noteId
const outlineGetDocxRoute = require('./routes/outlines/outline-getDocx')
// notebooks
const notebookPostRoute = require('./routes/notebooks/notebook-post') // POST /notebooks
const notebookGetRoute = require('./routes/notebooks/notebook-get') // GET /notebooks/:id
const notebooksGetRoute = require('./routes/notebooks/notebooks.get') // GET /notebooks
const notebookPutRoute = require('./routes/notebooks/notebook-put') // PUT /notebooks/:id
const notebookDeleteRoute = require('./routes/notebooks/notebook-delete') // DELETE /notebooks/:id
const notebookPutSourceRoute = require('./routes/notebooks/notebook-put-source') // PUT /notebooks/:id/sources/:sourceId
const notebookDeleteSourceRoute = require('./routes/notebooks/notebook-delete-source') // DELETE /notebooks/:id/sources/:sourceId
const notebookPutNoteRoute = require('./routes/notebooks/notebook-put-note') // PUT /notebooks/:id/notes/:noteId
const notebookDeleteNoteRoute = require('./routes/notebooks/notebook-delete-note') // DELETE /notebooks/:id/notes/:noteId
const notebookPostNoteRoute = require('./routes/notebooks/notebook-note-post') // POST /notebooks/:id/notes
const notebookPutTagRoute = require('./routes/notebooks/notebook-put-tag') // PUT /notebooks/:id/tags/:tagId
const notebookDeleteTagRoute = require('./routes/notebooks/notebook-delete-tag') // DELETE /notebooks/:id/tags/:tagId
const notebookPostSourceRoute = require('./routes/notebooks/notebook-source-post')
// collaborators
const collaboratorPostRoute = require('./routes/collaborator/collaborator-post')
const collaboratorPutRoute = require('./routes/collaborator/collaborator-put')
const collaboratorDeleteRoute = require('./routes/collaborator/collaborator-delete')
const hardDeleteRoute = require('./routes/hardDelete')
const metricsGetRoute = require('./routes/metrics-get')
const metadataGetRoute = require('./routes/metadata-get')
// search
const searchRoute = require('./routes/search-post')
const setupKnex = async skip_migrate => {
let config
config = require('./knexfile.js')
app.knex = require('knex')(config)
if (!skip_migrate) {
await app.knex.migrate.rollback()
await app.knex.migrate.latest()
}
const objection = require('objection')
const Model = objection.Model
Model.knex(app.knex)
return null
}
const setupPassport = () => {
var opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken()
opts.secretOrKey = process.env.SECRETORKEY
opts.issuer = process.env.ISSUER
opts.audience = process.env.AUDIENCE
passport.use(
new Strategy(opts, (jwt, callback) => {
callback(null, jwt.sub)
})
)
}
setupPassport()
// Security settings
app.use(
helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: [
"'self'",
"'unsafe-inline'",
'https://cdn.jsdelivr.net/npm/redoc@next/'
],
styleSrc: ["'self'", "'unsafe-inline'"],
workerSrc: ['blob:'],
connectSrc: ["'self'"],
objectSrc: ["'none'"],
imgSrc: ['*', 'data:', 'https:'],
frameSrc: [
'https://www.youtube.com',
'https://www.youtube-nocookie.com'
],
fontSrc: ["'self'"],
formAction: ["'self'", 'https://rebus.auth0.com/'],
frameAncestors: ["'none'"]
}
}
})
)
// Basic Settings
app.enable('strict routing')
app.set('trust proxy', true)
app.use(express.urlencoded({ extended: true }))
app.use(
express.json({
type: [
'application/json',
'application/activity+json',
'application/ld+json'
],
limit: '100mb'
})
)
app.use(compression())
app.use(passport.initialize())
/**
* @swagger
* /:
* get:
* tags:
* - general
* description: GET /
* produces:
* - text/html:
* responses:
* 200:
* description: confirmation that the api is running
* content:
* text/html:
* properties:
* running:
* type: string
* enum: ['true']
*
*/
app.get('/', function (req, res) {
return res.format({
'text/html': function () {
res.send('Running!')
},
'application/json': function () {
return res.send({ running: true })
}
})
})
app.initialized = false
app.initialize = async skip_migrate => {
if (!app.initialized) {
await setupKnex(skip_migrate)
app.initialized = true
}
await cache.connectClient()
return app.initialized
}
app.terminate = async () => {
if (!app.initialized) {
throw new Error('App not initialized; cannot terminate')
}
app.initialized = false
if (metricsQueue) {
await metricsQueue.clean(0)
await metricsQueue.clean(0, 'failed')
await metricsQueue.empty()
metricsQueue.close()
}
if (fileDeleteQueue) {
await fileDeleteQueue.clean(0)
await fileDeleteQueue.clean(0, 'failed')
await fileDeleteQueue.empty()
fileDeleteQueue.close()
}
// if (elasticsearchQueue) {
// await elasticsearchQueue.clean(0)
// await elasticsearchQueue.clean(0, 'failed')
// await elasticsearchQueue.empty()
// elasticsearchQueue.close()
// }
// if (epubQueue) {
// await epubQueue.clean(0)
// await epubQueue.clean(0, 'failed')
// await epubQueue.empty()
// epubQueue.close()
// }
if (cache) {
cache.quitCache()
}
return await app.knex.destroy()
}
whoamiRoute(app)
readersRoute(app)
getTagsRoute(app)
// new routes
readActivityPostRoute(app)
sourcePostRoute(app)
sourceBatchUpdate(app)
sourcePatchRoute(app)
sourceDeleteRoute(app)
sourcePutTagRoute(app)
sourceDeleteTagRoute(app)
sourceGetRoute(app)
tagPostRoute(app)
tagPutRoute(app)
tagDeleteRoute(app)
readerGetRoute(app)
readerLibraryRoute(app)
readerNotesRoute(app)
getNoteRoute(app)
notePutTagRoute(app)
noteDeleteTagRoute(app)
notePostRoute(app)
noteDeleteRoute(app)
notePutRoute(app)
noteRelationPostRoute(app)
noteRelationPutRoute(app)
noteRelationDeleteRoute(app)
noteContextPostRoute(app)
noteContextPutRoute(app)
noteContextDeleteRoute(app)
noteContextAddNoteRoute(app)
noteContextGetRoute(app)
noteContextsGetRoute(app)
outlineGetDocxRoute(app)
outlineGetRoute(app)
outlinePostRoute(app)
outlineDeleteRoute(app)
outlinePutRoute(app)
outlineAddNoteRoute(app)
outlineDeleteNoteRoute(app)
outlinePatchNoteRoute(app)
readerPutRoute(app)
readerDeleteRoute(app)
collaboratorPostRoute(app)
collaboratorPutRoute(app)
collaboratorDeleteRoute(app)
notebookPostRoute(app)
notebookGetRoute(app)
notebooksGetRoute(app)
notebookPutRoute(app)
notebookDeleteRoute(app)
notebookPutSourceRoute(app)
notebookDeleteSourceRoute(app)
notebookPutNoteRoute(app)
notebookDeleteNoteRoute(app)
notebookPostNoteRoute(app)
notebookPutTagRoute(app)
notebookDeleteTagRoute(app)
notebookPostSourceRoute(app)
hardDeleteRoute(app)
metricsGetRoute(app)
metadataGetRoute(app)
searchRoute(app)
app.use(errorHandling)
app.use((req, res) => {
res.sendStatus(404)
})
app.start = port => {
app.listen(port, () => console.log('Listening'))
}
module.exports = {
// Export app for reuse in other express apps/servers
app,
start (port) {
app.start(port)
}
}