Skip to content

Commit 9b3d9fc

Browse files
feat: combine services enterprise
1 parent 0ccfc29 commit 9b3d9fc

22 files changed

+47
-46
lines changed

src/lib/app.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export default async function getApp(
3535
services: IUnleashServices,
3636
unleashSession?: RequestHandler,
3737
db?: Knex,
38-
): Promise<Application> {
38+
): Promise<{ app: Application; services: unknown; stores: unknown }> {
39+
let combinedServices = services;
40+
let combinedStores = stores;
3941
const app = express();
4042

4143
const baseUriPath = config.server.baseUriPath || '';
@@ -179,7 +181,11 @@ export default async function getApp(
179181
);
180182

181183
if (typeof config.preRouterHook === 'function') {
182-
config.preRouterHook(app, config, services, stores, db);
184+
const { services: enterpriseServices, stores: enterpriseStores } =
185+
config.preRouterHook(app, config, services, stores, db);
186+
187+
combinedServices = enterpriseServices;
188+
combinedStores = enterpriseStores;
183189
}
184190

185191
// Setup API routes
@@ -212,5 +218,5 @@ export default async function getApp(
212218
res.send(indexHTML);
213219
});
214220

215-
return app;
221+
return { app, stores: combinedStores, services: combinedServices };
216222
}

src/lib/features/client-feature-toggles/tests/client-feature-toggle.e2e.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function getSetup() {
2121
});
2222
const services = createServices(stores, config);
2323

24-
app = await getApp(config, stores, services);
24+
const { app } = await getApp(config, stores, services);
2525

2626
return {
2727
base,

src/lib/features/metrics/instance/metrics.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ async function getSetup(opts?: IUnleashOptions) {
2121
db = await dbInit('metrics', config.getLogger);
2222

2323
const services = createServices(db.stores, config, db.rawDatabase);
24-
const app = await getApp(config, db.stores, services);
24+
const { app } = await getApp(config, db.stores, services);
2525
return {
2626
request: supertest(app),
2727
stores: db.stores,

src/lib/features/metrics/instance/register.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async function getSetup() {
1010
const stores = createStores();
1111
const config = createTestConfig();
1212
const services = createServices(stores, config);
13-
const app = await getApp(config, stores, services);
13+
const { app } = await getApp(config, stores, services);
1414

1515
return {
1616
request: supertest(app),

src/lib/features/playground/playground.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function getSetup() {
2323
experimental: { flags: { strictSchemaValidation: true } },
2424
});
2525
const services = createServices(stores, config);
26-
const app = await getApp(config, stores, services);
26+
const { app } = await getApp(config, stores, services);
2727
return { base, request: supertest(app) };
2828
}
2929
describe('toggle generator', () => {

src/lib/middleware/oss-authentication.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function getSetup(preRouterHook) {
2727
const stores = createStores();
2828
const services = createServices(stores, config);
2929
const unleashSession = sessionDb(config, {} as Knex);
30-
const app = await getApp(config, stores, services, unleashSession);
30+
const { app } = await getApp(config, stores, services, unleashSession);
3131

3232
return {
3333
base,

src/lib/proxy/proxy-repository.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import EventEmitter from 'events';
22
import { RepositoryInterface } from 'unleash-client/lib/repository';
33
import { Segment } from 'unleash-client/lib/strategy/strategy';
4-
import {
5-
EnhancedFeatureInterface,
6-
FeatureInterface,
7-
} from 'unleash-client/lib/feature';
4+
import { FeatureInterface } from 'unleash-client/lib/feature';
85
import { IApiUser } from '../types/api-user';
96
import { IUnleashConfig, IUnleashServices, IUnleashStores } from '../types';
107
import {
@@ -67,15 +64,10 @@ export class ProxyRepository
6764
this.configurationRevisionService =
6865
services.configurationRevisionService;
6966
this.token = token;
70-
this.onUpdateRevisionEvent = this.onUpdateRevisionEvent.bind(this);
67+
this.onAnyEvent = this.onAnyEvent.bind(this);
7168
this.interval = config.frontendApi.refreshIntervalInMs;
7269
}
7370

74-
getTogglesWithSegmentData(): EnhancedFeatureInterface[] {
75-
// TODO: add real implementation
76-
return [];
77-
}
78-
7971
getSegment(id: number): Segment | undefined {
8072
return this.segments.find((segment) => segment.id === id);
8173
}
@@ -95,20 +87,14 @@ export class ProxyRepository
9587

9688
// Reload cached token data whenever something relevant has changed.
9789
// For now, simply reload all the data on any EventStore event.
98-
this.configurationRevisionService.on(
99-
UPDATE_REVISION,
100-
this.onUpdateRevisionEvent,
101-
);
90+
this.configurationRevisionService.on(UPDATE_REVISION, this.onAnyEvent);
10291

10392
this.emit(UnleashEvents.Ready);
10493
this.emit(UnleashEvents.Changed);
10594
}
10695

10796
stop(): void {
108-
this.configurationRevisionService.off(
109-
UPDATE_REVISION,
110-
this.onUpdateRevisionEvent,
111-
);
97+
this.configurationRevisionService.off(UPDATE_REVISION, this.onAnyEvent);
11298
this.running = false;
11399
}
114100

@@ -136,16 +122,20 @@ export class ProxyRepository
136122
this.features = await this.featuresForToken();
137123
this.segments = await this.segmentsForToken();
138124
} catch (e) {
139-
this.logger.error('Cannot load data for token', e);
125+
this.logger.error(e);
140126
}
141127
}
142128

143129
private randomizeDelay(floor: number, ceiling: number): number {
144130
return Math.floor(Math.random() * (ceiling - floor) + floor);
145131
}
146132

147-
private async onUpdateRevisionEvent() {
148-
await this.loadDataForToken();
133+
private async onAnyEvent() {
134+
try {
135+
await this.loadDataForToken();
136+
} catch (error) {
137+
this.logger.error(error);
138+
}
149139
}
150140

151141
private async featuresForToken(): Promise<FeatureInterface[]> {

src/lib/routes/admin-api/api-token.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function getSetup(adminTokenKillSwitchEnabled: boolean) {
2626
enabled: true,
2727
});
2828
const services = createServices(stores, config);
29-
const app = await getApp(config, stores, services);
29+
const { app } = await getApp(config, stores, services);
3030

3131
return {
3232
base,

src/lib/routes/admin-api/config.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function getSetup() {
2525
const stores = createStores();
2626
const services = createServices(stores, config);
2727

28-
const app = await getApp(config, stores, services);
28+
const { app } = await getApp(config, stores, services);
2929

3030
return {
3131
base,

src/lib/routes/admin-api/context.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async function getSetup() {
1616
const stores = createStores();
1717

1818
const services = createServices(stores, config);
19-
const app = await getApp(config, stores, services);
19+
const { app } = await getApp(config, stores, services);
2020

2121
return {
2222
base,

src/lib/routes/admin-api/email.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async function getSetup() {
1515
});
1616

1717
const services = createServices(stores, config);
18-
const app = await getApp(config, stores, services);
18+
const { app } = await getApp(config, stores, services);
1919

2020
return {
2121
base,

src/lib/routes/admin-api/events.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function getSetup(anonymise: boolean = false) {
2020
experimental: { flags: { anonymiseEventLog: anonymise } },
2121
});
2222
const services = createServices(stores, config);
23-
const app = await getApp(config, stores, services);
23+
const { app } = await getApp(config, stores, services);
2424

2525
return {
2626
base,

src/lib/routes/admin-api/metrics.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async function getSetup() {
1414
preRouterHook: perms.hook,
1515
});
1616
const services = createServices(stores, config);
17-
const app = await getApp(config, stores, services);
17+
const { app } = await getApp(config, stores, services);
1818

1919
return {
2020
request: supertest(app),

src/lib/routes/admin-api/public-signup.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Public Signup API', () => {
2323
};
2424

2525
const services = createServices(stores, config);
26-
const app = await getApp(config, stores, services);
26+
const { app } = await getApp(config, stores, services);
2727

2828
await stores.roleStore.create({
2929
name: RoleName.VIEWER,

src/lib/routes/admin-api/strategy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async function getSetup() {
1414
preRouterHook: perms.hook,
1515
});
1616
const services = createServices(stores, config);
17-
const app = await getApp(config, stores, services);
17+
const { app } = await getApp(config, stores, services);
1818

1919
return {
2020
base: randomBase,

src/lib/routes/admin-api/tag.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async function getSetup() {
1616
preRouterHook: perms.hook,
1717
});
1818
const services = createServices(stores, config);
19-
const app = await getApp(config, stores, services);
19+
const { app } = await getApp(config, stores, services);
2020

2121
return {
2222
base,

src/lib/routes/admin-api/user/user.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function getSetup() {
2929
server: { baseUriPath: base },
3030
});
3131
const services = createServices(stores, config);
32-
const app = await getApp(config, stores, services);
32+
const { app } = await getApp(config, stores, services);
3333
return {
3434
base,
3535
userStore: stores.userStore,

src/lib/routes/backstage.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test('should enable prometheus', async () => {
1111
const config = createTestConfig();
1212
const services = createServices(stores, config);
1313

14-
const app = await getApp(config, stores, services);
14+
const { app } = await getApp(config, stores, services);
1515

1616
const request = supertest(app);
1717

src/lib/routes/health-check.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async function getSetup() {
1111
const stores = createStores();
1212
const config = createTestConfig();
1313
const services = createServices(stores, config);
14-
const app = await getApp(config, stores, services);
14+
const { app } = await getApp(config, stores, services);
1515

1616
return {
1717
request: supertest(app),

src/lib/routes/public-invite.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Public Signup API', () => {
3030
};
3131

3232
const services = createServices(stores, config);
33-
const app = await getApp(config, stores, services);
33+
const { app } = await getApp(config, stores, services);
3434

3535
await stores.roleStore.create({
3636
name: RoleName.VIEWER,

src/lib/server-impl.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
RoleName,
2121
CustomAuthHandler,
2222
SYSTEM_USER,
23+
IUnleashStores,
2324
} from './types';
2425

2526
import User, { IUser } from './types/user';
@@ -68,7 +69,11 @@ async function createApp(
6869
const secret = await stores.settingStore.get<string>('unleash.secret');
6970
config.server.secret = secret!;
7071
}
71-
const app = await getApp(config, stores, services, unleashSession, db);
72+
const {
73+
app,
74+
services: combinedServices,
75+
stores: combinedStores,
76+
} = await getApp(config, stores, services, unleashSession, db);
7277

7378
await metricsMonitor.startMonitoring(
7479
config,
@@ -80,9 +85,9 @@ async function createApp(
8085
db,
8186
);
8287
const unleash: Omit<IUnleash, 'stop'> = {
83-
stores,
88+
stores: combinedStores as IUnleashStores,
8489
eventBus: config.eventBus,
85-
services,
90+
services: combinedServices as IUnleashServices,
8691
app,
8792
config,
8893
version: serverVersion,

src/test/e2e/helpers/test-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ async function createApp(
323323
const unleashSession = sessionDb(config, undefined);
324324
const emitter = new EventEmitter();
325325
emitter.setMaxListeners(0);
326-
const app = await getApp(config, stores, services, unleashSession, db);
326+
const { app } = await getApp(config, stores, services, unleashSession, db);
327327
const request = supertest.agent(app);
328328

329329
const destroy = async () => {

0 commit comments

Comments
 (0)