From b5c8775c817324e57008c3e68b947075df4e0df9 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 17 Feb 2017 21:16:07 +0000 Subject: [PATCH] Fix strange issues with import and array checks --- src/cli.ts | 36 ++++++++++++++++++++++++++++++++++ src/cli.txt | 15 -------------- src/index.ts | 22 ++------------------- src/middleware/basic-auth.ts | 2 +- src/middleware/logging.ts | 4 ++-- src/middleware/static-files.ts | 6 +++--- src/server.ts | 25 +++++++++++------------ tsconfig.json | 3 ++- 8 files changed, 59 insertions(+), 54 deletions(-) create mode 100644 src/cli.ts delete mode 100644 src/cli.txt diff --git a/src/cli.ts b/src/cli.ts new file mode 100644 index 0000000..23b51e0 --- /dev/null +++ b/src/cli.ts @@ -0,0 +1,36 @@ +import { docopt } from 'docopt'; +import { Options } from './types'; + +const PKG = require('../package.json'); + +const ARG_DATA = ` +${PKG.name}. +${PKG.description} + +Usage: + tstatic [options] + tstatic -h | --help + tstatic --version + +Options: + -h --help Show this screen. + --version Show version. + -b --basic-auth= Enable basic-auth. + -i --ips= Allowed IP addresses. + -l --list-dir List Directory. + -o --opbeat Enable Opbeat. +`; + +export default function getArgs() : Options { + const rawArgs = docopt(ARG_DATA, { + version: PKG.version, + help: true + }); + return { + allowed_ips: rawArgs['--ips'] ? rawArgs['--ips'].split(',') : [], + basicAuth: rawArgs['--basic-auth'] ? rawArgs['--basic-auth'].split(':') : [], + dirList: rawArgs['--list-dir'], + serveDir: rawArgs[''], + opbeat: rawArgs['--opbeat'] + }; +} diff --git a/src/cli.txt b/src/cli.txt deleted file mode 100644 index aa03abd..0000000 --- a/src/cli.txt +++ /dev/null @@ -1,15 +0,0 @@ -tstatic. - -Usage: - tstatic [options] - tstatic -h | --help - tstatic --version - -Options: - -h --help Show this screen. - --version Show version. - -b --basic-auth= Enable basic-auth. - -i --ips= Allowed IP addresses. - -l --list-dir List Directory. - -o --opbeat Enable Opbeat. - diff --git a/src/index.ts b/src/index.ts index 60abd5c..6d40342 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,26 +1,8 @@ import { docopt } from 'docopt'; -import { readFileSync } from 'fs'; -import { join } from 'path'; -import { Options } from './types'; import createServer from './server'; +import getArgs from './cli'; -const ARG_DATA = readFileSync(join(__dirname, '..', 'src', 'cli.txt')).toString(); - -function getArgs() : Options { - const rawArgs = docopt(ARG_DATA, { - version: require('../package.json').version, - help: true - }); - return { - allowed_ips: rawArgs['--ips'] ? rawArgs['--ips'].split(',') : [], - basicAuth: rawArgs['--basic-auth'] ? rawArgs['--basic-auth'].split(':') : [], - dirList: rawArgs['--list-dir'], - serveDir: rawArgs[''], - opbeat: rawArgs['--opbeat'] - } -} - - +console.log("Starting Server...") const app = createServer(getArgs()); const server = app.listen(5000, function () { diff --git a/src/middleware/basic-auth.ts b/src/middleware/basic-auth.ts index 12dc09b..e31f729 100644 --- a/src/middleware/basic-auth.ts +++ b/src/middleware/basic-auth.ts @@ -1,4 +1,4 @@ -import basicAuth from 'express-basic-auth'; +import * as basicAuth from 'express-basic-auth'; export default function basicAuthHandler(username : string, password : string) { return basicAuth({ diff --git a/src/middleware/logging.ts b/src/middleware/logging.ts index c30a728..49e3809 100644 --- a/src/middleware/logging.ts +++ b/src/middleware/logging.ts @@ -1,5 +1,5 @@ -import winston from 'winston'; -import expressWinston from 'express-winston'; +import * as winston from 'winston'; +import * as expressWinston from 'express-winston'; export default expressWinston.logger({ transports: [ diff --git a/src/middleware/static-files.ts b/src/middleware/static-files.ts index ea65d58..4430ff5 100644 --- a/src/middleware/static-files.ts +++ b/src/middleware/static-files.ts @@ -1,12 +1,12 @@ -import express, { Request, Response } from 'express'; -import serveIndex from 'serve-index'; +import * as express from 'express'; +import * as serveIndex from 'serve-index'; import path from 'path'; function isDirectory(url : string) : boolean { return /\/$/.test(url); } -export function indexHandle(request : Request, response : Response, next : Function) { +export function indexHandle(request : express.Request, response : express.Response, next : Function) { if (isDirectory(request.url)) { request.url = path.join(request.url, 'index.html'); } diff --git a/src/server.ts b/src/server.ts index 81de56d..624d84c 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,9 +1,9 @@ -import express, { Application } from 'express'; +import * as express from 'express'; -import AccessControl from 'express-ip-access-control'; -import compression from 'compression'; -import helmet from 'helmet'; -import opbeat from 'opbeat'; +import * as AccessControl from 'express-ip-access-control'; +import * as compression from 'compression'; +import * as helmet from 'helmet'; +import * as opbeat from 'opbeat'; import logging from './middleware/logging'; import basicAuthHandler from './middleware/basic-auth'; @@ -12,15 +12,12 @@ import handle404 from './middleware/404'; import { Options } from './types'; -export default function createServer(opts : Options) : Application { +export default function createServer(opts : Options) : express.Application { const app = express(); - const opbeatHandle = opbeat.start({ - active: opts.opbeat - }); app.use(logging); - if (opts.allowed_ips) { + if (opts.allowed_ips.length) { app.set('trust proxy', true); app.use(AccessControl({ mode: 'allow', @@ -29,7 +26,7 @@ export default function createServer(opts : Options) : Application { })); } - if (opts.basicAuth) { + if (opts.basicAuth.length) { app.use(basicAuthHandler(opts.basicAuth[0], opts.basicAuth[1])); } @@ -44,7 +41,11 @@ export default function createServer(opts : Options) : Application { app.use(compression({ level: 9 })); app.use(helmet()); - app.use(opbeatHandle.middleware.express()); + if (opts.opbeat) { + app.use(opbeat.start({ + active: opts.opbeat + }).middleware.express()); + } return app; } diff --git a/tsconfig.json b/tsconfig.json index 5d404e6..13a0150 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,8 @@ "experimentalDecorators": true, "preserveConstEnums": true, "allowJs": true, - "sourceMap": true + "sourceMap": true, + "pretty": true }, "filesGlob": [ "typings/index.d.ts",