This repository has been archived by the owner on Feb 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
61 lines (53 loc) · 1.77 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
const path = require('path');
const express = require('express');
const helmet = require('helmet');
const log = require('./lib/logger');
const rewriteUrl = require('./lib/rewriteUrl');
const constants = require('./config/constants');
const promBundle = require('./lib/promBundle');
const errorCounter = require('./lib/promCounters').errorPageViews;
const app = express();
// start collecting default metrics
promBundle.promClient.collectDefaultMetrics();
// metrics needs to be registered before routes wishing to have metrics generated
// see https://github.com/jochen-schweizer/express-prom-bundle#sample-uusage
app.use(promBundle.middleware);
app.use(helmet({
noCache: true,
frameguard: { action: 'deny' },
hsts: { includeSubDomains: false },
contentSecurityPolicy: {
directives: {
defaultSrc: ['\'self\''],
imgSrc: ['\'self\'', 'data:'],
styleSrc: ['\'unsafe-inline\''],
}
},
}));
app.use((req, res, next) => {
log.debug({ req });
next();
});
app.get('/', (req, res) => {
res.redirect(301, constants.SITE_ROOT);
});
app.get(constants.SITE_ROOT, (req, res) => {
const debug = req.query.debug;
const referer = req.get('referer');
const rewrittenUrl = rewriteUrl(referer);
if (debug === '' || debug) {
log.debug({ req, debug: true, rewrittenUrl }, `Not redirecting request from ${referer} to ${rewrittenUrl} due to debug flag`);
return res.json({
headers: req.headers,
redirectTo: rewrittenUrl,
});
}
if (rewrittenUrl) {
log.debug({ referer, rewrittenUrl }, `Redirecting request from ${referer} to ${rewrittenUrl}`);
return res.redirect(rewrittenUrl);
}
errorCounter.inc(1);
log.error({ req }, 'Unable to redirect');
return res.sendFile(path.join(__dirname, '/views/options.html'));
});
module.exports = app;