From 89f6b0a8c26855cb2b170e3126cbde09ca84c6cb Mon Sep 17 00:00:00 2001 From: Philip Okugbe <16838612+Philipinho@users.noreply.github.com> Date: Wed, 26 Feb 2025 13:00:01 +0000 Subject: [PATCH] feat: add stats to standalone collab server (#798) * Log APP_URL on startup * add stats endpoint to standalone collab server --- .../src/collaboration/collaboration.module.ts | 1 + .../src/collaboration/server/collab-app.module.ts | 10 ++++++++-- .../src/collaboration/server/collab-main.ts | 4 ++-- .../server/collaboration.controller.ts | 15 +++++++++++++++ apps/server/src/main.ts | 11 +++++++++-- 5 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 apps/server/src/collaboration/server/collaboration.controller.ts diff --git a/apps/server/src/collaboration/collaboration.module.ts b/apps/server/src/collaboration/collaboration.module.ts index e233eb89..3011d290 100644 --- a/apps/server/src/collaboration/collaboration.module.ts +++ b/apps/server/src/collaboration/collaboration.module.ts @@ -16,6 +16,7 @@ import { HistoryListener } from './listeners/history.listener'; PersistenceExtension, HistoryListener, ], + exports: [CollaborationGateway], imports: [TokenModule], }) export class CollaborationModule implements OnModuleInit, OnModuleDestroy { diff --git a/apps/server/src/collaboration/server/collab-app.module.ts b/apps/server/src/collaboration/server/collab-app.module.ts index d30426c1..5f3ddcbb 100644 --- a/apps/server/src/collaboration/server/collab-app.module.ts +++ b/apps/server/src/collaboration/server/collab-app.module.ts @@ -7,6 +7,7 @@ import { DatabaseModule } from '@docmost/db/database.module'; import { QueueModule } from '../../integrations/queue/queue.module'; import { EventEmitterModule } from '@nestjs/event-emitter'; import { HealthModule } from '../../integrations/health/health.module'; +import { CollaborationController } from './collaboration.controller'; @Module({ imports: [ @@ -17,7 +18,12 @@ import { HealthModule } from '../../integrations/health/health.module'; HealthModule, EventEmitterModule.forRoot(), ], - controllers: [AppController], + controllers: [ + AppController, + ...(process.env.COLLAB_SHOW_STATS.toLowerCase() === 'true' + ? [CollaborationController] + : []), + ], providers: [AppService], }) -export class CollabAppAppModule {} +export class CollabAppModule {} diff --git a/apps/server/src/collaboration/server/collab-main.ts b/apps/server/src/collaboration/server/collab-main.ts index 62621115..489ad943 100644 --- a/apps/server/src/collaboration/server/collab-main.ts +++ b/apps/server/src/collaboration/server/collab-main.ts @@ -1,5 +1,5 @@ import { NestFactory } from '@nestjs/core'; -import { CollabAppAppModule } from './collab-app.module'; +import { CollabAppModule } from './collab-app.module'; import { FastifyAdapter, NestFastifyApplication, @@ -10,7 +10,7 @@ import { Logger } from '@nestjs/common'; async function bootstrap() { const app = await NestFactory.create( - CollabAppAppModule, + CollabAppModule, new FastifyAdapter({ ignoreTrailingSlash: true, ignoreDuplicateSlashes: true, diff --git a/apps/server/src/collaboration/server/collaboration.controller.ts b/apps/server/src/collaboration/server/collaboration.controller.ts new file mode 100644 index 00000000..6da78941 --- /dev/null +++ b/apps/server/src/collaboration/server/collaboration.controller.ts @@ -0,0 +1,15 @@ +import { Controller, Get } from '@nestjs/common'; +import { CollaborationGateway } from '../collaboration.gateway'; + +@Controller('collab') +export class CollaborationController { + constructor(private readonly collaborationGateway: CollaborationGateway) {} + + @Get('stats') + async getStats() { + return { + connections: this.collaborationGateway.getConnectionCount(), + documents: this.collaborationGateway.getDocumentCount(), + }; + } +} diff --git a/apps/server/src/main.ts b/apps/server/src/main.ts index 89830d58..40000802 100644 --- a/apps/server/src/main.ts +++ b/apps/server/src/main.ts @@ -4,7 +4,7 @@ import { FastifyAdapter, NestFastifyApplication, } from '@nestjs/platform-fastify'; -import { NotFoundException, ValidationPipe } from '@nestjs/common'; +import { Logger, NotFoundException, ValidationPipe } from '@nestjs/common'; import { TransformHttpResponseInterceptor } from './common/interceptors/http-response.interceptor'; import fastifyMultipart from '@fastify/multipart'; import { WsRedisIoAdapter } from './ws/adapter/ws-redis.adapter'; @@ -65,7 +65,14 @@ async function bootstrap() { app.useGlobalInterceptors(new TransformHttpResponseInterceptor()); app.enableShutdownHooks(); - await app.listen(process.env.PORT || 3000, '0.0.0.0'); + const logger = new Logger('NestApplication'); + + const port = process.env.PORT || 3000; + await app.listen(port, '0.0.0.0', () => { + logger.log( + `Listening on http://127.0.0.1:${port} / ${process.env.APP_URL}`, + ); + }); } bootstrap();