1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-04 00:18:01 +01:00

fix: add support for experimental flags (#1025)

This commit is contained in:
Ivar Conradi Østhus 2021-10-13 20:52:44 +02:00 committed by GitHub
parent 1ce913ddf1
commit ae03390331
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 197 additions and 8 deletions

View File

@ -0,0 +1,81 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`should create default config 1`] = `
Object {
"authentication": Object {
"createAdminUser": true,
"customAuthHandler": [Function],
"enableApiToken": true,
"type": "open-source",
},
"db": Object {
"acquireConnectionTimeout": 30000,
"database": "unleash_db",
"disableMigration": false,
"driver": "postgres",
"host": "localhost",
"password": "password",
"pool": Object {
"idleTimeoutMillis": 30000,
"max": 4,
"min": 0,
"propagateCreateError": false,
},
"port": 4242,
"schema": "public",
"ssl": Object {
"rejectUnauthorized": false,
},
"user": "unleash",
"version": undefined,
},
"email": Object {
"host": undefined,
"port": 587,
"secure": false,
"sender": "noreply@unleash-hosted.com",
"smtppass": undefined,
"smtpuser": undefined,
},
"enableOAS": false,
"enterpriseVersion": undefined,
"eventHook": undefined,
"experimental": Object {},
"getLogger": [Function],
"import": Object {
"dropBeforeImport": false,
"file": undefined,
"keepExisting": false,
},
"listen": Object {
"host": undefined,
"port": 4242,
},
"preHook": undefined,
"preRouterHook": undefined,
"secureHeaders": false,
"server": Object {
"baseUriPath": "",
"enableRequestLogger": false,
"gracefulShutdownEnable": true,
"gracefulShutdownTimeout": 1000,
"headersTimeout": 61000,
"host": undefined,
"keepAliveTimeout": 60000,
"pipe": undefined,
"port": 4242,
"secret": "super-secret",
"serverMetrics": true,
"unleashUrl": "http://localhost:4242",
},
"session": Object {
"db": true,
"ttlHours": 48,
},
"ui": Object {},
"versionCheck": Object {
"enable": true,
"url": "https://version.unleash.run",
},
}
`;

View File

@ -0,0 +1,66 @@
// @ts-nocheck
import { createConfig } from './create-config';
test('should create default config', async () => {
const config = createConfig({
db: {
host: 'localhost',
port: 4242,
user: 'unleash',
password: 'password',
database: 'unleash_db',
},
server: {
port: 4242,
},
});
expect(config).toMatchSnapshot();
});
test('should enabled metricsV2 via options', async () => {
const config = createConfig({
experimental: {
metricsV2: { enabled: true },
},
});
expect(config.experimental.metricsV2.enabled).toBe(true);
});
test('should enabled metricsV2 via env variable', async () => {
process.env.EXP_METRICS_V2 = 'true';
const config = createConfig({});
expect(config.experimental.metricsV2.enabled).toBe(true);
delete process.env.EXP_METRICS_V2;
});
test('should enabled metricsV2 when environments is enabled via env variable', async () => {
process.env.EXP_ENVIRONMENTS = 'true';
const config = createConfig({});
expect(config.experimental.environments.enabled).toBe(true);
expect(config.experimental.metricsV2.enabled).toBe(true);
delete process.env.EXP_ENVIRONMENTS;
});
test('should enabled metricsV2 when environments is enabled via options', async () => {
const config = createConfig({
experimental: {
environments: { enabled: true },
},
});
expect(config.experimental.environments.enabled).toBe(true);
expect(config.experimental.metricsV2.enabled).toBe(true);
});
test('should set UI flag when environments is enabled', async () => {
process.env.EXP_ENVIRONMENTS = 'true';
const config = createConfig({});
expect(config.experimental.environments.enabled).toBe(true);
expect(config.ui.flags?.E).toBe(true);
delete process.env.EXP_ENVIRONMENTS;
});

View File

@ -14,6 +14,7 @@ import {
IEmailOption, IEmailOption,
IListeningPipe, IListeningPipe,
IListeningHost, IListeningHost,
IUIConfig,
} from './types/option'; } from './types/option';
import { getDefaultLogProvider, LogLevel, validateLogProvider } from './logger'; import { getDefaultLogProvider, LogLevel, validateLogProvider } from './logger';
import { defaultCustomAuthDenyAll } from './default-custom-auth-deny-all'; import { defaultCustomAuthDenyAll } from './default-custom-auth-deny-all';
@ -51,6 +52,36 @@ function mergeAll<T>(objects: Partial<T>[]): T {
return merge.all<T>(objects.filter((i) => i)); return merge.all<T>(objects.filter((i) => i));
} }
function loadExperimental(options: IUnleashOptions): any {
const experimental = options.experimental || {};
if (safeBoolean(process.env.EXP_ENVIRONMENTS, false)) {
experimental.environments = { enabled: true };
}
if (
safeBoolean(process.env.EXP_METRICS_V2, false) ||
//@ts-ignore
experimental.environments?.enabled
) {
experimental.metricsV2 = { enabled: true };
}
return experimental;
}
function loadUI(options: IUnleashOptions, experimental: any = {}): IUIConfig {
const uiO = options.ui || {};
const ui: IUIConfig = {};
if (experimental.environments?.enabled) {
ui.flags = {
E: true,
};
}
return mergeAll([uiO, ui]);
}
const defaultDbOptions: IDBOption = { const defaultDbOptions: IDBOption = {
user: process.env.DATABASE_USERNAME, user: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD, password: process.env.DATABASE_PASSWORD,
@ -215,18 +246,14 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {
: options.authentication, : options.authentication,
]); ]);
const ui = options.ui || {};
const importSetting: IImportOption = mergeAll([ const importSetting: IImportOption = mergeAll([
defaultImport, defaultImport,
options.import, options.import,
]); ]);
const experimental = options.experimental || {}; const experimental = loadExperimental(options);
if (safeBoolean(process.env.EXP_METRICS_V2, false)) { const ui = loadUI(options, experimental);
experimental.metricsV2 = { enabled: true };
}
const email: IEmailOption = mergeAll([defaultEmail, options.email]); const email: IEmailOption = mergeAll([defaultEmail, options.email]);
@ -253,7 +280,7 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {
authentication, authentication,
ui, ui,
import: importSetting, import: importSetting,
experimental: experimental || {}, experimental,
email, email,
secureHeaders, secureHeaders,
enableOAS, enableOAS,

View File

@ -111,11 +111,26 @@ export interface IEmailOption {
export interface IListeningPipe { export interface IListeningPipe {
path: string; path: string;
} }
export interface IListeningHost { export interface IListeningHost {
host?: string; host?: string;
port: number; port: number;
} }
export interface IUIConfig {
slogan?: string;
name?: string;
flags?: { [key: string]: boolean };
links?: [
{
value: string;
icon?: string;
href: string;
title: string;
},
];
}
export interface IUnleashConfig { export interface IUnleashConfig {
db: IDBOption; db: IDBOption;
session: ISessionOption; session: ISessionOption;
@ -124,7 +139,7 @@ export interface IUnleashConfig {
listen: IListeningHost | IListeningPipe; listen: IListeningHost | IListeningPipe;
versionCheck: IVersionOption; versionCheck: IVersionOption;
authentication: IAuthOption; authentication: IAuthOption;
ui: object; ui: IUIConfig;
import: IImportOption; import: IImportOption;
experimental: { experimental: {
[key: string]: object; [key: string]: object;