1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/lib/server-impl.js
2018-05-07 08:43:26 +02:00

53 lines
1.4 KiB
JavaScript

'use strict';
const { EventEmitter } = require('events');
const logger = require('./logger')('server-impl.js');
const migrator = require('../migrator');
const getApp = require('./app');
const { startMonitoring } = require('./metrics');
const { createStores } = require('./db');
const { createOptions } = require('./options');
const User = require('./user');
const AuthenticationRequired = require('./authentication-required');
function createApp(options) {
// Database dependecies (statefull)
const stores = createStores(options);
const eventBus = new EventEmitter();
const config = Object.assign(
{
stores,
eventBus,
},
options
);
const app = getApp(config);
startMonitoring(options.serverMetrics, eventBus);
return new Promise(resolve => {
const server = app.listen(app.get('port'), options.host, () => {
logger.info(`Unleash started on port ${server.address().port}`);
resolve({ app, server, eventBus });
});
});
}
function start(opts) {
const options = createOptions(opts);
return migrator({ databaseUrl: options.databaseUrl })
.catch(err => logger.error('failed to migrate db', err))
.then(() => createApp(options))
.catch(err => logger.error('failed creating app', err));
}
module.exports = {
start,
User,
AuthenticationRequired,
};