1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00
unleash.unleash/lib/server-impl.js

66 lines
1.6 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 logFactory = require('./logger');
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,
logFactory,
2017-06-28 10:17:14 +02:00
},
options
2017-06-28 10:17:14 +02:00
);
2016-11-13 15:15:52 +01:00
const app = getApp(config);
startMonitoring(
options.serverMetrics,
eventBus,
stores.eventStore,
stores.clientMetricsStore
);
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);
});
}
async function start(opts) {
const options = createOptions(opts);
try {
await migrator({ databaseUrl: options.databaseUrl });
} catch (err) {
logger.error('Failed to migrate db', err);
throw err;
}
return createApp(options);
}
module.exports = {
2016-06-18 21:55:46 +02:00
start,
User,
AuthenticationRequired,
};