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.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

2017-02-17 09:24:29 +00:00
import express, { Application } from 'express';
import AccessControl from 'express-ip-access-control';
2017-02-15 20:46:18 +00:00
import compression from 'compression';
import helmet from 'helmet';
import opbeat from 'opbeat';
import logging from './middleware/logging';
import basicAuthHandler from './middleware/basic-auth';
import { serveIndexHandle, indexHandle, staticFileHandle } from './middleware/static-files';
import handle404 from './middleware/404';
import { Options } from './types';
2017-02-17 09:24:29 +00:00
export default function createServer(opts : Options) : Application {
const app = express();
2017-02-15 20:46:18 +00:00
const opbeatHandle = opbeat.start({
active: opts.opbeat
});
app.use(logging);
if (opts.allowed_ips) {
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-15 20:46:18 +00:00
if (opts.basicAuth) {
const credentials = opts.basicAuth.split(':');
app.use(basicAuthHandler(credentials[0], credentials[1]));
}
if (opts.dirList) {
app.use(serveIndexHandle(opts.serveDir));
} else {
app.use(indexHandle);
}
app.use(staticFileHandle(opts.serveDir));
app.use(handle404);
app.use(compression({ level: 9 }));
app.use(helmet());
app.use(opbeatHandle.middleware.express());
return app;
}