diff --git a/src/404.js b/src/404.js deleted file mode 100644 index c2675a8..0000000 --- a/src/404.js +++ /dev/null @@ -1,9 +0,0 @@ -const staticFile = require('connect-static-file'); -const path = require('path'); -const { SERVE_DIR } = require('./consts'); - -const handle404 = staticFile(path.join(SERVE_DIR, '.404.html')); -module.exports = function (request, response, next) { - response.statusCode = 404; - return handle404(request, response, next); -}; diff --git a/src/basic-auth.js b/src/basic-auth.js deleted file mode 100644 index 2a22837..0000000 --- a/src/basic-auth.js +++ /dev/null @@ -1,17 +0,0 @@ -const basicAuth = require('express-basic-auth'); -const { BASIC_AUTH_ENABLED } = require('./consts'); - -function basicAuthHandler(username, password) { - return process.env.BASIC_AUTH_USERNAME === username && process.env.BASIC_AUTH_PASSWORD === password; -} - -if (BASIC_AUTH_ENABLED) { - module.exports = basicAuth({ - authorizer: basicAuthHandler, - challenge: true - }); -} else { - module.exports = (req, res, next) => next(); -} - - diff --git a/src/consts.js b/src/consts.js deleted file mode 100644 index 7d8f464..0000000 --- a/src/consts.js +++ /dev/null @@ -1,11 +0,0 @@ -const IN_TEST = process.env.NODE_ENV === 'test'; - -module.exports = { - SERVE_DIR: IN_TEST ? 'site/' : process.argv[process.argv.length - 1], - PORT: process.env.PORT || 5000, - ALLOWED_IPS: process.env.ALLOWED_IPS ? process.env.ALLOWED_IPS.split(',') : undefined, - IN_TEST, - IN_PRODUCTION: process.env.NODE_ENV === 'production', - DIR_LIST: process.env.DIR_LIST, - BASIC_AUTH_ENABLED: process.env.BASIC_AUTH_USERNAME && process.env.BASIC_AUTH_PASSWORD -}; diff --git a/src/logging.js b/src/logging.js deleted file mode 100644 index dba9130..0000000 --- a/src/logging.js +++ /dev/null @@ -1,17 +0,0 @@ -const winston = require('winston'); -const expressWinston = require('express-winston'); - -module.exports = expressWinston.logger({ - transports: [ - new winston.transports.Console({ - colorize: true - }) - ], - meta: false, - msg: '{{ req.url }} ' - .concat('status:{{ res.statusCode }} ') - .concat('useragent:{{ req.headers["user-agent"] }} ') - .concat('time:{{ res.responseTime }}ms'), - colorize: true, - statusLevels: true -}); diff --git a/src/server.js b/src/server.js deleted file mode 100755 index 10da450..0000000 --- a/src/server.js +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/env node - -console.log('Starting Server...'); - -const app = require('express')(); -const consts = require('./consts'); - -const compression = require('compression'); -const helmet = require('helmet'); -const serveIndex = require('serve-index'); -const AccessControl = require('express-ip-access-control'); -const opbeat = require('opbeat').start({ - active: consts.IN_PRODUCTION -}); - -const logging = require('./logging'); -const staticFiles = require('./static-files'); -const handle404 = require('./404'); -const basicAuth = require('./basic-auth'); - -if (consts.ALLOWED_IPS) { - app.set('trust proxy', true); - app.use(AccessControl({ - mode: 'allow', - allows: consts.ALLOWED_IPS, - statusCode: 404 - })); -} - -// Custom Middleware -app.use(logging); -app.use(basicAuth); - -if (consts.DIR_LIST) { - app.use(serveIndex(consts.SERVE_DIR, { - icons: true - })); -} else { - app.use(staticFiles.indexHandle); -} - -app.use(staticFiles.static); -app.use(handle404); - -// Library -app.use(compression({ level: 9 })); -app.use(helmet()); -app.use(opbeat.middleware.express()); - -const server = app.listen(consts.PORT, function () { - console.log('Server started on ' + server.address().port); -}); - -module.exports = server; diff --git a/src/static-files.js b/src/static-files.js deleted file mode 100644 index 773add7..0000000 --- a/src/static-files.js +++ /dev/null @@ -1,16 +0,0 @@ -const express = require('express'); -const path = require('path'); -const { SERVE_DIR } = require('./consts'); - -module.exports.indexHandle = function (request, response, next) { - if (request.url.endsWith('/')) { - request.url = path.join(request.url, 'index.html'); - } - next(); -}; - -module.exports.static = express.static(SERVE_DIR, { - dotfiles: 'ignore', - index: false, - redirect: true -}); diff --git a/tests/server.test.js b/tests/server.test.js deleted file mode 100644 index d9cca10..0000000 --- a/tests/server.test.js +++ /dev/null @@ -1,42 +0,0 @@ -const request = require('supertest'); -const fs = require('fs'); - - -describe('Server', function () { - var server; - before(function () { - server = require('../src/server'); - }); - - after(function () { - server.close(); - }); - - it('responds to /', function (done) { - request(server) - .get('/') - .expect(200, done); - }); - - it('returns 404 on bad path', function (done) { - request(server) - .get('/foo/bar') - .expect(404, done); - }); - - describe('index route', function () { - const body = fs.readFileSync(__dirname + '/../site/index.html').toString(); - - it('should render /index.html', function (done) { - request(server) - .get('/index.html') - .expect(200, body, done); - }); - - it('should render /', function (done) { - request(server) - .get('/') - .expect(200, body, done); - }); - }); -});