-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
96 lines (84 loc) · 2.92 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const express = require('express');
const path = require('path');
const fs = require('fs');
const url = require('url');
const swaggerUi = require('swagger-ui-express');
const jsYaml = require('js-yaml');
const logger = require('bunyan')
.createLogger({
name: 'paxdb-API',
module: 'app'
//TODO server / host / process ..
});
const routes = require('./routes/index');
const species = require('./routes/species');
const dataset = require('./routes/dataset');
const protein = require('./routes/protein');
const proteins = require('./routes/proteins');
const app = express();
try {
// eslint-disable-next-line detect-non-literal-fs-filename
const v1ApiDoc = fs.readFileSync(path.resolve(__dirname, './lib/paxdb-openapi.yaml'), 'utf8');
const apidoc = jsYaml.safeLoad(v1ApiDoc, { json: true });
app.use('/v1/swagger-ui', swaggerUi.serve, swaggerUi.setup(apidoc));
apidoc.servers.forEach((server) => {
logger.info(`swagger-ui mounted at ${server.url}/v1/swagger-ui`);
});
} catch (e) {
logger.error('failed to read the api yaml spec', e);
}
app.use((req, res, next) => {
// looks like it's not possible to list many hosts so need to mirror back allowed:
if (req.headers.origin) {
try {
const origin = url.parse(req.headers.origin, false).hostname;
if (origin.endsWith('pax-db.org')
|| origin.endsWith('string-db.org')
|| origin === 'https://editor.swagger.io'
|| origin.endsWith('localhost')
|| origin.endsWith('127.0.0.1')
) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
}
} catch (e) {
// logger.error('failed to set cors: ', JSON.stringify(req.headers));
logger.error('failed to set cors: ', req.headers);
}
}
next();
});
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use('/', routes);
app.use('/species', species);
app.use('/dataset', dataset);
app.use('/protein', protein);
app.use('/proteins', proteins);
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
res.status(err.status || 500);
res.render('error', {
message: err.message
});
});
module.exports = app;