This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
tstatic/src/server.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

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,
statusCode: 404
}));
}
2017-02-13 13:39:26 +00:00
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;