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';
|
2023-01-30 11:13:17 +01:00
|
|
|
import { createServices, scheduleServices } 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
|
2022-11-17 12:02:40 +01:00
|
|
|
import {
|
|
|
|
IAuthType,
|
|
|
|
IUnleash,
|
|
|
|
IUnleashConfig,
|
|
|
|
IUnleashOptions,
|
|
|
|
IUnleashServices,
|
|
|
|
RoleName,
|
|
|
|
} from './types';
|
|
|
|
|
2021-08-13 19:22:15 +02:00
|
|
|
import User, { IUser } from './types/user';
|
2022-01-04 20:01:18 +01:00
|
|
|
import ApiUser from './types/api-user';
|
2021-08-13 19:22:15 +02:00
|
|
|
import { Logger, LogLevel } from './logger';
|
|
|
|
import AuthenticationRequired from './types/authentication-required';
|
|
|
|
import Controller from './routes/controller';
|
|
|
|
import { IAuthRequest } from './routes/unleash-types';
|
2021-10-29 10:25:42 +02:00
|
|
|
import { SimpleAuthSettings } from './types/settings/simple-auth-settings';
|
2022-10-31 10:49:12 +01:00
|
|
|
import { Knex } from 'knex';
|
2022-11-21 09:42:43 +01:00
|
|
|
import * as permissions from './types/permissions';
|
|
|
|
import * as eventType from './types/events';
|
2023-01-30 09:02:44 +01:00
|
|
|
import { Db } from './db/db';
|
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;
|
2021-08-12 15:04:37 +02:00
|
|
|
const db = createDb(config);
|
2021-12-09 21:02:58 +01:00
|
|
|
const stores = createStores(config, db);
|
2023-02-16 08:08:51 +01:00
|
|
|
const services = createServices(stores, config, db);
|
2023-01-30 11:13:17 +01:00
|
|
|
scheduleServices(services, 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();
|
|
|
|
}
|
2023-02-16 10:20:38 +01:00
|
|
|
services.schedulerService.stop();
|
2021-06-17 20:33:34 +02:00
|
|
|
metricsMonitor.stopMonitoring();
|
2021-06-18 11:53:52 +02:00
|
|
|
stores.clientInstanceStore.destroy();
|
2021-10-08 10:09:22 +02:00
|
|
|
services.clientMetricsServiceV2.destroy();
|
2022-12-14 17:35:22 +01:00
|
|
|
services.proxyService.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;
|
|
|
|
}
|
2022-10-31 10:35:59 +01:00
|
|
|
const app = await getApp(config, stores, services, unleashSession, db);
|
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);
|
|
|
|
}
|
2022-09-15 09:47:57 +02:00
|
|
|
await metricsMonitor.startMonitoring(
|
2021-12-09 21:02:58 +01:00
|
|
|
config,
|
|
|
|
stores,
|
|
|
|
serverVersion,
|
|
|
|
config.eventBus,
|
2022-10-25 13:10:27 +02:00
|
|
|
services.instanceStatsService,
|
2021-12-09 21:02:58 +01:00
|
|
|
db,
|
|
|
|
);
|
2021-04-22 10:07:10 +02:00
|
|
|
const unleash: Omit<IUnleash, 'stop'> = {
|
|
|
|
stores,
|
2021-12-09 21:02:58 +01:00
|
|
|
eventBus: config.eventBus,
|
2021-04-22 10:07:10 +02:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-11 10:16:58 +01:00
|
|
|
if (config.environmentEnableOverrides?.length > 0) {
|
|
|
|
await services.environmentService.overrideEnabledProjects(
|
|
|
|
config.environmentEnableOverrides,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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-15 20:28:10 +02:00
|
|
|
logger.debug('DB migration: start');
|
2021-09-10 11:42:11 +02:00
|
|
|
await migrateDb(config);
|
2021-09-15 20:28:10 +02:00
|
|
|
logger.debug('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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-11-17 12:02:40 +01:00
|
|
|
export default {
|
|
|
|
start,
|
|
|
|
create,
|
|
|
|
};
|
|
|
|
|
2021-08-13 19:22:15 +02:00
|
|
|
export {
|
2021-04-22 10:07:10 +02:00
|
|
|
start,
|
|
|
|
create,
|
2021-08-13 19:22:15 +02:00
|
|
|
Controller,
|
|
|
|
AuthenticationRequired,
|
|
|
|
User,
|
2022-01-04 20:01:18 +01:00
|
|
|
ApiUser,
|
2021-08-13 19:22:15 +02:00
|
|
|
LogLevel,
|
2021-08-23 12:11:29 +02:00
|
|
|
RoleName,
|
2021-09-13 15:57:38 +02:00
|
|
|
IAuthType,
|
2022-10-31 10:49:12 +01:00
|
|
|
Knex,
|
2023-01-30 09:02:44 +01:00
|
|
|
Db,
|
2022-11-21 09:42:43 +01:00
|
|
|
permissions,
|
|
|
|
eventType,
|
2021-08-13 19:22:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export type {
|
|
|
|
Logger,
|
|
|
|
IUnleash,
|
|
|
|
IUnleashOptions,
|
|
|
|
IUnleashConfig,
|
|
|
|
IUser,
|
|
|
|
IUnleashServices,
|
|
|
|
IAuthRequest,
|
2021-10-29 10:25:42 +02:00
|
|
|
SimpleAuthSettings,
|
2021-04-22 10:07:10 +02:00
|
|
|
};
|