2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
|
|
|
const logger = require('./lib/logger');
|
2016-09-29 18:10:23 +02:00
|
|
|
const migrator = require('./migrator');
|
2016-06-16 17:27:13 +02:00
|
|
|
|
2016-09-29 18:10:23 +02:00
|
|
|
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);
|
2016-06-16 17:27:13 +02:00
|
|
|
|
|
|
|
// Database dependecies (statefull)
|
2016-06-18 21:53:18 +02:00
|
|
|
const eventDb = require('./lib/db/event')(db);
|
|
|
|
const EventStore = require('./lib/eventStore');
|
|
|
|
const eventStore = new EventStore(eventDb);
|
|
|
|
const featureDb = require('./lib/db/feature')(db, eventStore);
|
|
|
|
const strategyDb = require('./lib/db/strategy')(db, eventStore);
|
2016-06-16 17:27:13 +02:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
const config = {
|
2016-09-29 18:10:23 +02:00
|
|
|
baseUriPath: options.baseUriPath,
|
|
|
|
port: options.port,
|
|
|
|
publicFolder: options.publicFolder,
|
2016-06-18 21:53:18 +02:00
|
|
|
db,
|
|
|
|
eventDb,
|
|
|
|
eventStore,
|
|
|
|
featureDb,
|
|
|
|
strategyDb,
|
2016-06-16 17:27:13 +02:00
|
|
|
};
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
const app = require('./app')(config);
|
|
|
|
const server = app.listen(app.get('port'), () => {
|
2016-09-29 18:10:23 +02:00
|
|
|
logger.info(`Unleash started on ${app.get('port')}`);
|
2016-06-16 17:27:13 +02:00
|
|
|
});
|
2016-09-29 18:10:23 +02:00
|
|
|
return { app, server };
|
|
|
|
}
|
2016-06-16 17:27:13 +02:00
|
|
|
|
2016-09-29 18:10:23 +02:00
|
|
|
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));
|
2016-06-16 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
process.on('uncaughtException', err => {
|
2016-06-16 17:27:13 +02:00
|
|
|
logger.error('Uncaught Exception:', err.message);
|
|
|
|
logger.error(err.stack);
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
2016-06-18 21:55:46 +02:00
|
|
|
start,
|
2016-06-16 17:27:13 +02:00
|
|
|
};
|