From 8f2709ab556ef45c0dbdc8651e370e0288de70f4 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 14 Feb 2017 22:11:07 +0000 Subject: [PATCH] Add basic structure and start server code --- package.json | 6 +++--- src/server.ts | 20 ++++++++++++++++++++ src/types/fakes.d.ts | 3 +++ src/types/index.ts | 4 ++++ tsconfig.json | 26 ++++++++++++++++++++++++++ typings.json | 9 +++++++++ 6 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/server.ts create mode 100644 src/types/fakes.d.ts create mode 100644 src/types/index.ts create mode 100644 tsconfig.json create mode 100644 typings.json diff --git a/package.json b/package.json index b1e3cd0..2b835fc 100644 --- a/package.json +++ b/package.json @@ -2,12 +2,12 @@ "name": "tstatic", "version": "1.0.0", "description": "Container to host simple static applications using a node server, so files can be deployed using rsync", - "main": "./src/server.js", + "main": "node ./dist/server.js", "bin": { - "tstatic": "./src/server.js" + "tstatic": "node ./dist/server.js" }, "scripts": { - "start": "./src/server.js site/", + "start": "node ./dist/server.js site/", "postinstall": "typings install", "build": "tsc" }, diff --git a/src/server.ts b/src/server.ts new file mode 100644 index 0000000..1a48488 --- /dev/null +++ b/src/server.ts @@ -0,0 +1,20 @@ +import express from 'express'; + +import AccessControl from 'express-ip-access-control'; + +import { Options } from './types'; + +export default function createServer(opts : Options) { + const app = express(); + + if (opts.allowed_ips) { + app.set('trust proxy', true); + app.use(AccessControl({ + mode: 'allow', + allows: opts.allowed_ips, + statusCode: 404 + })); + } + + return app; +} diff --git a/src/types/fakes.d.ts b/src/types/fakes.d.ts new file mode 100644 index 0000000..45d03bd --- /dev/null +++ b/src/types/fakes.d.ts @@ -0,0 +1,3 @@ +/* Mock types that dont exist */ + +declare module 'express-ip-access-control'; diff --git a/src/types/index.ts b/src/types/index.ts new file mode 100644 index 0000000..c17f545 --- /dev/null +++ b/src/types/index.ts @@ -0,0 +1,4 @@ + +export interface Options { + allowed_ips: string[]; +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..5d404e6 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,26 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src", + "allowSyntheticDefaultImports": true, + "noImplicitAny": true, + "experimentalDecorators": true, + "preserveConstEnums": true, + "allowJs": true, + "sourceMap": true + }, + "filesGlob": [ + "typings/index.d.ts", + "src/**/*.ts" + ], + "exclude": [ + "dist", + "node_modules" + ], + "typeRoots": [ + "node_modules", + "typings", + "src/types" + ], + "compileOnSave": false +} diff --git a/typings.json b/typings.json new file mode 100644 index 0000000..f4ed3f6 --- /dev/null +++ b/typings.json @@ -0,0 +1,9 @@ +{ + "dependencies": { + "debug": "registry:npm/debug#2.0.0+20160723033700", + "express": "registry:npm/express#4.14.0+20160925001530" + }, + "globalDependencies": { + "node": "registry:dt/node#7.0.0+20170204020307" + } +}