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

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-06-18 21:53:18 +02:00
'use strict';
2016-10-26 10:43:11 +02:00
const { EventEmitter } = require('events');
const logger = require('./logger')('server-impl.js');
const migrator = require('../migrator');
2016-11-13 21:09:00 +01:00
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');
2017-06-28 10:17:14 +02:00
function createApp(options) {
// Database dependecies (statefull)
const stores = createStores(options);
const eventBus = new EventEmitter();
2017-06-28 10:17:14 +02:00
const config = Object.assign(
{
stores,
eventBus,
},
options
);
2016-11-13 15:15:52 +01:00
const app = getApp(config);
startMonitoring(options.serverMetrics, eventBus);
const server = app.listen({ port: options.port, host: options.host }, () =>
logger.info(`Unleash started on port ${server.address().port}`)
);
return new Promise((resolve, reject) => {
server.on('listening', () => resolve({ app, server, eventBus }));
server.on('error', reject);
});
}
2017-06-28 10:17:14 +02:00
function start(opts) {
const options = createOptions(opts);
2016-12-03 13:45:22 +01:00
return migrator({ databaseUrl: options.databaseUrl })
2016-11-13 20:43:29 +01:00
.catch(err => logger.error('failed to migrate db', err))
.then(() => createApp(options));
}
module.exports = {
2016-06-18 21:55:46 +02:00
start,
User,
AuthenticationRequired,
};