This repository was archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathhttp_server.js
More file actions
78 lines (63 loc) · 2.26 KB
/
http_server.js
File metadata and controls
78 lines (63 loc) · 2.26 KB
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
/* this file is being used to serve the files from /dist folder. It is being used by heroku */
const express = require('express');
const serveStatic = require('serve-static');
const compression = require('compression');
const serveIndex = require('serve-index');
// import CORS config
const headerConfig = require('./etc/headerConfig');
const whitelist = require('./etc/whitelist');
// Application setup.
const port = process.argv[2] || process.env.PORT || 8080;
let CACHE_DURATION = '10m';
let DOCUMENT_ROOT = __dirname + '/dist';
let domainWhitelist = function (req, res, next) {
let host = req.header("host") || req.header("Host");
if (isHostInWhitelist(host)) {
next();
} else {
res
.status(403)
.send('Host not allowed! Please contact administrator.');
}
};
function isHostInWhitelist(host) {
let split = host.split('.');
let length = split.length;
if (length >= 2) {
let domainNameWithEnding = split[length - 2] + '.' + split[length - 1];
return whitelist.whitelist.includes(domainNameWithEnding);
} else if (host.startsWith("localhost") || host.startsWith("oilsite") || host.startsWith("oilcdn")) {
return true;
}
return false;
}
let additionalHeaders = function (req, res, next) {
//res.header('Content-Security-Policy', 'script-src \'self\' *');
for (let key in headerConfig.headers) {
// skip loop if the property is from prototype
if (!headerConfig.headers.hasOwnProperty(key)) continue;
// copy header config
let object = headerConfig.headers[key];
res.header(key, object);
}
next();
};
/*
* start server
*/
let app = express();
app.use(domainWhitelist);
app.use(additionalHeaders);
// server gzip
app.use(compression());
// Serve directory indexes folder (with icons)
app.use('/release', serveIndex('release', {'icons': true}));
app.use('/examples', serveIndex('dist/examples', {'icons': true}));
app.use('/demos', serveIndex('dist/demos', {'icons': true}));
// static with cache headers
app.use(serveStatic(DOCUMENT_ROOT, {maxAge: CACHE_DURATION, cacheControl: true}));
app.use('/devExamples', express.static('src/examples'));
app.use('/devExamples', serveIndex('src/examples', {'icons': true}));
console.log('server is now starting on port ', port);
app.listen(port, '0.0.0.0');
module.exports = app;