From a05cea474d71442f4fa6009c202a4a3a3e03d966 Mon Sep 17 00:00:00 2001 From: Ivar Date: Thu, 16 Jun 2016 17:27:13 +0200 Subject: [PATCH] Fixed api from server-impl to make it easier to require. only release resources needed by others. --- index.js | 3 --- package.json | 17 ++++++++++---- server-dev.js | 17 ++++++++++++++ server-impl.js | 45 +++++++++++++++++++++++++++++++++++++ server.js | 60 ++------------------------------------------------ 5 files changed, 77 insertions(+), 65 deletions(-) delete mode 100644 index.js create mode 100644 server-dev.js create mode 100644 server-impl.js diff --git a/index.js b/index.js deleted file mode 100644 index 14a7724fe2..0000000000 --- a/index.js +++ /dev/null @@ -1,3 +0,0 @@ -var server = require('./server'); - -server.start({}); diff --git a/package.json b/package.json index aac566f66a..733314b9ef 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,16 @@ "feature", "toggle" ], + "files": [ + "lib", + "migrations", + "public", + "app.js", + "server.js", + "server-impl.js", + "README.md", + "LICENSE" + ], "repository": { "type": "git", "url": "ssh://git@github.com:finn-no/unleash.git" @@ -18,13 +28,13 @@ "engines": { "node": ">=4.4.3" }, - "main": "./server.js", + "main": "./server-impl.js", "private": true, "scripts": { "db-migrate-and-start": "npm run db-migrate && npm run start", - "start": "NODE_ENV=production node index.js", + "start": "npm run build && NODE_ENV=production node server.js", "build": "./node_modules/.bin/webpack -p", - "dev": "NODE_ENV=development supervisor --ignore ./node_modules/,./public/js index.js", + "dev": "NODE_ENV=development supervisor --ignore ./node_modules/,./public/js server-dev.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", @@ -37,7 +47,6 @@ "test-bamboo-ci": "mocha test test/*", "coverage": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec", "coverage-report": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", - "postinstall": "npm run build", "jest": "jest", "lint": "eslint . --ignore-path .gitignore" }, diff --git a/server-dev.js b/server-dev.js new file mode 100644 index 0000000000..1e506dd286 --- /dev/null +++ b/server-dev.js @@ -0,0 +1,17 @@ +var server = require('./server-impl'); + +var unleash = server.start({}); +var app = unleash.app; +var config = unleash.config; + +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 +})); diff --git a/server-impl.js b/server-impl.js new file mode 100644 index 0000000000..6ece96bdca --- /dev/null +++ b/server-impl.js @@ -0,0 +1,45 @@ +var logger = require('./lib/logger'); +var defaultDatabaseUri = require('./lib/databaseConfig').getDatabaseUrl(); + +function start(options) { + options = options || {}; + + 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 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 app = require('./app')(config); + + var server = app.listen(app.get('port'), function() { + logger.info('unleash started on ' + app.get('port')); + }); + + return { + app: app, + server: server, + config: config + }; +} + +process.on('uncaughtException', function(err) { + logger.error('Uncaught Exception:', err.message); + logger.error(err.stack); +}); + +module.exports = { + start: start +}; diff --git a/server.js b/server.js index 452c1966f1..b9d1e75cac 100644 --- a/server.js +++ b/server.js @@ -1,59 +1,3 @@ -var logger = require('./lib/logger'); -var defaultDatabaseUri = require('./lib/databaseConfig').getDatabaseUrl(); +var server = require('./server-impl'); -function start(options) { - options = options || {}; - - 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 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 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 - })); - } - - return { - app: app, - server: server - }; -} - -process.on('uncaughtException', function(err) { - logger.error('Uncaught Exception:', err.message); - logger.error(err.stack); -}); - -module.exports = { - start: start -}; +server.start({});