From 833e148ad9989dd7e5f5ecbb04fb174a60fcd1f8 Mon Sep 17 00:00:00 2001 From: sveisvei Date: Thu, 16 Jun 2016 15:44:03 +0200 Subject: [PATCH] make it possible to require server --- index.js | 3 +++ package.json | 5 ++-- server.js | 75 +++++++++++++++++++++++++++++----------------------- 3 files changed, 48 insertions(+), 35 deletions(-) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000000..14a7724fe2 --- /dev/null +++ b/index.js @@ -0,0 +1,3 @@ +var server = require('./server'); + +server.start({}); diff --git a/package.json b/package.json index de9bc6abe4..aac566f66a 100644 --- a/package.json +++ b/package.json @@ -18,12 +18,13 @@ "engines": { "node": ">=4.4.3" }, + "main": "./server.js", "private": true, "scripts": { "db-migrate-and-start": "npm run db-migrate && npm run start", - "start": "NODE_ENV=production node server.js", + "start": "NODE_ENV=production node index.js", "build": "./node_modules/.bin/webpack -p", - "dev": "NODE_ENV=development supervisor --ignore ./node_modules/,./public/js server.js", + "dev": "NODE_ENV=development supervisor --ignore ./node_modules/,./public/js index.js", "start-pg": "pg_virtualenv npm run start-pg-chain", "start-pg-chain": "export DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@localhost:$PGPORT/postgres ; db-migrate up && npm run dev", "test": "export PORT=4243 ; jest && npm run lint && mocha test test/*.js && npm run coverage", diff --git a/server.js b/server.js index e02a37d17a..452c1966f1 100644 --- a/server.js +++ b/server.js @@ -1,42 +1,52 @@ var logger = require('./lib/logger'); -var databaseUri = require('./lib/databaseConfig').getDatabaseUrl(); +var defaultDatabaseUri = require('./lib/databaseConfig').getDatabaseUrl(); -// Database dependecies (statefull) -var db = require('./lib/db/dbPool')(databaseUri); -var eventDb = require('./lib/db/event')(db); -var EventStore = require('./lib/eventStore'); -var eventStore = new EventStore(eventDb); -var featureDb = require('./lib/db/feature')(db, eventStore); -var strategyDb = require('./lib/db/strategy')(db, eventStore); +function start(options) { + options = options || {}; -var config = { - baseUriPath: process.env.BASE_URI_PATH || '', - port: process.env.HTTP_PORT || process.env.PORT || 4242, - db: db, - eventDb: eventDb, - eventStore: eventStore, - featureDb: featureDb, - strategyDb: strategyDb -}; + var db = require('./lib/db/dbPool')(options.databaseUri || defaultDatabaseUri); + // Database dependecies (statefull) + var eventDb = require('./lib/db/event')(db); + var EventStore = require('./lib/eventStore'); + var eventStore = new EventStore(eventDb); + var featureDb = require('./lib/db/feature')(db, eventStore); + var strategyDb = require('./lib/db/strategy')(db, eventStore); -var app = require('./app')(config); + var config = { + baseUriPath: process.env.BASE_URI_PATH || '', + port: process.env.HTTP_PORT || process.env.PORT || 4242, + db: db, + eventDb: eventDb, + eventStore: eventStore, + featureDb: featureDb, + strategyDb: strategyDb + }; -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 app = require('./app')(config); - var webpack = require('webpack'); - var webpackDevMiddleware = require('webpack-dev-middleware'); - var webpackConfig = require('./webpack.config'); - var compiler = webpack(webpackConfig); + var server = app.listen(app.get('port'), function() { + logger.info('unleash started on ' + app.get('port')); + }); - app.use(config.baseUriPath, webpackDevMiddleware(compiler, { - publicPath: '/js', - noInfo: true - })); + 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 + })); + } + + return { + app: app, + server: server + }; } process.on('uncaughtException', function(err) { @@ -45,6 +55,5 @@ process.on('uncaughtException', function(err) { }); module.exports = { - app: app, - server: server + start: start };