forked from gbv/jskos-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
585 lines (527 loc) · 16.1 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/**
* Simple JSON API to retrieve JSKOS Concept Mappings for mappings between RVK and GND.
*
* If the database doesn't exist yet, import the mappings like this:
* mongoimport --db cocoda_api --collection mappings --file mappings.ndjson
*
* Import vocabularies into collection "terminologies" and their concepts into collection "concepts":
* mongoimport --db cocoda_api --collection terminologies --file terminologies.ndjson
* mongoimport --db cocoda_api --collection concepts --file concepts.ndjson
*
* Download the file from here: http://coli-conc.gbv.de/concordances/
*/
const config = require("./config")
config.log(`running in ${config.env} mode`)
if (!config.auth.postAuthRequired) {
config.log("Note: POST /mappings does not require authentication. To change this, remove `auth.postAuthRequired` from the configuration file.")
}
if (!config.baseUrl) {
config.log("Warning: If you're using jskos-server behind a reverse proxy, it is necessary to add `baseUrl` to the configuration file!")
}
const express = require("express")
const bodyParser = require("body-parser")
const app = express()
const mongo = require("mongodb").MongoClient
const MappingProvider = require("./lib/mapping-provider")
const TerminologyProvider = require("./lib/terminology-provider")
const StatusProvider = require("./lib/status-provider")
const AnnotationProvider = require("./lib/annotation-provider")
const _ = require("lodash")
const jskos = require("jskos-tools")
const portfinder = require("portfinder")
const { Transform } = require("stream")
const JSONStream = require("JSONStream")
const util = require("./lib/util")
// Pretty-print JSON output
app.set("json spaces", 2)
let optionalStrategies = [], auth = null
// Prepare authorization via JWT
const passport = require("passport")
if (config.auth.algorithm && config.auth.key) {
const JwtStrategy = require("passport-jwt").Strategy,
ExtractJwt = require("passport-jwt").ExtractJwt
var opts = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: config.auth.key,
algorithms: [config.auth.algorithm]
}
try {
passport.use(new JwtStrategy(opts, (jwt_payload, done) => {
done(null, jwt_payload.user)
}))
// Use like this: app.get("/secureEndpoint", auth, (req, res) => { ... })
// res.user will contain the current authorized user.
auth = passport.authenticate("jwt", { session: false })
optionalStrategies.push("jwt")
} catch(error) {
console.error("Error setting up JWT authentication")
}
} else {
console.warn("Note: To provide authentication via JWT, please add `auth.algorithm` and `auth.key` to the configuration file!")
// Deny all requests
auth = (req, res) => {
res.sendStatus(403)
}
}
// Also use anonymous strategy for endpoints that can be used authenticated or not authenticated
const AnonymousStrategy = require("passport-anonymous").Strategy
passport.use(new AnonymousStrategy())
optionalStrategies.push("anonymous")
// For endpoints with optional authentication
// For example: app.get("/optionallySecureEndpoint", config.auth.postAuthRequired ? auth : authOptional, (req, res) => { ... })
// req.user will cointain the user if authorized, otherwise stays undefined.
const authOptional = passport.authenticate(optionalStrategies, { session: false })
// Promise for MongoDB db
const db = mongo.connect(config.mongo.url, config.mongo.options).then(client => {
return client.db(config.mongo.db)
}).catch(error => {
throw error
})
db.then(db => {
config.log(`connected to MongoDB ${config.mongo.url} (database: ${config.mongo.db})`)
mappingProvider = new MappingProvider(db.collection("mappings"), db.collection("concordances"), db.collection("terminologies"))
terminologyProvider = new TerminologyProvider(db.collection("terminologies"), db.collection("concepts"))
statusProvider = new StatusProvider(db)
annotationProvider = new AnnotationProvider(db.collection("annotations"))
if (config.env == "test") {
portfinder.basePort = config.port
return portfinder.getPortPromise()
} else {
return Promise.resolve(config.port)
}
}).then(port => {
app.listen(port, () => {
config.log(`listening on port ${port}`)
})
}).catch(error => {
console.error("Error with database or express:", error)
})
// Add default headers
app.use(function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*")
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
res.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,PATCH,DELETE")
res.setHeader("Access-Control-Expose-Headers", "X-Total-Count, Link")
res.setHeader("Content-Type", "application/json; charset=utf-8")
next()
})
// Add body-parser middleware
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
// Recursively remove all fields starting with _ from response
function cleanJSON(json) {
if (_.isArray(json)) {
json.forEach(cleanJSON)
} else if (_.isObject(json)) {
_.forOwn(json, (value, key) => {
if (key.startsWith("_")) {
// remove from object
_.unset(json, key)
} else {
cleanJSON(value)
}
})
}
}
function adjustSchemes(schemes) {
// Remove MongoDB specific fields, add JSKOS specific fields
schemes.forEach(scheme => {
delete scheme._id
scheme["@context"] = "https://gbv.github.io/jskos/context.json"
scheme.type = scheme.type || ["http://www.w3.org/2004/02/skos/core#ConceptScheme"]
})
return schemes
}
function adjustConcept(req) {
return concept => {
if (!concept) {
return null
}
// Remove MongoDB specific fields, add JSKOS specific fields
delete concept._id
concept["@context"] = "https://gbv.github.io/jskos/context.json"
concept.type = concept.type || ["http://www.w3.org/2004/02/skos/core#Concept"]
return util.handleProperties({ terminologyProvider, annotationProvider }, concept, _.get(req, "query.properties"))
}
}
function adjustConcepts(req) {
return concepts => {
return Promise.all(concepts.map(concept => adjustConcept(req)(concept)))
}
}
function adjustMapping(req) {
return mapping => {
if (!mapping) {
return null
}
// Remove MongoDB specific fields, add JSKOS specific fields
delete mapping._id
mapping["@context"] = "https://gbv.github.io/jskos/context.json"
return util.handleProperties({ annotationProvider }, mapping, _.get(req, "query.properties"))
}
}
function adjustMappings(req) {
return mappings => {
return Promise.all(mappings.map(mapping => adjustMapping(req)(mapping)))
}
}
function adjustAnnotations(req) {
return annotations => {
return annotations.map(annotation => util.adjustAnnotation(req)(annotation))
}
}
function handleDownload(req, res, results, filename) {
/**
* Transformation object to remove _id parameter from objects in a stream.
*/
const removeIdTransform = new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
cleanJSON(chunk)
this.push(chunk)
callback()
}
})
// Default transformation: JSON
let transform = JSONStream.stringify("[\n", ",\n", "\n]\n")
let fileEnding = "json"
let first = true, delimiter = ","
switch (req.query.download) {
case "ndjson":
fileEnding = "ndjson"
res.set("Content-Type", "application/x-ndjson; charset=utf-8")
transform = new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
this.push(JSON.stringify(chunk) + "\n")
callback()
}
})
break
case "csv":
case "tsv":
fileEnding = req.query.download
if (req.query.download == "csv") {
delimiter = ","
res.set("Content-Type", "text/csv; charset=utf-8")
} else {
delimiter = "\t"
res.set("Content-Type", "text/tab-separated-values; charset=utf-8")
}
transform = new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
// Small workaround to prepend a line to CSV
if (first) {
this.push(`"fromNotation"${delimiter}"toNotation"${delimiter}"type"\n`)
first = false
}
let mappingToCSV = jskos.mappingToCSV({
lineTerminator: "\r\n",
delimiter,
})
this.push(mappingToCSV(chunk))
callback()
}
})
break
}
// Add file header
res.set("Content-disposition", `attachment; filename=${filename}.${fileEnding}`)
// results is a database cursor
results.stream()
.pipe(removeIdTransform)
.pipe(transform)
.pipe(res)
}
const mung = require("express-mung")
app.use(mung.json((cleanJSON)))
const path = require("path")
app.get("/", function(req, res) {
res.setHeader("Content-Type", "text/html")
res.sendFile(path.join(__dirname + "/index.html"))
})
app.get("/checkAuth", auth, (req, res) => {
res.sendStatus(204)
})
app.get("/status", (req, res) => {
statusProvider.getStatus(req)
.then(result => {
res.json(result)
})
})
/**
* ########## Mapping related endpoints ##########
*/
if (config.mappings) {
app.get("/concordances", (req, res) => {
let supportedTypes = ["json", "ndjson"]
if (req.query.download && !supportedTypes.includes(req.query.download)) {
req.query.download = null
}
mappingProvider.getConcordances(req, res)
.catch(err => res.send(err))
.then(results => {
if (req.query.download) {
handleDownload(req, res, results, "concordances")
} else {
res.json(results)
}
})
})
app.get("/mappings", (req, res) => {
let supportedTypes = ["json", "ndjson", "csv", "tsv"]
if (req.query.download && !supportedTypes.includes(req.query.download)) {
req.query.download = null
}
mappingProvider.getMappings(req, res)
.catch(err => res.send(err))
// Only adjust if it's not a download (-> stream)
.then(req.query.download ? (result => result) : adjustMappings(req))
.then(results => {
if (req.query.download) {
handleDownload(req, res, results, "mappings")
} else {
res.json(results)
}
})
})
app.post("/mappings", config.auth.postAuthRequired ? auth : authOptional, (req, res) => {
mappingProvider.saveMapping(req, res)
.catch(err => res.send(err))
.then(adjustMapping(req))
.then(result => {
if (result) {
res.status(201).json(result)
} else {
if (!res.headersSent) {
res.sendStatus(400)
}
}
})
})
app.get("/mappings/suggest", (req, res) => {
mappingProvider.getNotationSuggestions(req, res)
.catch(err => res.send(err))
.then(results => {
res.json(results)
})
})
app.get("/mappings/voc", (req, res) => {
mappingProvider.getMappingSchemes(req, res)
.catch(err => res.send(err))
.then(adjustSchemes)
.then(results => {
res.json(results)
})
})
app.get("/mappings/:_id", (req, res) => {
mappingProvider.getMapping(req, res)
.catch(err => res.send(err))
.then(adjustMapping(req))
.then(result => {
if (result) {
res.json(result)
} else {
res.sendStatus(404)
}
})
})
app.put("/mappings/:_id", auth, (req, res) => {
mappingProvider.putMapping(req, res)
.catch(err => res.send(err))
.then(adjustMapping(req))
.then(result => {
if (result) {
res.json(result)
} else {
if (!res.headersSent) {
res.sendStatus(400)
}
}
})
})
app.patch("/mappings/:_id", auth, (req, res) => {
mappingProvider.patchMapping(req, res)
.catch(err => res.send(err))
.then(adjustMapping(req))
.then(result => {
if (result) {
res.json(result)
} else {
if (!res.headersSent) {
res.sendStatus(400)
}
}
})
})
app.delete("/mappings/:_id", auth, (req, res) => {
mappingProvider.deleteMapping(req, res)
.catch(err => res.send(err))
.then(result => {
// `result` will be either true or false
if (result) {
res.sendStatus(204)
} else {
if (!res.headersSent) {
res.sendStatus(400)
}
}
})
})
}
/**
* ########## Annotation related endpoints ##########
*/
if (config.annotations) {
app.get("/annotations", (req, res) => {
annotationProvider.getAnnotations(req, res)
.catch(err => res.send(err))
.then(adjustAnnotations(req))
.then(results => {
res.json(results)
})
})
app.post("/annotations", auth, (req, res) => {
annotationProvider.postAnnotation(req, res)
.catch(err => res.send(err))
.then(util.adjustAnnotation(req))
.then(result => {
if (result) {
res.status(201).json(result)
} else {
res.sendStatus(400)
}
})
})
app.get("/annotations/:_id", (req, res) => {
annotationProvider.getAnnotation(req, res)
.catch(err => res.send(err))
.then(util.adjustAnnotation(req))
.then(result => {
if (result) {
res.json(result)
} else {
res.sendStatus(404)
}
})
})
app.put("/annotations/:_id", auth, (req, res) => {
annotationProvider.putAnnotation(req, res)
.catch(err => res.send(err))
.then(util.adjustAnnotation(req))
.then(result => {
if (result) {
res.json(result)
} else {
if (!res.headersSent) {
res.sendStatus(400)
}
}
})
})
app.patch("/annotations/:_id", auth, (req, res) => {
annotationProvider.patchAnnotation(req, res)
.catch(err => res.send(err))
.then(util.adjustAnnotation(req))
.then(result => {
if (result) {
res.json(result)
} else {
if (!res.headersSent) {
res.sendStatus(400)
}
}
})
})
app.delete("/annotations/:_id", auth, (req, res) => {
annotationProvider.deleteAnnotation(req, res)
.catch(err => res.send(err))
.then(result => {
// `result` will be either true or false
if (result) {
res.sendStatus(204)
} else {
if (!res.headersSent) {
res.sendStatus(400)
}
}
})
})
}
/**
* ########## Scheme related endpoints ##########
*/
if (config.schemes) {
app.get("/voc", (req, res) => {
terminologyProvider.getVocabularies(req, res)
.catch(err => res.send(err))
.then(adjustSchemes)
.then(results => {
res.json(results)
})
})
app.get("/voc/top", (req, res) => {
terminologyProvider.getTop(req, res)
.catch(err => res.send(err))
.then(adjustConcepts(req))
.then(results => {
res.json(results)
})
})
app.get("/voc/concepts", (req, res) => {
terminologyProvider.getConcepts(req, res)
.catch(err => res.send(err))
.then(adjustConcepts(req))
.then(results => {
res.json(results)
})
})
}
/**
* ########## Concept related endpoints ##########
*/
if (config.concepts) {
app.get("/data", (req, res) => {
terminologyProvider.getDetails(req, res)
.catch(err => res.send(err))
.then(adjustConcepts(req))
.then(results => {
res.json(results)
})
})
app.get("/narrower", (req, res) => {
terminologyProvider.getNarrower(req, res)
.catch(err => res.send(err))
.then(adjustConcepts(req))
.then(results => {
res.json(results)
})
})
app.get("/ancestors", (req, res) => {
terminologyProvider.getAncestors(req, res)
.catch(err => res.send(err))
.then(adjustConcepts(req))
.then(results => {
res.json(results)
})
})
app.get("/suggest", (req, res) => {
terminologyProvider.getSuggestions(req, res)
.catch(err => res.send(err))
.then(results => {
res.json(results)
})
})
app.get("/search", (req, res) => {
terminologyProvider.search(req, res)
.catch(err => res.send(err))
.then(adjustConcepts(req))
.then(results => {
res.json(results)
})
})
}
module.exports = {
db, app
}