1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/packages/unleash-api/server-impl.js

65 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
2016-10-26 10:43:11 +02:00
2016-06-18 21:53:18 +02:00
const logger = require('./lib/logger');
const migrator = require('./migrator');
const DEFAULT_OPTIONS = {
2016-11-01 15:38:27 +01:00
databaseUri: process.env.DATABASE_URL || 'postgres://unleash_user:passord@localhost:5432/unleash',
port: process.env.HTTP_PORT || process.env.PORT || 4242,
baseUriPath: process.env.BASE_URI_PATH || '',
};
function createApp (options) {
2016-10-27 20:51:21 +02:00
const db = require('./lib/db/db-pool')(options.databaseUri);
// Database dependecies (statefull)
2016-11-05 10:16:48 +01:00
const {
eventStore,
featureToggleStore,
strategyStore,
clientInstancesDb,
clientMetricsDb,
clientStrategiesDb,
} = require('./lib/db')(db);
2016-06-18 21:53:18 +02:00
const config = {
baseUriPath: options.baseUriPath,
port: options.port,
publicFolder: options.publicFolder,
2016-06-18 21:53:18 +02:00
db,
eventStore,
2016-11-05 10:16:48 +01:00
featureToggleStore,
strategyStore,
2016-11-04 09:03:13 +01:00
clientMetricsDb,
2016-11-02 23:17:28 +01:00
clientStrategiesDb,
2016-11-04 09:03:13 +01:00
clientInstancesDb,
};
2016-06-18 21:53:18 +02:00
const app = require('./app')(config);
const server = app.listen(app.get('port'), () => {
logger.info(`Unleash started on ${app.get('port')}`);
});
return { app, server };
}
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-18 21:53:18 +02:00
process.on('uncaughtException', err => {
logger.error('Uncaught Exception:', err.message);
logger.error(err.stack);
});
module.exports = {
2016-06-18 21:55:46 +02:00
start,
};