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
|
|
|
|
2020-12-16 14:49:11 +01:00
|
|
|
const { createMetricsMonitor } = require('./metrics');
|
2016-11-30 23:41:57 +01:00
|
|
|
const { createStores } = require('./db');
|
2020-09-28 21:54:44 +02:00
|
|
|
const { createServices } = require('./services');
|
2016-11-30 23:41:57 +01:00
|
|
|
const { createOptions } = require('./options');
|
2017-11-16 16:45:01 +01:00
|
|
|
const User = require('./user');
|
2020-04-28 08:20:29 +02:00
|
|
|
const permissions = require('./permissions');
|
2017-11-16 16:45:01 +01:00
|
|
|
const AuthenticationRequired = require('./authentication-required');
|
2019-06-18 19:22:18 +02:00
|
|
|
const { addEventHook } = require('./event-hook');
|
2020-09-28 21:54:44 +02:00
|
|
|
const eventType = require('./event-type');
|
2016-09-29 18:10:23 +02:00
|
|
|
|
2020-12-16 14:49:11 +01:00
|
|
|
async function closeServer(opts) {
|
|
|
|
const { server, metricsMonitor } = opts;
|
|
|
|
|
|
|
|
metricsMonitor.stopMonitoring();
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
server.close(err => (err ? reject(err) : resolve()));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function destroyDatabase(stores) {
|
|
|
|
const { db, clientInstanceStore, clientMetricsStore } = stores;
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
clientInstanceStore.destroy();
|
|
|
|
clientMetricsStore.destroy();
|
|
|
|
|
|
|
|
db.destroy(error => (error ? reject(error) : resolve()));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-13 19:10:13 +01:00
|
|
|
async function createApp(options) {
|
2019-06-14 18:10:38 +02:00
|
|
|
// Database dependencies (stateful)
|
2019-04-30 21:14:23 +02:00
|
|
|
const logger = options.getLogger('server-impl.js');
|
2016-11-30 23:41:57 +01:00
|
|
|
const eventBus = new EventEmitter();
|
2019-08-04 11:10:51 +02:00
|
|
|
const stores = createStores(options, eventBus);
|
2020-09-28 21:54:44 +02:00
|
|
|
const services = createServices(stores, options, eventBus);
|
2020-04-13 22:38:46 +02:00
|
|
|
const secret = await stores.settingStore.get('unleash.secret');
|
2016-06-16 17:27:13 +02:00
|
|
|
|
2020-04-14 22:29:11 +02:00
|
|
|
const config = {
|
|
|
|
stores,
|
|
|
|
eventBus,
|
|
|
|
secret,
|
|
|
|
logFactory: options.getLogger, // TODO: remove in v4.x
|
|
|
|
|
|
|
|
...options,
|
|
|
|
};
|
2016-06-16 17:27:13 +02:00
|
|
|
|
2020-12-17 19:43:01 +01:00
|
|
|
const app = getApp(config, services);
|
2020-12-16 14:49:11 +01:00
|
|
|
const metricsMonitor = createMetricsMonitor();
|
|
|
|
metricsMonitor.startMonitoring(config);
|
2016-11-30 23:41:57 +01:00
|
|
|
|
2019-06-18 19:22:18 +02:00
|
|
|
if (typeof config.eventHook === 'function') {
|
|
|
|
addEventHook(config.eventHook, stores.eventStore);
|
|
|
|
}
|
|
|
|
|
2019-03-13 19:10:13 +01:00
|
|
|
if (config.importFile) {
|
2020-11-23 21:52:57 +01:00
|
|
|
await services.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',
|
2020-11-03 14:56:07 +01:00
|
|
|
keepExisting: config.importKeepExisting,
|
2019-03-13 19:10:13 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-14 17:56:02 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-06-17 08:03:02 +02:00
|
|
|
const payload = {
|
|
|
|
app,
|
|
|
|
config,
|
|
|
|
stores,
|
2020-09-28 21:54:44 +02:00
|
|
|
services,
|
2020-06-17 08:03:02 +02:00
|
|
|
eventBus,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.start) {
|
|
|
|
const server = app.listen(options.listen, () =>
|
|
|
|
logger.info('Unleash has started.', server.address()),
|
|
|
|
);
|
2020-12-16 14:49:11 +01:00
|
|
|
const stop = () => {
|
|
|
|
logger.info('Shutting down Unleash...');
|
|
|
|
|
|
|
|
return closeServer({ server, metricsMonitor }).then(() => {
|
|
|
|
return destroyDatabase(stores);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-07-13 21:40:22 +02:00
|
|
|
server.keepAliveTimeout = options.keepAliveTimeout;
|
|
|
|
server.headersTimeout = options.headersTimeout;
|
2020-06-17 08:03:02 +02:00
|
|
|
server.on('listening', () => {
|
2020-12-16 14:49:11 +01:00
|
|
|
resolve({ ...payload, server, stop });
|
2020-06-17 08:03:02 +02:00
|
|
|
});
|
|
|
|
server.on('error', reject);
|
|
|
|
} else {
|
2020-12-16 14:49:11 +01:00
|
|
|
const stop = () => {
|
|
|
|
logger.info('Shutting down Unleash...');
|
|
|
|
metricsMonitor.stopMonitoring();
|
|
|
|
return destroyDatabase(stores);
|
|
|
|
};
|
|
|
|
|
|
|
|
resolve({ ...payload, stop });
|
2020-06-17 08:03:02 +02:00
|
|
|
}
|
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-11-18 08:10:58 +01:00
|
|
|
if (options.disableDBMigration) {
|
|
|
|
logger.info('DB migrations disabled');
|
|
|
|
} else {
|
|
|
|
await migrator(options);
|
|
|
|
}
|
2018-05-09 21:20:45 +02:00
|
|
|
} catch (err) {
|
|
|
|
logger.error('Failed to migrate db', err);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2020-05-02 09:12:36 +02:00
|
|
|
return createApp(options);
|
2016-06-16 17:27:13 +02:00
|
|
|
}
|
|
|
|
|
2020-06-17 08:03:02 +02:00
|
|
|
async function create(opts) {
|
|
|
|
return start({ ...opts, start: false });
|
|
|
|
}
|
|
|
|
|
2016-06-16 17:27:13 +02:00
|
|
|
module.exports = {
|
2016-06-18 21:55:46 +02:00
|
|
|
start,
|
2020-06-17 08:03:02 +02:00
|
|
|
create,
|
2017-11-16 16:45:01 +01:00
|
|
|
User,
|
|
|
|
AuthenticationRequired,
|
2020-04-28 08:20:29 +02:00
|
|
|
permissions,
|
2020-09-28 21:54:44 +02:00
|
|
|
eventType,
|
2016-06-16 17:27:13 +02:00
|
|
|
};
|