2017-02-17 21:16:07 +00:00
|
|
|
import * as express from 'express';
|
2017-02-14 22:11:07 +00:00
|
|
|
|
2017-02-17 21:16:07 +00:00
|
|
|
import * as AccessControl from 'express-ip-access-control';
|
|
|
|
import * as compression from 'compression';
|
|
|
|
import * as helmet from 'helmet';
|
|
|
|
import * as opbeat from 'opbeat';
|
2017-02-15 20:46:18 +00:00
|
|
|
|
|
|
|
import logging from './middleware/logging';
|
|
|
|
import basicAuthHandler from './middleware/basic-auth';
|
|
|
|
import { serveIndexHandle, indexHandle, staticFileHandle } from './middleware/static-files';
|
|
|
|
import handle404 from './middleware/404';
|
2017-02-14 22:11:07 +00:00
|
|
|
|
|
|
|
import { Options } from './types';
|
|
|
|
|
2017-02-17 21:16:07 +00:00
|
|
|
export default function createServer(opts : Options) : express.Application {
|
2017-02-14 22:11:07 +00:00
|
|
|
const app = express();
|
2017-02-15 20:46:18 +00:00
|
|
|
|
2017-05-06 22:59:29 +01:00
|
|
|
app.disable('x-powered-by');
|
2017-02-19 18:19:45 +00:00
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
|
|
app.use(logging);
|
|
|
|
}
|
2017-02-14 22:11:07 +00:00
|
|
|
|
2017-02-17 21:16:07 +00:00
|
|
|
if (opts.allowed_ips.length) {
|
2017-02-14 22:11:07 +00:00
|
|
|
app.set('trust proxy', true);
|
|
|
|
app.use(AccessControl({
|
|
|
|
mode: 'allow',
|
|
|
|
allows: opts.allowed_ips,
|
|
|
|
statusCode: 404
|
|
|
|
}));
|
|
|
|
}
|
2017-02-14 22:21:08 +00:00
|
|
|
|
2017-02-17 21:16:07 +00:00
|
|
|
if (opts.basicAuth.length) {
|
2017-02-17 20:27:30 +00:00
|
|
|
app.use(basicAuthHandler(opts.basicAuth[0], opts.basicAuth[1]));
|
2017-02-15 20:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.dirList) {
|
|
|
|
app.use(serveIndexHandle(opts.serveDir));
|
|
|
|
} else {
|
|
|
|
app.use(indexHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
app.use(staticFileHandle(opts.serveDir));
|
2017-02-19 21:53:06 +00:00
|
|
|
app.use(handle404(opts.serveDir));
|
2017-02-15 20:46:18 +00:00
|
|
|
|
|
|
|
app.use(compression({ level: 9 }));
|
|
|
|
app.use(helmet());
|
2017-02-17 21:16:07 +00:00
|
|
|
if (opts.opbeat) {
|
|
|
|
app.use(opbeat.start({
|
|
|
|
active: opts.opbeat
|
|
|
|
}).middleware.express());
|
|
|
|
}
|
2017-02-15 20:46:18 +00:00
|
|
|
|
2017-02-14 22:11:07 +00:00
|
|
|
return app;
|
|
|
|
}
|