diff --git a/packages/nocodb/src/main.ts b/packages/nocodb/src/main.ts index 4f1d5a3d69..cbd293166d 100644 --- a/packages/nocodb/src/main.ts +++ b/packages/nocodb/src/main.ts @@ -2,15 +2,26 @@ import { NestFactory } from '@nestjs/core'; import cors from 'cors'; import express from 'express'; import { AppModule } from './app.module'; +import { AppModule as WorkerModule } from './worker.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); - app.use(express.json({ limit: process.env.NC_REQUEST_BODY_SIZE || '50mb' })); - app.use( - cors({ - exposedHeaders: 'xc-db-response', - }), - ); - await app.listen(8080); + if (!process.env['NC_WORKER_CONTAINER']) { + app.use( + express.json({ limit: process.env.NC_REQUEST_BODY_SIZE || '50mb' }), + ); + app.use( + cors({ + exposedHeaders: 'xc-db-response', + }), + ); + await app.listen(8080); + } else { + if (!process.env['NC_REDIS_URL']) { + throw new Error('NC_REDIS_URL is required'); + } + const app = await NestFactory.create(WorkerModule); + await app.init(); + } } bootstrap(); diff --git a/packages/nocodb/src/modules/datas/datas.module.ts b/packages/nocodb/src/modules/datas/datas.module.ts index 6c9f6f713b..dd444e8ae2 100644 --- a/packages/nocodb/src/modules/datas/datas.module.ts +++ b/packages/nocodb/src/modules/datas/datas.module.ts @@ -27,14 +27,18 @@ import { PublicDatasService } from '../../services/public-datas.service'; }), ], controllers: [ - DatasController, - BulkDataAliasController, - DataAliasController, - DataAliasNestedController, - DataAliasExportController, - OldDatasController, - PublicDatasController, - PublicDatasExportController, + ...(!process.env['NC_WORKER_CONTAINER'] + ? [ + DatasController, + BulkDataAliasController, + DataAliasController, + DataAliasNestedController, + DataAliasExportController, + OldDatasController, + PublicDatasController, + PublicDatasExportController, + ] + : []), ], providers: [ DatasService, diff --git a/packages/nocodb/src/modules/jobs/jobs.module.ts b/packages/nocodb/src/modules/jobs/jobs.module.ts index f211cc5d46..1d9c83404c 100644 --- a/packages/nocodb/src/modules/jobs/jobs.module.ts +++ b/packages/nocodb/src/modules/jobs/jobs.module.ts @@ -24,7 +24,11 @@ import { AtImportProcessor } from './at-import/at-import.processor'; name: JOBS_QUEUE, }), ], - controllers: [DuplicateController, AtImportController], + controllers: [ + ...(!process.env['NC_WORKER_CONTAINER'] + ? [DuplicateController, AtImportController] + : []), + ], providers: [ QueueService, JobsGateway, diff --git a/packages/nocodb/src/modules/metas/metas.module.ts b/packages/nocodb/src/modules/metas/metas.module.ts index 0aa6a9ae76..cc5765ec7a 100644 --- a/packages/nocodb/src/modules/metas/metas.module.ts +++ b/packages/nocodb/src/modules/metas/metas.module.ts @@ -83,38 +83,42 @@ import { ProjectUsersService } from '../../services/project-users/project-users. GlobalModule, ], controllers: [ - ApiDocsController, - ApiTokensController, - AttachmentsController, - AuditsController, - BasesController, - CachesController, - ColumnsController, - FiltersController, - FormColumnsController, - FormsController, - GalleriesController, - GridColumnsController, - GridsController, - HooksController, - KanbansController, - MapsController, - MetaDiffsController, - ModelVisibilitiesController, - OrgLcenseController, - OrgTokensController, - OrgUsersController, - PluginsController, - ProjectUsersController, - ProjectsController, - PublicMetasController, - ViewsController, - ViewColumnsController, - UtilsController, - TablesController, - SyncController, - SortsController, - SharedBasesController, + ...(!process.env['NC_WORKER_CONTAINER'] + ? [ + ApiDocsController, + ApiTokensController, + AttachmentsController, + AuditsController, + BasesController, + CachesController, + ColumnsController, + FiltersController, + FormColumnsController, + FormsController, + GalleriesController, + GridColumnsController, + GridsController, + HooksController, + KanbansController, + MapsController, + MetaDiffsController, + ModelVisibilitiesController, + OrgLcenseController, + OrgTokensController, + OrgUsersController, + PluginsController, + ProjectUsersController, + ProjectsController, + PublicMetasController, + ViewsController, + ViewColumnsController, + UtilsController, + TablesController, + SyncController, + SortsController, + SharedBasesController, + ] + : []), ], providers: [ ApiDocsService, diff --git a/packages/nocodb/src/modules/test/test.module.ts b/packages/nocodb/src/modules/test/test.module.ts index d86fece015..d8e4b12d56 100644 --- a/packages/nocodb/src/modules/test/test.module.ts +++ b/packages/nocodb/src/modules/test/test.module.ts @@ -2,6 +2,8 @@ import { Module } from '@nestjs/common'; import { TestController } from '../../controllers/test/test.controller'; @Module({ - controllers: [TestController], + controllers: [ + ...(!process.env['NC_WORKER_CONTAINER'] ? [TestController] : []), + ], }) export class TestModule {} diff --git a/packages/nocodb/src/modules/users/users.module.ts b/packages/nocodb/src/modules/users/users.module.ts index d4297b3c5e..75a5539ce9 100644 --- a/packages/nocodb/src/modules/users/users.module.ts +++ b/packages/nocodb/src/modules/users/users.module.ts @@ -10,7 +10,9 @@ import { UsersController } from '../../controllers/users/users.controller'; @Module({ imports: [GlobalModule, PassportModule], - controllers: [UsersController], + controllers: [ + ...(!process.env['NC_WORKER_CONTAINER'] ? [UsersController] : []), + ], providers: [UsersService, GoogleStrategyProvider], exports: [UsersService], }) diff --git a/packages/nocodb/src/worker.module.ts b/packages/nocodb/src/worker.module.ts new file mode 100644 index 0000000000..67fe751b30 --- /dev/null +++ b/packages/nocodb/src/worker.module.ts @@ -0,0 +1,78 @@ +import { Inject, Module, RequestMethod } from '@nestjs/common'; +import { APP_FILTER } from '@nestjs/core'; +import { BullModule } from '@nestjs/bull'; +import { EventEmitterModule as NestJsEventEmitter } from '@nestjs/event-emitter'; +import { Connection } from './connection/connection'; +import { GlobalExceptionFilter } from './filters/global-exception/global-exception.filter'; +import NcPluginMgrv2 from './helpers/NcPluginMgrv2'; +import { DatasModule } from './modules/datas/datas.module'; +import { IEventEmitter } from './modules/event-emitter/event-emitter.interface'; +import { EventEmitterModule } from './modules/event-emitter/event-emitter.module'; +import { AuthService } from './services/auth.service'; +import { UsersModule } from './modules/users/users.module'; +import { MetaService } from './meta/meta.service'; +import Noco from './Noco'; +import { TestModule } from './modules/test/test.module'; +import { GlobalModule } from './modules/global/global.module'; +import { HookHandlerService } from './services/hook-handler.service'; +import { LocalStrategy } from './strategies/local.strategy'; +import { AuthTokenStrategy } from './strategies/authtoken.strategy/authtoken.strategy'; +import { BaseViewStrategy } from './strategies/base-view.strategy/base-view.strategy'; +import { MetasModule } from './modules/metas/metas.module'; +import NocoCache from './cache/NocoCache'; +import { JobsModule } from './modules/jobs/jobs.module'; +import type { OnApplicationBootstrap } from '@nestjs/common'; + +@Module({ + imports: [ + GlobalModule, + UsersModule, + ...(process.env['PLAYWRIGHT_TEST'] === 'true' ? [TestModule] : []), + MetasModule, + DatasModule, + EventEmitterModule, + JobsModule, + NestJsEventEmitter.forRoot(), + ...(process.env['NC_REDIS_URL'] + ? [ + BullModule.forRoot({ + url: process.env.NC_REDIS_URL, + }), + ] + : []), + ], + controllers: [], + providers: [ + AuthService, + { + provide: APP_FILTER, + useClass: GlobalExceptionFilter, + }, + LocalStrategy, + AuthTokenStrategy, + BaseViewStrategy, + HookHandlerService, + ], +}) +export class AppModule implements OnApplicationBootstrap { + constructor( + private readonly connection: Connection, + private readonly metaService: MetaService, + @Inject('IEventEmitter') private readonly eventEmitter: IEventEmitter, + ) {} + + // app init + async onApplicationBootstrap(): Promise { + process.env.NC_VERSION = '0105004'; + + await NocoCache.init(); + + // todo: remove + // temporary hack + Noco._ncMeta = this.metaService; + Noco.config = this.connection.config; + Noco.eventEmitter = this.eventEmitter; + + await NcPluginMgrv2.init(Noco.ncMeta); + } +}