2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
2016-11-30 23:41:57 +01:00
|
|
|
const { EventEmitter } = require('events');
|
|
|
|
|
2016-11-13 21:07:14 +01:00
|
|
|
const migrator = require('../migrator');
|
2016-11-13 21:09:00 +01:00
|
|
|
const getApp = require('./app');
|
2016-06-16 17:27:13 +02:00
|
|
|
|
2016-11-30 23:41:57 +01:00
|
|
|
const { startMonitoring } = require('./metrics');
|
|
|
|
const { createStores } = require('./db');
|
|
|
|
const { createOptions } = require('./options');
|
2019-03-13 19:10:13 +01:00
|
|
|
const StateService = require('./state-service');
|
2017-11-16 16:45:01 +01:00
|
|
|
const User = require('./user');
|
|
|
|
const AuthenticationRequired = require('./authentication-required');
|
2016-09-29 18:10:23 +02:00
|
|
|
|
2019-03-13 19:10:13 +01:00
|
|
|
async function createApp(options) {
|
2016-06-16 17:27:13 +02:00
|
|
|
// Database dependecies (statefull)
|
2019-04-30 21:14:23 +02:00
|
|
|
const logger = options.getLogger('server-impl.js');
|
2016-11-05 14:08:47 +01:00
|
|
|
const stores = createStores(options);
|
2016-11-30 23:41:57 +01:00
|
|
|
const eventBus = new EventEmitter();
|
2016-06-16 17:27:13 +02:00
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
const config = Object.assign(
|
|
|
|
{
|
|
|
|
stores,
|
|
|
|
eventBus,
|
2019-04-30 21:14:23 +02:00
|
|
|
logFactory: options.getLogger, // TODO: remove in v4.x
|
2017-06-28 10:17:14 +02:00
|
|
|
},
|
2018-11-24 12:58:30 +01:00
|
|
|
options
|
2017-06-28 10:17:14 +02:00
|
|
|
);
|
2016-06-16 17:27:13 +02:00
|
|
|
|
2016-11-13 15:15:52 +01:00
|
|
|
const app = getApp(config);
|
2018-11-28 15:50:49 +01:00
|
|
|
startMonitoring(
|
|
|
|
options.serverMetrics,
|
|
|
|
eventBus,
|
|
|
|
stores.eventStore,
|
|
|
|
stores.clientMetricsStore
|
|
|
|
);
|
2016-11-30 23:41:57 +01:00
|
|
|
|
2019-03-13 19:10:13 +01:00
|
|
|
const stateService = new StateService(config);
|
|
|
|
config.stateService = stateService;
|
|
|
|
if (config.importFile) {
|
|
|
|
await stateService.importFile({
|
2019-03-14 17:56:02 +01:00
|
|
|
file: config.importFile,
|
2019-03-13 19:10:13 +01:00
|
|
|
dropBeforeImport: config.dropBeforeImport,
|
2019-03-14 17:56:02 +01:00
|
|
|
userName: 'import',
|
2019-03-13 19:10:13 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-26 08:45:45 +02:00
|
|
|
const server = app.listen(options.listen, () =>
|
|
|
|
logger.info('Unleash has started.', server.address())
|
2018-05-06 12:57:44 +02:00
|
|
|
);
|
|
|
|
|
2019-03-14 17:56:02 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-03-13 19:10:13 +01:00
|
|
|
server.on('listening', () =>
|
|
|
|
resolve({
|
|
|
|
app,
|
|
|
|
server,
|
|
|
|
eventBus,
|
|
|
|
stateService,
|
|
|
|
})
|
|
|
|
);
|
2018-05-06 12:57:44 +02:00
|
|
|
server.on('error', reject);
|
2018-05-06 12:17:46 +02:00
|
|
|
});
|
2016-09-29 18:10:23 +02:00
|
|
|
}
|
2016-06-16 17:27:13 +02:00
|
|
|
|
2018-05-09 21:20:45 +02:00
|
|
|
async function start(opts) {
|
2016-11-30 23:41:57 +01:00
|
|
|
const options = createOptions(opts);
|
2019-04-30 21:14:23 +02:00
|
|
|
const logger = options.getLogger('server-impl.js');
|
2016-09-29 18:10:23 +02:00
|
|
|
|
2018-05-09 21:20:45 +02:00
|
|
|
try {
|
2019-02-01 08:06:02 +01:00
|
|
|
await migrator(options);
|
2018-05-09 21:20:45 +02:00
|
|
|
} catch (err) {
|
|
|
|
logger.error('Failed to migrate db', err);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return createApp(options);
|
2016-06-16 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2016-06-18 21:55:46 +02:00
|
|
|
start,
|
2017-11-16 16:45:01 +01:00
|
|
|
User,
|
|
|
|
AuthenticationRequired,
|
2016-06-16 17:27:13 +02:00
|
|
|
};
|