Fix strange issues with import and array checks

This commit is contained in:
Jake Howard 2017-02-17 21:16:07 +00:00
parent 2c401c1535
commit b5c8775c81
8 changed files with 59 additions and 54 deletions

36
src/cli.ts Normal file
View file

@ -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 <dir> [options]
tstatic -h | --help
tstatic --version
Options:
-h --help Show this screen.
--version Show version.
-b <auth> --basic-auth=<auth> Enable basic-auth.
-i <ips> --ips=<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['<dir>'],
opbeat: rawArgs['--opbeat']
};
}

View file

@ -1,15 +0,0 @@
tstatic.
Usage:
tstatic <dir> [options]
tstatic -h | --help
tstatic --version
Options:
-h --help Show this screen.
--version Show version.
-b <auth> --basic-auth=<auth> Enable basic-auth.
-i <ips> --ips=<ips> Allowed IP addresses.
-l --list-dir List Directory.
-o --opbeat Enable Opbeat.

View file

@ -1,26 +1,8 @@
import { docopt } from 'docopt'; import { docopt } from 'docopt';
import { readFileSync } from 'fs';
import { join } from 'path';
import { Options } from './types';
import createServer from './server'; import createServer from './server';
import getArgs from './cli';
const ARG_DATA = readFileSync(join(__dirname, '..', 'src', 'cli.txt')).toString(); console.log("Starting Server...")
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['<dir>'],
opbeat: rawArgs['--opbeat']
}
}
const app = createServer(getArgs()); const app = createServer(getArgs());
const server = app.listen(5000, function () { const server = app.listen(5000, function () {

View file

@ -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) { export default function basicAuthHandler(username : string, password : string) {
return basicAuth({ return basicAuth({

View file

@ -1,5 +1,5 @@
import winston from 'winston'; import * as winston from 'winston';
import expressWinston from 'express-winston'; import * as expressWinston from 'express-winston';
export default expressWinston.logger({ export default expressWinston.logger({
transports: [ transports: [

View file

@ -1,12 +1,12 @@
import express, { Request, Response } from 'express'; import * as express from 'express';
import serveIndex from 'serve-index'; import * as serveIndex from 'serve-index';
import path from 'path'; import path from 'path';
function isDirectory(url : string) : boolean { function isDirectory(url : string) : boolean {
return /\/$/.test(url); 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)) { if (isDirectory(request.url)) {
request.url = path.join(request.url, 'index.html'); request.url = path.join(request.url, 'index.html');
} }

View file

@ -1,9 +1,9 @@
import express, { Application } from 'express'; import * as express from 'express';
import AccessControl from 'express-ip-access-control'; import * as AccessControl from 'express-ip-access-control';
import compression from 'compression'; import * as compression from 'compression';
import helmet from 'helmet'; import * as helmet from 'helmet';
import opbeat from 'opbeat'; import * as opbeat from 'opbeat';
import logging from './middleware/logging'; import logging from './middleware/logging';
import basicAuthHandler from './middleware/basic-auth'; import basicAuthHandler from './middleware/basic-auth';
@ -12,15 +12,12 @@ import handle404 from './middleware/404';
import { Options } from './types'; import { Options } from './types';
export default function createServer(opts : Options) : Application { export default function createServer(opts : Options) : express.Application {
const app = express(); const app = express();
const opbeatHandle = opbeat.start({
active: opts.opbeat
});
app.use(logging); app.use(logging);
if (opts.allowed_ips) { if (opts.allowed_ips.length) {
app.set('trust proxy', true); app.set('trust proxy', true);
app.use(AccessControl({ app.use(AccessControl({
mode: 'allow', 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])); 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(compression({ level: 9 }));
app.use(helmet()); app.use(helmet());
app.use(opbeatHandle.middleware.express()); if (opts.opbeat) {
app.use(opbeat.start({
active: opts.opbeat
}).middleware.express());
}
return app; return app;
} }

View file

@ -7,7 +7,8 @@
"experimentalDecorators": true, "experimentalDecorators": true,
"preserveConstEnums": true, "preserveConstEnums": true,
"allowJs": true, "allowJs": true,
"sourceMap": true "sourceMap": true,
"pretty": true
}, },
"filesGlob": [ "filesGlob": [
"typings/index.d.ts", "typings/index.d.ts",