1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/server-impl.ts

202 lines
5.6 KiB
TypeScript
Raw Normal View History

import stoppable, { StoppableServer } from 'stoppable';
import { promisify } from 'util';
import version from './util/version';
2021-09-10 11:42:11 +02:00
import { migrateDb } from '../migrator';
import getApp from './app';
import { createMetricsMonitor } from './metrics';
import { createStores } from './db';
import { createServices } from './services';
import { createConfig } from './create-config';
import { addEventHook } from './event-hook';
import registerGracefulShutdown from './util/graceful-shutdown';
import { createDb } from './db/db-pool';
import sessionDb from './middleware/session-db';
2021-08-13 19:22:15 +02:00
// Types
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';
import { SimpleAuthSettings } from './types/settings/simple-auth-settings';
2022-10-31 10:49:12 +01:00
import { Knex } from 'knex';
import * as permissions from './types/permissions';
import * as eventType from './types/events';
import { Db } from './db/db';
async function createApp(
config: IUnleashConfig,
startApp: boolean,
): Promise<IUnleash> {
// Database dependencies (stateful)
const logger = config.getLogger('server-impl.js');
const serverVersion = version;
const db = createDb(config);
2021-12-09 21:02:58 +01:00
const stores = createStores(config, db);
const services = createServices(stores, config);
const metricsMonitor = createMetricsMonitor();
const unleashSession = sessionDb(config, db);
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();
services.clientMetricsServiceV2.destroy();
services.proxyService.destroy();
await db.destroy();
};
if (!config.server.secret) {
const secret = await stores.settingStore.get('unleash.secret');
// eslint-disable-next-line no-param-reassign
config.server.secret = secret;
}
const app = await getApp(config, stores, services, unleashSession, db);
if (typeof config.eventHook === 'function') {
addEventHook(config.eventHook, stores.eventStore);
}
await metricsMonitor.startMonitoring(
2021-12-09 21:02:58 +01:00
config,
stores,
serverVersion,
config.eventBus,
services.instanceStatsService,
2021-12-09 21:02:58 +01:00
db,
);
const unleash: Omit<IUnleash, 'stop'> = {
stores,
2021-12-09 21:02:58 +01:00
eventBus: config.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,
});
}
if (config.environmentEnableOverrides?.length > 0) {
await services.environmentService.overrideEnabledProjects(
config.environmentEnableOverrides,
);
}
return new Promise((resolve, reject) => {
if (startApp) {
const server = stoppable(
app.listen(config.listen, () =>
logger.info('Unleash has started.', server.address()),
),
config.server.gracefulShutdownTimeout,
);
server.keepAliveTimeout = config.server.keepAliveTimeout;
server.headersTimeout = config.server.headersTimeout;
server.on('listening', () => {
resolve({
...unleash,
server,
stop: () => stopUnleash(server),
});
});
server.on('error', reject);
} else {
resolve({ ...unleash, stop: stopUnleash });
}
});
}
async function start(opts: IUnleashOptions = {}): Promise<IUnleash> {
const config = createConfig(opts);
const logger = config.getLogger('server-impl.js');
try {
if (config.db.disableMigration) {
logger.info('DB migration: disabled');
} else {
logger.debug('DB migration: start');
2021-09-10 11:42:11 +02:00
await migrateDb(config);
logger.debug('DB migration: end');
}
} catch (err) {
logger.error('Failed to migrate db', err);
throw err;
}
const unleash = await createApp(config, true);
if (config.server.gracefulShutdownEnable) {
registerGracefulShutdown(unleash, logger);
}
return unleash;
}
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);
}
} catch (err) {
logger.error('Failed to migrate db', err);
throw err;
}
return createApp(config, false);
}
2021-08-13 19:22:15 +02:00
export default {
start,
create,
};
2021-08-13 19:22:15 +02:00
export {
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,
RoleName,
IAuthType,
2022-10-31 10:49:12 +01:00
Knex,
Db,
permissions,
eventType,
2021-08-13 19:22:15 +02:00
};
export type {
Logger,
IUnleash,
IUnleashOptions,
IUnleashConfig,
IUser,
IUnleashServices,
IAuthRequest,
SimpleAuthSettings,
};