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

84 lines
2.0 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 StateService = require('./state-service');
const User = require('./user');
const AuthenticationRequired = require('./authentication-required');
async 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 stateService = new StateService(config);
config.stateService = stateService;
if (config.importFile) {
await stateService.importFile({
file: config.importFile,
dropBeforeImport: config.dropBeforeImport,
userName: 'import',
});
}
const server = app.listen(options.listen, () =>
logger.info('Unleash has started.', server.address())
);
return new Promise((resolve, reject) => {
server.on('listening', () =>
resolve({
app,
server,
eventBus,
stateService,
})
);
server.on('error', reject);
});
}
async function start(opts) {
const options = createOptions(opts);
try {
await migrator(options);
} 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,
};