From 2355da231e0f74a2e5574d2dd36b0c86abcd1b48 Mon Sep 17 00:00:00 2001 From: Ivar Date: Sun, 1 May 2016 18:20:10 +0200 Subject: [PATCH] Start injecting config to app. --- app.js | 54 ++++++++++++++++++---------------------------- server.js | 23 +++++++++++++++++++- test/specHelper.js | 10 ++++----- 3 files changed, 48 insertions(+), 39 deletions(-) diff --git a/app.js b/app.js index 3f236aeed8..d6cb006f80 100644 --- a/app.js +++ b/app.js @@ -2,50 +2,38 @@ var express = require('express'); var favicon = require('serve-favicon'); var bodyParser = require('body-parser'); var cookieParser = require('cookie-parser'); +var validator = require('express-validator'); var log4js = require('log4js'); var logger = require('./lib/logger'); var routes = require('./lib/routes'); -var validator = require('express-validator'); -var app = express(); -var router = express.Router(); // eslint-disable-line -var baseUriPath = process.env.BASE_URI_PATH || ''; -if (app.get('env') === 'development') { - app.use(require('errorhandler')()); +module.exports = function(config) { + var app = express(); + var router = express.Router(); // eslint-disable-line + var baseUriPath = config.baseUriPath || ''; - var webpack = require('webpack'), - webpackDevMiddleware = require('webpack-dev-middleware'), - webpackConfig = require('./webpack.config'), - compiler = webpack(webpackConfig), - config = { - publicPath: '/js', - noInfo: true - }; + app.use(favicon(__dirname + '/public/favicon.ico')); - app.use(baseUriPath, webpackDevMiddleware(compiler, config)); -} + app.use(validator([])); -app.use(favicon(__dirname + '/public/favicon.ico')); + app.set('trust proxy'); + app.locals.baseUriPath = baseUriPath; -app.use(validator([])); + app.use(log4js.connectLogger(logger, { + format: ':remote-addr :status :method :url :response-timems', + level: 'auto' // 3XX=WARN, 4xx/5xx=ERROR + })); -app.set('trust proxy'); -app.locals.baseUriPath = baseUriPath; + app.set('port', config.port); -app.use(log4js.connectLogger(logger, { - format: ':remote-addr :status :method :url :response-timems', - level: 'auto' // 3XX=WARN, 4xx/5xx=ERROR -})); + app.use(baseUriPath, express.static(__dirname + '/public')); + app.use(bodyParser.json({ strict: false })); -app.set('port', process.env.HTTP_PORT || process.env.PORT || 4242); + app.use(cookieParser()); -app.use(baseUriPath, express.static(__dirname + '/public')); -app.use(bodyParser.json({ strict: false })); + routes.create(router); -app.use(cookieParser()); + app.use(baseUriPath, router); -routes.create(router); - -app.use(baseUriPath, router); - -module.exports = app; + return app; +}; diff --git a/server.js b/server.js index 238ac698f7..8590411f1b 100644 --- a/server.js +++ b/server.js @@ -1,10 +1,31 @@ -var app = require('./app'); var logger = require('./lib/logger'); +var config = { + baseUriPath: process.env.BASE_URI_PATH || '', + port: process.env.HTTP_PORT || process.env.PORT || 4242 +}; + +var app = require('./app')(config); + var server = app.listen(app.get('port'), function() { logger.info('unleash started on ' + app.get('port')); }); +if (app.get('env') === 'development') { + app.use(require('errorhandler')()); + + var webpack = require('webpack'); + var webpackDevMiddleware = require('webpack-dev-middleware'); + var webpackConfig = require('./webpack.config'); + var compiler = webpack(webpackConfig); + + app.use(config.baseUriPath, webpackDevMiddleware(compiler, { + publicPath: '/js', + noInfo: true + })); +} + + process.on('uncaughtException', function(err) { logger.error('Uncaught Exception:', err.message); logger.error(err.stack); diff --git a/test/specHelper.js b/test/specHelper.js index 65db1d5882..02a6563481 100644 --- a/test/specHelper.js +++ b/test/specHelper.js @@ -1,11 +1,11 @@ 'use strict'; process.env.NODE_ENV = 'test'; -var Promise = require('bluebird'); -var request = require('supertest'); -var app = require('../app'); -var knex = require('../lib/db/dbPool'); -var featureDb = require('../lib/db/feature'); +var Promise = require('bluebird'); +var request = require('supertest'); +var app = require('../app')({ baseUriPath: '' }); +var knex = require('../lib/db/dbPool'); +var featureDb = require('../lib/db/feature'); var strategyDb = require('../lib/db/strategy'); Promise.promisifyAll(request);