-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
68 lines (58 loc) · 2.52 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
module.exports = function() {
var express = require('express'),
path = require('path'),
config = require('./config.js'),
fs = require('fs'),
app = express(),
version = require('./version.json');
app.enable('trust proxy');
app.set('trust proxy', 'loopback, linklocal, uniquelocal');
app.use('/public', express.static(path.join(__dirname, 'public')));
app.use('/fonts', express.static(path.join(__dirname, 'public/fonts')));
if (process.env.RDB_HOST) {
js = 'app.js'
var root = __dirname + '/src/static/index.html'
var html = fs.readFileSync(root).toString('utf-8');
var analytics = '', drift = '';
if (config.analytics && config.analytics.piwik && config.analytics.piwik.enabled) {
analytics = fs.readFileSync(__dirname + '/src/static/piwik.tmpl').toString('utf-8');
analytics = analytics.replace(new RegExp('__ENDPOINT__', 'g'), config.analytics.piwik.endpoint);
analytics = analytics.replace(new RegExp('__DOMAIN__', 'g'), config.analytics.piwik.domain);
analytics = analytics.replace(new RegExp('__SITEID__', 'g'), config.analytics.piwik.siteId);
html = html.replace('__ANALYTICS__', analytics);
}else{
html = html.replace('__ANALYTICS__', '');
}
if (config.analytics && config.analytics.drift) {
drift = fs.readFileSync(__dirname + '/src/static/drift.tmpl').toString('utf-8');
drift = drift.replace('__DRIFT_VERSION__', config.analytics.drift.version);
drift = drift.replace('__DRIFT_ID__', config.analytics.drift.id);
html = html.replace('__DRIFT__', drift);
}else{
html = html.replace('__DRIFT__', '');
}
html = html.replace(new RegExp('__JS__', 'g'), js)
app.use('/*', function (req, res, next) {
res.setHeader('content-type', 'text/html');
return res.sendFile(html);
});
} else {
app.use('/*', function (req, res, next) {
res.setHeader('content-type', 'text/html');
return res.sendFile(path.join(__dirname, 'public/index.html'));
});
}
app.get('/version.json', function(req, res, next) {
return res.json(version);
});
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.send({
message: err.message,
error: {}
});
});
return app;
};