Create rest of server

This commit is contained in:
Jake Howard 2017-02-15 20:46:18 +00:00
parent 9cbae3b8a1
commit 1062c7bfa6
5 changed files with 47 additions and 2 deletions

View file

@ -1,4 +1,5 @@
import express, { Request, Response } from 'express';
import serveIndex from 'serve-index';
import path from 'path';
function isDirectory(url : string) : boolean {
@ -12,7 +13,6 @@ export function indexHandle(request : Request, response : Response, next : Funct
return next();
}
export function staticFileHandle(serveDir : string) {
return express.static(serveDir, {
dotfiles: 'ignore',
@ -20,3 +20,9 @@ export function staticFileHandle(serveDir : string) {
redirect: true
});
}
export function serveIndexHandle(serveDir : string) {
return serveIndex(serveDir, {
icons: true
});
}

View file

@ -1,11 +1,24 @@
import express from 'express';
import AccessControl from 'express-ip-access-control';
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';
export default function createServer(opts : Options) {
const app = express();
const opbeatHandle = opbeat.start({
active: opts.opbeat
});
app.use(logging);
if (opts.allowed_ips) {
app.set('trust proxy', true);
@ -16,5 +29,23 @@ export default function createServer(opts : Options) {
}));
}
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;
}

View file

@ -5,3 +5,4 @@ declare module 'connect-static-file';
declare module 'express-basic-auth';
declare module 'winston'; // doesnt like console transport
declare module 'express-winston';
declare module 'opbeat';

View file

@ -1,4 +1,8 @@
export interface Options {
allowed_ips: string[];
basicAuth: string;
dirList: boolean;
serveDir: string;
opbeat: boolean;
}

View file

@ -1,7 +1,10 @@
{
"dependencies": {
"compression": "registry:dt/compression#0.0.0+20160725212620",
"debug": "registry:npm/debug#2.0.0+20160723033700",
"express": "registry:npm/express#4.14.0+20160925001530"
"express": "registry:npm/express#4.14.0+20160925001530",
"helmet": "registry:dt/helmet#0.0.0+20161005184000",
"serve-index": "registry:dt/serve-index#1.7.2+20160428043022"
},
"globalDependencies": {
"node": "registry:dt/node#7.0.0+20170204020307"