This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
80 lines (69 loc) · 2 KB
/
app.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
/*
sfxResolve module
Resolves an OpenURL against an SFX instance
KULeuven/LIBIS (c) 2018
Mehmet Celik
*/
const express = require('express');
const cors = require('cors');
const app = express();
const sfxResolver = require('./sfx');
const fs = require('fs');
const config = require('./config')('./config.json');
// set cors headers to allow cross domain calls from the browser
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
var corsOptions = {
origin: true,
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
//app.use(cors());
/**
* This is the main application. Only 1 parameter is needed 'url' it should contain the OpenURL
* @example https://sfxresolve?url=[OpenUrl]
*/
app.get('/', cors(corsOptions), async (req, res, next) => {
try {
const sfxUrl = req.query.url || null;
const remoteIp = req.query.ip || '0.0.0.0';
//fail is 'url' parameter is null
if (sfxUrl === null || sfxUrl.length == 0) {
res.status(503);
res.header('Content-Type', 'application/json ');
res.json({
error: 'Please supply OpenUrl in "url" parameter'
});
res.end();
} else {
response = await sfxResolver.resolve(sfxUrl, remoteIp);
res.json(response);
res.end();
}
} catch (e) {
//return the caught error
next(e);
}
});
/**
* return reclassify.json
*
* reclassify.json structure
* {
* "targetUrl": {
* "facility": "name of the Institution",
* "name": "display name",
* "url": "overwrite targetUrl"
* }
* }
*/
app.get('/reclassify', cors(corsOptions), async(req, res, next) => {
try {
res.status(200);
res.header('Content-Type', 'application/json ');
res.json(JSON.parse(fs.readFileSync(config.reclassify, 'utf8')));
res.end();
} catch(e) {
next(e);
}
})
//make app public
module.exports = app;