From 48fa39c9fcae6fdfcb21670dace7a7b255e2662c Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Mon, 11 Mar 2024 11:22:04 +0100 Subject: [PATCH] feat: Compositon root for proxy service (#6488) --- src/lib/proxy/createProxyService.ts | 108 ++++++++++++++++++++++ src/lib/proxy/proxy-repository.ts | 5 +- src/lib/proxy/proxy-service.ts | 5 +- src/lib/services/index.ts | 31 ++----- src/test/e2e/api/admin/config.e2e.test.ts | 14 ++- 5 files changed, 126 insertions(+), 37 deletions(-) create mode 100644 src/lib/proxy/createProxyService.ts diff --git a/src/lib/proxy/createProxyService.ts b/src/lib/proxy/createProxyService.ts new file mode 100644 index 0000000000..e8c1b25388 --- /dev/null +++ b/src/lib/proxy/createProxyService.ts @@ -0,0 +1,108 @@ +import { ProxyService } from './proxy-service'; +import { SegmentReadModel } from '../features/segment/segment-read-model'; +import ClientMetricsServiceV2 from '../features/metrics/client-metrics/metrics-service-v2'; +import SettingService from '../services/setting-service'; +import SettingStore from '../db/setting-store'; +import { + createEventsService, + createFakeEventsService, + createFakeFeatureToggleService, + createFeatureToggleService, +} from '../features'; +import ConfigurationRevisionService from '../features/feature-toggle/configuration-revision-service'; +import EventStore from '../features/events/event-store'; +import { GlobalFrontendApiCache } from './global-frontend-api-cache'; +import ClientFeatureToggleReadModel from './client-feature-toggle-read-model'; +import { FakeSegmentReadModel } from '../features/segment/fake-segment-read-model'; +import FakeSettingStore from '../../test/fixtures/fake-setting-store'; +import FakeEventStore from '../../test/fixtures/fake-event-store'; +import FakeClientFeatureToggleReadModel from './fake-client-feature-toggle-read-model'; +import { IUnleashConfig } from '../types'; +import { Db } from '../db/db'; + +export const createProxyService = ( + db: Db, + config: IUnleashConfig, + // client metrics service needs to be shared because it uses in-memory cache + clientMetricsServiceV2: ClientMetricsServiceV2, +): ProxyService => { + const segmentReadModel = new SegmentReadModel(db); + const settingStore = new SettingStore(db, config.getLogger); + const eventService = createEventsService(db, config); + const settingService = new SettingService( + { settingStore }, + config, + eventService, + ); + // TODO: remove this dependency after we migrate frontend API + const featureToggleServiceV2 = createFeatureToggleService(db, config); + const eventStore = new EventStore( + db, + config.getLogger, + config.flagResolver, + ); + const configurationRevisionService = new ConfigurationRevisionService( + { eventStore }, + config, + ); + const clientFeatureToggleReadModel = new ClientFeatureToggleReadModel( + db, + config.eventBus, + ); + const globalFrontendApiCache = new GlobalFrontendApiCache( + config, + segmentReadModel, + clientFeatureToggleReadModel, + configurationRevisionService, + ); + return new ProxyService( + config, + { segmentReadModel }, + { + featureToggleServiceV2, + clientMetricsServiceV2, + settingService, + configurationRevisionService, + }, + globalFrontendApiCache, + ); +}; + +export const createFakeProxyService = ( + config: IUnleashConfig, + clientMetricsServiceV2: ClientMetricsServiceV2, +): ProxyService => { + const segmentReadModel = new FakeSegmentReadModel(); + const settingStore = new FakeSettingStore(); + const eventService = createFakeEventsService(config); + const settingService = new SettingService( + { settingStore }, + config, + eventService, + ); + // TODO: remove this dependency after we migrate frontend API + const featureToggleServiceV2 = createFakeFeatureToggleService(config); + const eventStore = new FakeEventStore(); + const configurationRevisionService = new ConfigurationRevisionService( + { eventStore }, + config, + ); + const clientFeatureToggleReadModel = new FakeClientFeatureToggleReadModel(); + const globalFrontendApiCache = new GlobalFrontendApiCache( + config, + segmentReadModel, + clientFeatureToggleReadModel, + configurationRevisionService, + ); + return new ProxyService( + config, + { segmentReadModel }, + { + featureToggleServiceV2, + clientMetricsServiceV2, + settingService, + configurationRevisionService, + }, + globalFrontendApiCache, + ); +}; diff --git a/src/lib/proxy/proxy-repository.ts b/src/lib/proxy/proxy-repository.ts index 1780d4b436..c025b75a4c 100644 --- a/src/lib/proxy/proxy-repository.ts +++ b/src/lib/proxy/proxy-repository.ts @@ -21,10 +21,7 @@ import { PROXY_FEATURES_FOR_TOKEN_TIME } from '../metric-events'; type Config = Pick; -type Stores = Pick< - IUnleashStores, - 'projectStore' | 'eventStore' | 'segmentReadModel' ->; +type Stores = Pick; type Services = Pick< IUnleashServices, diff --git a/src/lib/proxy/proxy-service.ts b/src/lib/proxy/proxy-service.ts index 4c175aaea0..eb6c6bfaef 100644 --- a/src/lib/proxy/proxy-service.ts +++ b/src/lib/proxy/proxy-service.ts @@ -25,10 +25,7 @@ export type Config = Pick< 'getLogger' | 'frontendApi' | 'frontendApiOrigins' | 'eventBus' >; -export type Stores = Pick< - IUnleashStores, - 'projectStore' | 'eventStore' | 'segmentReadModel' ->; +export type Stores = Pick; export type Services = Pick< IUnleashServices, diff --git a/src/lib/services/index.ts b/src/lib/services/index.ts index 1c31212c5c..5982c14451 100644 --- a/src/lib/services/index.ts +++ b/src/lib/services/index.ts @@ -111,9 +111,10 @@ import { import { InactiveUsersService } from '../users/inactive/inactive-users-service'; import { SegmentReadModel } from '../features/segment/segment-read-model'; import { FakeSegmentReadModel } from '../features/segment/fake-segment-read-model'; -import { GlobalFrontendApiCache } from '../proxy/global-frontend-api-cache'; -import ClientFeatureToggleReadModel from '../proxy/client-feature-toggle-read-model'; -import FakeClientFeatureToggleReadModel from '../proxy/fake-client-feature-toggle-read-model'; +import { + createFakeProxyService, + createProxyService, +} from '../proxy/createProxyService'; export const createServices = ( stores: IUnleashStores, @@ -296,27 +297,9 @@ export const createServices = ( ? createClientFeatureToggleService(db, config) : createFakeClientFeatureToggleService(config); - const clientFeatureToggleReadModel = db - ? new ClientFeatureToggleReadModel(db, config.eventBus) - : new FakeClientFeatureToggleReadModel(); - const globalFrontendApiCache = new GlobalFrontendApiCache( - config, - segmentReadModel, - clientFeatureToggleReadModel, - configurationRevisionService, - ); - - const proxyService = new ProxyService( - config, - stores, - { - featureToggleServiceV2, - clientMetricsServiceV2, - settingService, - configurationRevisionService, - }, - globalFrontendApiCache, - ); + const proxyService = db + ? createProxyService(db, config, clientMetricsServiceV2) + : createFakeProxyService(config, clientMetricsServiceV2); const edgeService = new EdgeService({ apiTokenService }, config); diff --git a/src/test/e2e/api/admin/config.e2e.test.ts b/src/test/e2e/api/admin/config.e2e.test.ts index 8ef084d5c0..2170d02372 100644 --- a/src/test/e2e/api/admin/config.e2e.test.ts +++ b/src/test/e2e/api/admin/config.e2e.test.ts @@ -12,13 +12,17 @@ let app: IUnleashTest; beforeAll(async () => { db = await dbInit('config_api_serial', getLogger); - app = await setupAppWithCustomConfig(db.stores, { - experimental: { - flags: { - strictSchemaValidation: true, + app = await setupAppWithCustomConfig( + db.stores, + { + experimental: { + flags: { + strictSchemaValidation: true, + }, }, }, - }); + db.rawDatabase, + ); }); afterAll(async () => {