From addf5d7e9dcf85f2dc24eee78814f40a5926a1b7 Mon Sep 17 00:00:00 2001 From: Ivar Date: Wed, 28 Sep 2016 23:54:19 +0200 Subject: [PATCH 1/3] inital step to createing a exectutable unleash binary #150 --- packages/unleash-server/package.json | 8 +++++--- packages/unleash-server/unleash.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100755 packages/unleash-server/unleash.js diff --git a/packages/unleash-server/package.json b/packages/unleash-server/package.json index 9584239462..21c1f07c70 100644 --- a/packages/unleash-server/package.json +++ b/packages/unleash-server/package.json @@ -24,6 +24,9 @@ "node": "6" }, "main": "./server.js", + "bin": { + "unleash": "NODE_ENV=production ./unleash.js" + }, "scripts": { "db-migrate-and-start": "npm run db-migrate && npm run start", "start": "NODE_ENV=production node server-impl.js", @@ -31,10 +34,9 @@ "test:ci": "npm run test" }, "dependencies": { + "commander": "^2.9.0", "unleash-api": "1.0.0-alpha.2", "unleash-frontend": "1.0.0-alpha.2" }, - "devDependencies": { - - } + "devDependencies": {} } diff --git a/packages/unleash-server/unleash.js b/packages/unleash-server/unleash.js new file mode 100755 index 0000000000..c73c25f004 --- /dev/null +++ b/packages/unleash-server/unleash.js @@ -0,0 +1,15 @@ +#!/usr/bin/env node + +'use strict'; + +const program = require('commander'); +const unleash = require('./server.js'); + + +program + .command('start', 'start unleash server') + .command('migrate', 'migrate the unleash db') + .option('-d, --databaseUri ', 'The full databaseUri to connect to, including username and password') + .parse(process.argv); + +unleash.start({ databaseUri: program.databaseUri }); From 100e99db7eef42dcd869e4d7b04180eab26af74d Mon Sep 17 00:00:00 2001 From: ivaosthu Date: Thu, 29 Sep 2016 18:10:23 +0200 Subject: [PATCH 2/3] upgrade db-migrate to support programmatic API #150 --- packages/unleash-api/migrator.js | 25 ++++++++++++++++++ packages/unleash-api/package.json | 4 ++- packages/unleash-api/server-impl.js | 39 ++++++++++++++++++---------- packages/unleash-server/package.json | 2 +- packages/unleash-server/unleash.js | 13 +++++++--- 5 files changed, 64 insertions(+), 19 deletions(-) create mode 100644 packages/unleash-api/migrator.js diff --git a/packages/unleash-api/migrator.js b/packages/unleash-api/migrator.js new file mode 100644 index 0000000000..5d21f69a32 --- /dev/null +++ b/packages/unleash-api/migrator.js @@ -0,0 +1,25 @@ +'use strict'; + +const DBMigrate = require('db-migrate'); +const path = require('path'); + +function findUnleashApiRoot () { + try { + return path.dirname(require.resolve('unleash-api/package.json')); + } catch (e) {} + try { + return path.dirname(require.resolve('../unleash-api')); + } catch (e) {} + return process.cwd(); +} + +function migrateDb (dbUri) { + const dbmigrate = DBMigrate.getInstance(true, { + cwd: findUnleashApiRoot(), + config: { custom: dbUri }, + env: 'custom' } + ); + return dbmigrate.up(); +} + +module.exports = migrateDb; diff --git a/packages/unleash-api/package.json b/packages/unleash-api/package.json index bcd560af67..cfd7becd07 100644 --- a/packages/unleash-api/package.json +++ b/packages/unleash-api/package.json @@ -36,6 +36,7 @@ "start:dev:pg": "pg_virtualenv npm run start:dev:pg-chain", "start:dev:pg-chain": "export DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@localhost:$PGPORT/postgres ; db-migrate up && npm run start:dev", "db-migrate": "db-migrate up", + "db-migrate:down": "db-migrate down", "test": "export PORT=4243 ; mocha test/**/*.js && npm run test:coverage", "test:unit": "mocha test/unit/**/*.js ", "test:ci": "npm run db-migrate && npm run test", @@ -49,7 +50,8 @@ "bluebird": "^3.4.6", "body-parser": "1.15.2", "cookie-parser": "^1.4.1", - "db-migrate": "0.9.25", + "db-migrate": "0.10.0-beta.17", + "db-migrate-pg": "0.1.10", "deep-diff": "^0.3.3", "errorhandler": "^1.4.3", "express": "4.14.0", diff --git a/packages/unleash-api/server-impl.js b/packages/unleash-api/server-impl.js index 853829d696..abd0ba922e 100644 --- a/packages/unleash-api/server-impl.js +++ b/packages/unleash-api/server-impl.js @@ -1,11 +1,16 @@ 'use strict'; const logger = require('./lib/logger'); -const defaultDatabaseUri = process.env.DATABASE_URL; +const migrator = require('./migrator'); -function start (options) { - options = options || {}; +const DEFAULT_OPTIONS = { + databaseUri: process.env.DATABASE_URL, + port: process.env.HTTP_PORT || process.env.PORT || 4242, + baseUriPath: process.env.BASE_URI_PATH || '', +}; + +function createApp (options) { + const db = require('./lib/db/dbPool')(options.databaseUri); - const db = require('./lib/db/dbPool')(options.databaseUri || defaultDatabaseUri); // Database dependecies (statefull) const eventDb = require('./lib/db/event')(db); const EventStore = require('./lib/eventStore'); @@ -14,27 +19,33 @@ function start (options) { const strategyDb = require('./lib/db/strategy')(db, eventStore); const config = { - baseUriPath: process.env.BASE_URI_PATH || '', - port: process.env.HTTP_PORT || process.env.PORT || 4242, + baseUriPath: options.baseUriPath, + port: options.port, + publicFolder: options.publicFolder, db, eventDb, eventStore, featureDb, strategyDb, - publicFolder: options.publicFolder, }; const app = require('./app')(config); - const server = app.listen(app.get('port'), () => { - logger.info(`unleash started on ${app.get('port')}`); + logger.info(`Unleash started on ${app.get('port')}`); }); + return { app, server }; +} - return { - app, - server, - config, - }; +function start (opts) { + const options = Object.assign({}, DEFAULT_OPTIONS, opts); + + if (!options.databaseUri) { + throw new Error('You must either pass databaseUri option or set environemnt variable DATABASE_URL'); + } + + return migrator(options.databaseUri) + .then(() => createApp(options)) + .catch(err => logger.error('failed to migrate db', err)); } process.on('uncaughtException', err => { diff --git a/packages/unleash-server/package.json b/packages/unleash-server/package.json index 21c1f07c70..56a4e37de5 100644 --- a/packages/unleash-server/package.json +++ b/packages/unleash-server/package.json @@ -25,7 +25,7 @@ }, "main": "./server.js", "bin": { - "unleash": "NODE_ENV=production ./unleash.js" + "unleash": "./unleash.js" }, "scripts": { "db-migrate-and-start": "npm run db-migrate && npm run start", diff --git a/packages/unleash-server/unleash.js b/packages/unleash-server/unleash.js index c73c25f004..aa11eaed61 100755 --- a/packages/unleash-server/unleash.js +++ b/packages/unleash-server/unleash.js @@ -2,14 +2,21 @@ 'use strict'; +process.env.NODE_ENV = 'production'; + const program = require('commander'); const unleash = require('./server.js'); program - .command('start', 'start unleash server') - .command('migrate', 'migrate the unleash db') + .option('-p, --port ', 'The full port you want to start unleash on.') .option('-d, --databaseUri ', 'The full databaseUri to connect to, including username and password') .parse(process.argv); -unleash.start({ databaseUri: program.databaseUri }); +unleash.start({ + databaseUri: program.databaseUri || process.env.DATABASE_URL, + port: program.port || process.env.PORT || 4242, +}).then(conf => { + console.log(`Unleash started on port:${conf.app.get('port')}`); +}); + From c00cf70cf15d7ef68c0021e462d3dfa52ecce84a Mon Sep 17 00:00:00 2001 From: Ivar Date: Thu, 29 Sep 2016 22:50:55 +0200 Subject: [PATCH 3/3] Minor fixes after feedback #150 --- packages/unleash-api/migrator.js | 2 +- packages/unleash-server/{ => bin}/unleash.js | 4 ++-- packages/unleash-server/package.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) rename packages/unleash-server/{ => bin}/unleash.js (80%) diff --git a/packages/unleash-api/migrator.js b/packages/unleash-api/migrator.js index 5d21f69a32..d51e2564b0 100644 --- a/packages/unleash-api/migrator.js +++ b/packages/unleash-api/migrator.js @@ -8,7 +8,7 @@ function findUnleashApiRoot () { return path.dirname(require.resolve('unleash-api/package.json')); } catch (e) {} try { - return path.dirname(require.resolve('../unleash-api')); + return path.dirname(require.resolve('../unleash-api/package.json')); } catch (e) {} return process.cwd(); } diff --git a/packages/unleash-server/unleash.js b/packages/unleash-server/bin/unleash.js similarity index 80% rename from packages/unleash-server/unleash.js rename to packages/unleash-server/bin/unleash.js index aa11eaed61..2a8305259c 100755 --- a/packages/unleash-server/unleash.js +++ b/packages/unleash-server/bin/unleash.js @@ -5,11 +5,11 @@ process.env.NODE_ENV = 'production'; const program = require('commander'); -const unleash = require('./server.js'); +const unleash = require('../server.js'); program - .option('-p, --port ', 'The full port you want to start unleash on.') + .option('-p, --port ', 'The port you want to start unleash on') .option('-d, --databaseUri ', 'The full databaseUri to connect to, including username and password') .parse(process.argv); diff --git a/packages/unleash-server/package.json b/packages/unleash-server/package.json index 56a4e37de5..05dad2a26e 100644 --- a/packages/unleash-server/package.json +++ b/packages/unleash-server/package.json @@ -25,7 +25,7 @@ }, "main": "./server.js", "bin": { - "unleash": "./unleash.js" + "unleash": "./bin/unleash.js" }, "scripts": { "db-migrate-and-start": "npm run db-migrate && npm run start",