From 1062c7bfa6add3c0fc91a79fd2db8a622f1bb41c Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 15 Feb 2017 20:46:18 +0000 Subject: [PATCH] Create rest of server --- src/middleware/static-files.ts | 8 +++++++- src/server.ts | 31 +++++++++++++++++++++++++++++++ src/types/fakes.d.ts | 1 + src/types/index.ts | 4 ++++ typings.json | 5 ++++- 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/middleware/static-files.ts b/src/middleware/static-files.ts index 4642a47..ea65d58 100644 --- a/src/middleware/static-files.ts +++ b/src/middleware/static-files.ts @@ -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 + }); +} diff --git a/src/server.ts b/src/server.ts index fc610b7..6903366 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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; } diff --git a/src/types/fakes.d.ts b/src/types/fakes.d.ts index c2a77d8..2665eeb 100644 --- a/src/types/fakes.d.ts +++ b/src/types/fakes.d.ts @@ -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'; diff --git a/src/types/index.ts b/src/types/index.ts index c17f545..140eec7 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,8 @@ export interface Options { allowed_ips: string[]; + basicAuth: string; + dirList: boolean; + serveDir: string; + opbeat: boolean; } diff --git a/typings.json b/typings.json index f4ed3f6..86fe7f9 100644 --- a/typings.json +++ b/typings.json @@ -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"