2017-01-19 17:31:23 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
console.log('Starting Server...');
|
|
|
|
|
|
|
|
const app = require('express')();
|
2017-01-21 21:06:21 +00:00
|
|
|
const consts = require('./consts');
|
2017-01-19 17:31:23 +00:00
|
|
|
|
|
|
|
const compression = require('compression');
|
|
|
|
const helmet = require('helmet');
|
2017-02-13 08:50:51 +00:00
|
|
|
const serveIndex = require('serve-index');
|
2017-02-13 09:49:32 +00:00
|
|
|
const AccessControl = require('express-ip-access-control');
|
2017-01-19 17:31:23 +00:00
|
|
|
const opbeat = require('opbeat').start({
|
2017-01-21 21:06:21 +00:00
|
|
|
active: consts.IN_PRODUCTION
|
2017-01-19 17:31:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const logging = require('./logging');
|
|
|
|
const staticFiles = require('./static-files');
|
|
|
|
const handle404 = require('./404');
|
2017-01-21 21:06:21 +00:00
|
|
|
const basicAuth = require('./basic-auth');
|
2017-01-19 17:31:23 +00:00
|
|
|
|
2017-02-13 09:49:32 +00:00
|
|
|
if (consts.ALLOWED_IPS) {
|
|
|
|
app.set('trust proxy', true);
|
|
|
|
|
|
|
|
app.use(AccessControl({
|
|
|
|
mode: 'allow',
|
|
|
|
allows: consts.ALLOWED_IPS,
|
|
|
|
forceConnectionAddress: true,
|
|
|
|
statusCode: 404
|
|
|
|
}));
|
|
|
|
}
|
2017-01-19 17:31:23 +00:00
|
|
|
// Custom Middleware
|
|
|
|
app.use(logging);
|
2017-01-21 21:06:21 +00:00
|
|
|
app.use(basicAuth);
|
2017-02-13 08:50:51 +00:00
|
|
|
|
|
|
|
if (consts.DIR_LIST) {
|
|
|
|
app.use(serveIndex(consts.SERVE_DIR, {
|
|
|
|
icons: true
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
app.use(staticFiles.indexHandle);
|
|
|
|
}
|
|
|
|
|
2017-01-19 17:31:23 +00:00
|
|
|
app.use(staticFiles.static);
|
|
|
|
app.use(handle404);
|
|
|
|
|
|
|
|
// Library
|
|
|
|
app.use(compression({ level: 9 }));
|
|
|
|
app.use(helmet());
|
|
|
|
app.use(opbeat.middleware.express());
|
|
|
|
|
2017-01-21 21:06:21 +00:00
|
|
|
const server = app.listen(consts.PORT, function () {
|
2017-01-19 17:31:23 +00:00
|
|
|
console.log('Server started on ' + server.address().port);
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = server;
|