Skip to content

Commit

Permalink
feat: combine services enterprise
Browse files Browse the repository at this point in the history
  • Loading branch information
FredrikOseberg committed Feb 22, 2024
1 parent 0ccfc29 commit 9b3d9fc
Show file tree
Hide file tree
Showing 22 changed files with 47 additions and 46 deletions.
12 changes: 9 additions & 3 deletions src/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export default async function getApp(
services: IUnleashServices,
unleashSession?: RequestHandler,
db?: Knex,
): Promise<Application> {
): Promise<{ app: Application; services: unknown; stores: unknown }> {
let combinedServices = services;
let combinedStores = stores;
const app = express();

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

if (typeof config.preRouterHook === 'function') {
config.preRouterHook(app, config, services, stores, db);
const { services: enterpriseServices, stores: enterpriseStores } =
config.preRouterHook(app, config, services, stores, db);

combinedServices = enterpriseServices;
combinedStores = enterpriseStores;
}

// Setup API routes
Expand Down Expand Up @@ -212,5 +218,5 @@ export default async function getApp(
res.send(indexHTML);
});

return app;
return { app, stores: combinedStores, services: combinedServices };
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function getSetup() {
});
const services = createServices(stores, config);

app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

return {
base,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/features/metrics/instance/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function getSetup(opts?: IUnleashOptions) {
db = await dbInit('metrics', config.getLogger);

const services = createServices(db.stores, config, db.rawDatabase);
const app = await getApp(config, db.stores, services);
const { app } = await getApp(config, db.stores, services);
return {
request: supertest(app),
stores: db.stores,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/features/metrics/instance/register.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function getSetup() {
const stores = createStores();
const config = createTestConfig();
const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

return {
request: supertest(app),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/features/playground/playground.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function getSetup() {
experimental: { flags: { strictSchemaValidation: true } },
});
const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);
return { base, request: supertest(app) };
}
describe('toggle generator', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/middleware/oss-authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function getSetup(preRouterHook) {
const stores = createStores();
const services = createServices(stores, config);
const unleashSession = sessionDb(config, {} as Knex);
const app = await getApp(config, stores, services, unleashSession);
const { app } = await getApp(config, stores, services, unleashSession);

return {
base,
Expand Down
32 changes: 11 additions & 21 deletions src/lib/proxy/proxy-repository.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import EventEmitter from 'events';
import { RepositoryInterface } from 'unleash-client/lib/repository';
import { Segment } from 'unleash-client/lib/strategy/strategy';
import {
EnhancedFeatureInterface,
FeatureInterface,
} from 'unleash-client/lib/feature';
import { FeatureInterface } from 'unleash-client/lib/feature';
import { IApiUser } from '../types/api-user';
import { IUnleashConfig, IUnleashServices, IUnleashStores } from '../types';
import {
Expand Down Expand Up @@ -67,15 +64,10 @@ export class ProxyRepository
this.configurationRevisionService =
services.configurationRevisionService;
this.token = token;
this.onUpdateRevisionEvent = this.onUpdateRevisionEvent.bind(this);
this.onAnyEvent = this.onAnyEvent.bind(this);
this.interval = config.frontendApi.refreshIntervalInMs;
}

getTogglesWithSegmentData(): EnhancedFeatureInterface[] {
// TODO: add real implementation
return [];
}

getSegment(id: number): Segment | undefined {
return this.segments.find((segment) => segment.id === id);
}
Expand All @@ -95,20 +87,14 @@ export class ProxyRepository

// Reload cached token data whenever something relevant has changed.
// For now, simply reload all the data on any EventStore event.
this.configurationRevisionService.on(
UPDATE_REVISION,
this.onUpdateRevisionEvent,
);
this.configurationRevisionService.on(UPDATE_REVISION, this.onAnyEvent);

this.emit(UnleashEvents.Ready);
this.emit(UnleashEvents.Changed);
}

stop(): void {
this.configurationRevisionService.off(
UPDATE_REVISION,
this.onUpdateRevisionEvent,
);
this.configurationRevisionService.off(UPDATE_REVISION, this.onAnyEvent);
this.running = false;
}

Expand Down Expand Up @@ -136,16 +122,20 @@ export class ProxyRepository
this.features = await this.featuresForToken();
this.segments = await this.segmentsForToken();
} catch (e) {
this.logger.error('Cannot load data for token', e);
this.logger.error(e);
}
}

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

private async onUpdateRevisionEvent() {
await this.loadDataForToken();
private async onAnyEvent() {
try {
await this.loadDataForToken();
} catch (error) {
this.logger.error(error);
}
}

private async featuresForToken(): Promise<FeatureInterface[]> {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/admin-api/api-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function getSetup(adminTokenKillSwitchEnabled: boolean) {
enabled: true,
});
const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

return {
base,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/admin-api/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function getSetup() {
const stores = createStores();
const services = createServices(stores, config);

const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

return {
base,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/admin-api/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function getSetup() {
const stores = createStores();

const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

return {
base,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/admin-api/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function getSetup() {
});

const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

return {
base,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/admin-api/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function getSetup(anonymise: boolean = false) {
experimental: { flags: { anonymiseEventLog: anonymise } },
});
const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

return {
base,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/admin-api/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function getSetup() {
preRouterHook: perms.hook,
});
const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

return {
request: supertest(app),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/admin-api/public-signup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Public Signup API', () => {
};

const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

await stores.roleStore.create({
name: RoleName.VIEWER,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/admin-api/strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async function getSetup() {
preRouterHook: perms.hook,
});
const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

return {
base: randomBase,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/admin-api/tag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function getSetup() {
preRouterHook: perms.hook,
});
const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

return {
base,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/admin-api/user/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function getSetup() {
server: { baseUriPath: base },
});
const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);
return {
base,
userStore: stores.userStore,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/backstage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('should enable prometheus', async () => {
const config = createTestConfig();
const services = createServices(stores, config);

const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

const request = supertest(app);

Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/health-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function getSetup() {
const stores = createStores();
const config = createTestConfig();
const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

return {
request: supertest(app),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/routes/public-invite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Public Signup API', () => {
};

const services = createServices(stores, config);
const app = await getApp(config, stores, services);
const { app } = await getApp(config, stores, services);

await stores.roleStore.create({
name: RoleName.VIEWER,
Expand Down
11 changes: 8 additions & 3 deletions src/lib/server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
RoleName,
CustomAuthHandler,
SYSTEM_USER,
IUnleashStores,
} from './types';

import User, { IUser } from './types/user';
Expand Down Expand Up @@ -68,7 +69,11 @@ async function createApp(
const secret = await stores.settingStore.get<string>('unleash.secret');
config.server.secret = secret!;
}
const app = await getApp(config, stores, services, unleashSession, db);
const {
app,
services: combinedServices,
stores: combinedStores,
} = await getApp(config, stores, services, unleashSession, db);

await metricsMonitor.startMonitoring(
config,
Expand All @@ -80,9 +85,9 @@ async function createApp(
db,
);
const unleash: Omit<IUnleash, 'stop'> = {
stores,
stores: combinedStores as IUnleashStores,
eventBus: config.eventBus,
services,
services: combinedServices as IUnleashServices,
app,
config,
version: serverVersion,
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/helpers/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ async function createApp(
const unleashSession = sessionDb(config, undefined);
const emitter = new EventEmitter();
emitter.setMaxListeners(0);
const app = await getApp(config, stores, services, unleashSession, db);
const { app } = await getApp(config, stores, services, unleashSession, db);
const request = supertest.agent(app);

const destroy = async () => {
Expand Down

0 comments on commit 9b3d9fc

Please sign in to comment.