From d706af5154738a5a9f89640cb817f98e245f9262 Mon Sep 17 00:00:00 2001 From: lehuygiang28 Date: Tue, 2 Jul 2024 12:46:07 +0700 Subject: [PATCH] fix(be): :bug: bull board and serverless compatible --- apps/be/src/app/app.module.ts | 20 +++++++++++-------- apps/be/src/app/config/app-config.ts | 11 +++++++++- apps/be/src/app/config/app-config.type.ts | 1 + .../app/middlewares/job-queues.middleware.ts | 7 +++++-- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/apps/be/src/app/app.module.ts b/apps/be/src/app/app.module.ts index 07344f0..c4c710d 100644 --- a/apps/be/src/app/app.module.ts +++ b/apps/be/src/app/app.module.ts @@ -66,13 +66,17 @@ export class AppModule { private readonly redisService: RedisService, ) {} configure(consumer: MiddlewareConsumer) { - consumer - .apply( - jobQueueUIMiddleware( - this.redisService.getClient, - this.configService.getOrThrow('app.globalPrefix', { infer: true }), - ), - ) - .forRoutes('admin/queues'); + const bullBoardPath = this.configService.getOrThrow('app.bullBoardPath', { infer: true }); + + if (bullBoardPath) { + const prefix = this.configService.getOrThrow('app.globalPrefix', { infer: true }) + ? '/' + this.configService.getOrThrow('app.globalPrefix', { infer: true }) + : ''; + consumer + .apply( + jobQueueUIMiddleware(this.redisService.getClient, `${prefix}/${bullBoardPath}`), + ) + .forRoutes(bullBoardPath); + } } } diff --git a/apps/be/src/app/config/app-config.ts b/apps/be/src/app/config/app-config.ts index e3ab9ad..dfad188 100644 --- a/apps/be/src/app/config/app-config.ts +++ b/apps/be/src/app/config/app-config.ts @@ -49,6 +49,10 @@ class EnvironmentVariablesValidator { @ValidateIf((o: EnvironmentVariablesValidator) => !!o?.API_STATS_PATH) @IsString() API_STATS_PASSWORD: string; + + @IsOptional() + @IsString() + BULL_BOARD_PATH: string; } export default registerAs('app', () => { @@ -66,8 +70,13 @@ export default registerAs('app', () => { : 100, port: process.env?.PORT ? parseInt(process.env?.PORT, 10) : 8000, fallbackLanguage: process.env?.FALLBACK_LANGUAGE || 'en', - apiStatsPath: process.env?.API_STATS_PATH, + apiStatsPath: process.env?.API_STATS_PATH + ? process.env.API_STATS_PATH.replace(/^\/|\\|\/$|\\$/g, '') + : '', apiStatsUsername: process.env?.API_STATS_USERNAME, apiStatsPassword: process.env?.API_STATS_PASSWORD, + bullBoardPath: process.env?.BULL_BOARD_PATH + ? process.env.BULL_BOARD_PATH.replace(/^\/|\\|\/$|\\$/g, '') + : '', }; }); diff --git a/apps/be/src/app/config/app-config.type.ts b/apps/be/src/app/config/app-config.type.ts index c7eb26d..97d2b8c 100644 --- a/apps/be/src/app/config/app-config.type.ts +++ b/apps/be/src/app/config/app-config.type.ts @@ -9,4 +9,5 @@ export type AppConfig = { apiStatsPath: string; apiStatsUsername: string; apiStatsPassword: string; + bullBoardPath: string; }; diff --git a/apps/be/src/app/middlewares/job-queues.middleware.ts b/apps/be/src/app/middlewares/job-queues.middleware.ts index d84a187..a338fce 100644 --- a/apps/be/src/app/middlewares/job-queues.middleware.ts +++ b/apps/be/src/app/middlewares/job-queues.middleware.ts @@ -10,9 +10,8 @@ import { BULLMQ_BG_JOB_QUEUE, } from '~be/common/bullmq/bullmq.constant'; -export function jobQueueUIMiddleware(connection: Redis, globalPrefix = '') { +export function jobQueueUIMiddleware(connection: Redis, basePath = '') { const serverAdapter = new ExpressAdapter(); - const basePath = `${globalPrefix ? '/' + globalPrefix : ''}/admin/queues`; serverAdapter.setBasePath(basePath); const queues = [ @@ -35,6 +34,10 @@ export function jobQueueUIMiddleware(connection: Redis, globalPrefix = '') { createBullBoard({ queues, serverAdapter, + options: { + uiBasePath: require.resolve(`@bull-board/ui/package.json`).replace('package.json', ''), + uiConfig: {}, + }, }); return serverAdapter.getRouter();