2021-04-22 10:07:10 +02:00
|
|
|
import EventEmitter from 'events';
|
2021-06-17 20:33:34 +02:00
|
|
|
import stoppable, { StoppableServer } from 'stoppable';
|
|
|
|
import { promisify } from 'util';
|
2021-04-22 10:07:10 +02:00
|
|
|
import version from './util/version';
|
2021-09-10 11:42:11 +02:00
|
|
|
import { migrateDb } from '../migrator';
|
2021-04-22 10:07:10 +02:00
|
|
|
import getApp from './app';
|
2021-06-17 20:33:34 +02:00
|
|
|
import { createMetricsMonitor } from './metrics';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { createStores } from './db';
|
|
|
|
import { createServices } from './services';
|
2021-04-22 15:04:08 +02:00
|
|
|
import { createConfig } from './create-config';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { addEventHook } from './event-hook';
|
2021-06-15 12:32:35 +02:00
|
|
|
import registerGracefulShutdown from './util/graceful-shutdown';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { createDb } from './db/db-pool';
|
|
|
|
import sessionDb from './middleware/session-db';
|
2021-08-13 19:22:15 +02:00
|
|
|
// Types
|
|
|
|
import { IUnleash } from './types/core';
|
2021-09-13 15:57:38 +02:00
|
|
|
import { IUnleashConfig, IUnleashOptions, IAuthType } from './types/option';
|
2021-08-13 19:22:15 +02:00
|
|
|
import { IUnleashServices } from './types/services';
|
|
|
|
import User, { IUser } from './types/user';
|
|
|
|
import { Logger, LogLevel } from './logger';
|
|
|
|
import AuthenticationRequired from './types/authentication-required';
|
|
|
|
import Controller from './routes/controller';
|
|
|
|
import { IAuthRequest } from './routes/unleash-types';
|
|
|
|
import * as permissions from './types/permissions';
|
|
|
|
import * as eventType from './types/events';
|
2021-08-23 12:11:29 +02:00
|
|
|
import { RoleName } from './types/model';
|
2021-04-22 10:07:10 +02:00
|
|
|
|
|
|
|
async function createApp(
|
|
|
|
config: IUnleashConfig,
|
|
|
|
startApp: boolean,
|
|
|
|
): Promise<IUnleash> {
|
|
|
|
// Database dependencies (stateful)
|
|
|
|
const logger = config.getLogger('server-impl.js');
|
|
|
|
const serverVersion = version;
|
|
|
|
const eventBus = new EventEmitter();
|
2021-08-12 15:04:37 +02:00
|
|
|
const db = createDb(config);
|
|
|
|
const stores = createStores(config, eventBus, db);
|
2021-04-22 10:07:10 +02:00
|
|
|
const services = createServices(stores, config);
|
2021-09-03 12:06:58 +02:00
|
|
|
|
2021-06-17 20:33:34 +02:00
|
|
|
const metricsMonitor = createMetricsMonitor();
|
2021-08-12 15:04:37 +02:00
|
|
|
const unleashSession = sessionDb(config, db);
|
2021-06-17 20:33:34 +02:00
|
|
|
|
|
|
|
const stopUnleash = async (server?: StoppableServer) => {
|
|
|
|
logger.info('Shutting down Unleash...');
|
|
|
|
if (server) {
|
|
|
|
const stopServer = promisify(server.stop);
|
|
|
|
await stopServer();
|
|
|
|
}
|
|
|
|
metricsMonitor.stopMonitoring();
|
2021-06-18 11:53:52 +02:00
|
|
|
stores.clientInstanceStore.destroy();
|
|
|
|
stores.clientMetricsStore.destroy();
|
2021-08-12 15:04:37 +02:00
|
|
|
await db.destroy();
|
2021-06-17 20:33:34 +02:00
|
|
|
};
|
2021-04-22 10:07:10 +02:00
|
|
|
|
|
|
|
if (!config.server.secret) {
|
|
|
|
const secret = await stores.settingStore.get('unleash.secret');
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
config.server.secret = secret;
|
|
|
|
}
|
2021-08-12 15:04:37 +02:00
|
|
|
const app = getApp(config, stores, services, eventBus, unleashSession);
|
2021-06-17 20:33:34 +02:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
if (typeof config.eventHook === 'function') {
|
|
|
|
addEventHook(config.eventHook, stores.eventStore);
|
|
|
|
}
|
2021-08-12 15:04:37 +02:00
|
|
|
metricsMonitor.startMonitoring(config, stores, serverVersion, eventBus, db);
|
2021-04-22 10:07:10 +02:00
|
|
|
const unleash: Omit<IUnleash, 'stop'> = {
|
|
|
|
stores,
|
|
|
|
eventBus,
|
|
|
|
services,
|
|
|
|
app,
|
|
|
|
config,
|
|
|
|
version: serverVersion,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (config.import.file) {
|
|
|
|
await services.stateService.importFile({
|
|
|
|
file: config.import.file,
|
|
|
|
dropBeforeImport: config.import.dropBeforeImport,
|
|
|
|
userName: 'import',
|
|
|
|
keepExisting: config.import.keepExisting,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (startApp) {
|
2021-06-17 20:33:34 +02:00
|
|
|
const server = stoppable(
|
|
|
|
app.listen(config.listen, () =>
|
|
|
|
logger.info('Unleash has started.', server.address()),
|
|
|
|
),
|
|
|
|
config.server.gracefulShutdownTimeout,
|
2021-04-22 10:07:10 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
server.keepAliveTimeout = config.server.keepAliveTimeout;
|
|
|
|
server.headersTimeout = config.server.headersTimeout;
|
|
|
|
server.on('listening', () => {
|
2021-06-17 20:33:34 +02:00
|
|
|
resolve({
|
|
|
|
...unleash,
|
|
|
|
server,
|
|
|
|
stop: () => stopUnleash(server),
|
|
|
|
});
|
2021-04-22 10:07:10 +02:00
|
|
|
});
|
|
|
|
server.on('error', reject);
|
|
|
|
} else {
|
2021-06-17 20:33:34 +02:00
|
|
|
resolve({ ...unleash, stop: stopUnleash });
|
2021-04-22 10:07:10 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async function start(opts: IUnleashOptions = {}): Promise<IUnleash> {
|
|
|
|
const config = createConfig(opts);
|
|
|
|
const logger = config.getLogger('server-impl.js');
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (config.db.disableMigration) {
|
2021-09-03 12:06:58 +02:00
|
|
|
logger.info('DB migration: disabled');
|
2021-04-22 10:07:10 +02:00
|
|
|
} else {
|
2021-09-03 12:06:58 +02:00
|
|
|
logger.info('DB migration: start');
|
2021-09-10 11:42:11 +02:00
|
|
|
await migrateDb(config);
|
2021-09-03 12:06:58 +02:00
|
|
|
logger.info('DB migration: end');
|
2021-04-22 10:07:10 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Failed to migrate db', err);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2021-06-15 12:32:35 +02:00
|
|
|
const unleash = await createApp(config, true);
|
2021-06-17 20:33:34 +02:00
|
|
|
if (config.server.gracefulShutdownEnable) {
|
|
|
|
registerGracefulShutdown(unleash, logger);
|
|
|
|
}
|
2021-06-15 12:32:35 +02:00
|
|
|
return unleash;
|
2021-04-22 10:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
async function create(opts: IUnleashOptions): Promise<IUnleash> {
|
|
|
|
const config = createConfig(opts);
|
|
|
|
const logger = config.getLogger('server-impl.js');
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (config.db.disableMigration) {
|
|
|
|
logger.info('DB migrations disabled');
|
|
|
|
} else {
|
2021-09-10 11:42:11 +02:00
|
|
|
await migrateDb(config);
|
2021-04-22 10:07:10 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Failed to migrate db', err);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
return createApp(config, false);
|
|
|
|
}
|
2021-08-13 19:22:15 +02:00
|
|
|
|
|
|
|
// Module exports
|
|
|
|
export {
|
2021-04-22 10:07:10 +02:00
|
|
|
start,
|
|
|
|
create,
|
|
|
|
permissions,
|
|
|
|
eventType,
|
2021-08-13 19:22:15 +02:00
|
|
|
Controller,
|
|
|
|
AuthenticationRequired,
|
|
|
|
User,
|
|
|
|
LogLevel,
|
2021-08-23 12:11:29 +02:00
|
|
|
RoleName,
|
2021-09-13 15:57:38 +02:00
|
|
|
IAuthType,
|
2021-08-13 19:22:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
start,
|
|
|
|
create,
|
|
|
|
};
|
|
|
|
|
|
|
|
export type {
|
|
|
|
Logger,
|
|
|
|
IUnleash,
|
|
|
|
IUnleashOptions,
|
|
|
|
IUnleashConfig,
|
|
|
|
IUser,
|
|
|
|
IUnleashServices,
|
|
|
|
IAuthRequest,
|
2021-04-22 10:07:10 +02:00
|
|
|
};
|