diff --git a/src/lib/app.ts b/src/lib/app.ts index 6e5ed9e5b450..f30fc681833e 100644 --- a/src/lib/app.ts +++ b/src/lib/app.ts @@ -35,7 +35,9 @@ export default async function getApp( services: IUnleashServices, unleashSession?: RequestHandler, db?: Knex, -): Promise { +): Promise<{ app: Application; services: unknown; stores: unknown }> { + let combinedServices = services; + let combinedStores = stores; const app = express(); const baseUriPath = config.server.baseUriPath || ''; @@ -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 @@ -212,5 +218,5 @@ export default async function getApp( res.send(indexHTML); }); - return app; + return { app, stores: combinedStores, services: combinedServices }; } diff --git a/src/lib/features/client-feature-toggles/tests/client-feature-toggle.e2e.test.ts b/src/lib/features/client-feature-toggles/tests/client-feature-toggle.e2e.test.ts index 7f21a2d8e1e9..8c5416702801 100644 --- a/src/lib/features/client-feature-toggles/tests/client-feature-toggle.e2e.test.ts +++ b/src/lib/features/client-feature-toggles/tests/client-feature-toggle.e2e.test.ts @@ -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, diff --git a/src/lib/features/metrics/instance/metrics.test.ts b/src/lib/features/metrics/instance/metrics.test.ts index ea6802f5efb6..8bbe19048e35 100644 --- a/src/lib/features/metrics/instance/metrics.test.ts +++ b/src/lib/features/metrics/instance/metrics.test.ts @@ -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, diff --git a/src/lib/features/metrics/instance/register.test.ts b/src/lib/features/metrics/instance/register.test.ts index fcdb620ea316..a97f1a2d17be 100644 --- a/src/lib/features/metrics/instance/register.test.ts +++ b/src/lib/features/metrics/instance/register.test.ts @@ -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), diff --git a/src/lib/features/playground/playground.test.ts b/src/lib/features/playground/playground.test.ts index 24dd2eb01ef1..11670a484043 100644 --- a/src/lib/features/playground/playground.test.ts +++ b/src/lib/features/playground/playground.test.ts @@ -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', () => { diff --git a/src/lib/middleware/oss-authentication.test.ts b/src/lib/middleware/oss-authentication.test.ts index 62b73c5d97a2..93712afd4523 100644 --- a/src/lib/middleware/oss-authentication.test.ts +++ b/src/lib/middleware/oss-authentication.test.ts @@ -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, diff --git a/src/lib/routes/admin-api/api-token.test.ts b/src/lib/routes/admin-api/api-token.test.ts index e6edc76ef2a8..0a11e2815de5 100644 --- a/src/lib/routes/admin-api/api-token.test.ts +++ b/src/lib/routes/admin-api/api-token.test.ts @@ -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, diff --git a/src/lib/routes/admin-api/config.test.ts b/src/lib/routes/admin-api/config.test.ts index 44e86be8c6b9..76c1e1b54081 100644 --- a/src/lib/routes/admin-api/config.test.ts +++ b/src/lib/routes/admin-api/config.test.ts @@ -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, diff --git a/src/lib/routes/admin-api/context.test.ts b/src/lib/routes/admin-api/context.test.ts index 2a017ea7b475..51d6132766ef 100644 --- a/src/lib/routes/admin-api/context.test.ts +++ b/src/lib/routes/admin-api/context.test.ts @@ -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, diff --git a/src/lib/routes/admin-api/email.test.ts b/src/lib/routes/admin-api/email.test.ts index ae1d39ac8146..5a3739703497 100644 --- a/src/lib/routes/admin-api/email.test.ts +++ b/src/lib/routes/admin-api/email.test.ts @@ -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, diff --git a/src/lib/routes/admin-api/events.test.ts b/src/lib/routes/admin-api/events.test.ts index 2c86514c52f0..980b2f1ec5b1 100644 --- a/src/lib/routes/admin-api/events.test.ts +++ b/src/lib/routes/admin-api/events.test.ts @@ -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, diff --git a/src/lib/routes/admin-api/metrics.test.ts b/src/lib/routes/admin-api/metrics.test.ts index adb1f6a57f06..d276134f8e50 100644 --- a/src/lib/routes/admin-api/metrics.test.ts +++ b/src/lib/routes/admin-api/metrics.test.ts @@ -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), diff --git a/src/lib/routes/admin-api/public-signup.test.ts b/src/lib/routes/admin-api/public-signup.test.ts index bc2d9156ecf4..4432e6bc91b5 100644 --- a/src/lib/routes/admin-api/public-signup.test.ts +++ b/src/lib/routes/admin-api/public-signup.test.ts @@ -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, diff --git a/src/lib/routes/admin-api/strategy.test.ts b/src/lib/routes/admin-api/strategy.test.ts index d21225f0983d..00125c70ff1f 100644 --- a/src/lib/routes/admin-api/strategy.test.ts +++ b/src/lib/routes/admin-api/strategy.test.ts @@ -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, diff --git a/src/lib/routes/admin-api/tag.test.ts b/src/lib/routes/admin-api/tag.test.ts index e12b713096c0..a7fe9b892247 100644 --- a/src/lib/routes/admin-api/tag.test.ts +++ b/src/lib/routes/admin-api/tag.test.ts @@ -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, diff --git a/src/lib/routes/admin-api/user/user.test.ts b/src/lib/routes/admin-api/user/user.test.ts index 0d488e8959aa..c732a72fa35f 100644 --- a/src/lib/routes/admin-api/user/user.test.ts +++ b/src/lib/routes/admin-api/user/user.test.ts @@ -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, diff --git a/src/lib/routes/backstage.test.ts b/src/lib/routes/backstage.test.ts index fd77cc9624e4..105b1759ca47 100644 --- a/src/lib/routes/backstage.test.ts +++ b/src/lib/routes/backstage.test.ts @@ -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); diff --git a/src/lib/routes/health-check.test.ts b/src/lib/routes/health-check.test.ts index b2fb3e0d9d5a..187e47dc8c35 100644 --- a/src/lib/routes/health-check.test.ts +++ b/src/lib/routes/health-check.test.ts @@ -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), diff --git a/src/lib/routes/public-invite.test.ts b/src/lib/routes/public-invite.test.ts index c5d950f57e83..978a471eef96 100644 --- a/src/lib/routes/public-invite.test.ts +++ b/src/lib/routes/public-invite.test.ts @@ -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, diff --git a/src/lib/server-impl.ts b/src/lib/server-impl.ts index 4456bc537a6c..76b6f891d23a 100644 --- a/src/lib/server-impl.ts +++ b/src/lib/server-impl.ts @@ -20,6 +20,7 @@ import { RoleName, CustomAuthHandler, SYSTEM_USER, + IUnleashStores, } from './types'; import User, { IUser } from './types/user'; @@ -68,7 +69,11 @@ async function createApp( const secret = await stores.settingStore.get('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, @@ -80,9 +85,9 @@ async function createApp( db, ); const unleash: Omit = { - stores, + stores: combinedStores as IUnleashStores, eventBus: config.eventBus, - services, + services: combinedServices as IUnleashServices, app, config, version: serverVersion, diff --git a/src/test/e2e/helpers/test-helper.ts b/src/test/e2e/helpers/test-helper.ts index f56b5aa6f725..85646f7544f4 100644 --- a/src/test/e2e/helpers/test-helper.ts +++ b/src/test/e2e/helpers/test-helper.ts @@ -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 () => {