diff --git a/packages/unleash-api/migrator.js b/packages/unleash-api/migrator.js new file mode 100644 index 0000000000..d51e2564b0 --- /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/package.json')); + } 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/bin/unleash.js b/packages/unleash-server/bin/unleash.js new file mode 100755 index 0000000000..2a8305259c --- /dev/null +++ b/packages/unleash-server/bin/unleash.js @@ -0,0 +1,22 @@ +#!/usr/bin/env node + +'use strict'; + +process.env.NODE_ENV = 'production'; + +const program = require('commander'); +const unleash = require('../server.js'); + + +program + .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); + +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')}`); +}); + diff --git a/packages/unleash-server/package.json b/packages/unleash-server/package.json index 9584239462..05dad2a26e 100644 --- a/packages/unleash-server/package.json +++ b/packages/unleash-server/package.json @@ -24,6 +24,9 @@ "node": "6" }, "main": "./server.js", + "bin": { + "unleash": "./bin/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": {} }