From 66773dfacaa91cab7bbaebe925c316aa7704de71 Mon Sep 17 00:00:00 2001 From: Will H Date: Fri, 5 Jul 2024 10:10:08 +1200 Subject: [PATCH] Add health check and dev script --- apps/server/src/app.module.ts | 2 ++ .../integrations/health/health.controller.ts | 26 +++++++++++++++++++ .../src/integrations/health/health.module.ts | 8 ++++++ package.json | 4 ++- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 apps/server/src/integrations/health/health.controller.ts create mode 100644 apps/server/src/integrations/health/health.module.ts diff --git a/apps/server/src/app.module.ts b/apps/server/src/app.module.ts index 2d1a4131..28832720 100644 --- a/apps/server/src/app.module.ts +++ b/apps/server/src/app.module.ts @@ -11,6 +11,7 @@ import { MailModule } from './integrations/mail/mail.module'; import { QueueModule } from './integrations/queue/queue.module'; import { StaticModule } from './integrations/static/static.module'; import { EventEmitterModule } from '@nestjs/event-emitter'; +import { HealthModule } from './integrations/health/health.module'; @Module({ imports: [ @@ -21,6 +22,7 @@ import { EventEmitterModule } from '@nestjs/event-emitter'; WsModule, QueueModule, StaticModule, + HealthModule, StorageModule.forRootAsync({ imports: [EnvironmentModule], }), diff --git a/apps/server/src/integrations/health/health.controller.ts b/apps/server/src/integrations/health/health.controller.ts new file mode 100644 index 00000000..65e28852 --- /dev/null +++ b/apps/server/src/integrations/health/health.controller.ts @@ -0,0 +1,26 @@ +import { KyselyDB } from '@docmost/db/types/kysely.types'; +import { + Controller, + Get, + HttpCode, + HttpStatus, + InternalServerErrorException, + Post, +} from '@nestjs/common'; +import { sql } from 'kysely'; +import { InjectKysely } from 'nestjs-kysely'; + +@Controller() +export class HealthController { + constructor(@InjectKysely() private readonly db: KyselyDB) {} + + @Get('health') + @HttpCode(HttpStatus.OK) + async health() { + try { + await sql`SELECT 1=1`.execute(this.db); + } catch (error) { + throw new InternalServerErrorException('Health check failed'); + } + } +} diff --git a/apps/server/src/integrations/health/health.module.ts b/apps/server/src/integrations/health/health.module.ts new file mode 100644 index 00000000..ea7ba33b --- /dev/null +++ b/apps/server/src/integrations/health/health.module.ts @@ -0,0 +1,8 @@ +import { Global, Module } from '@nestjs/common'; +import { HealthController } from './health.controller'; + +@Global() +@Module({ + controllers: [HealthController], +}) +export class HealthModule {} diff --git a/package.json b/package.json index 1d7f9067..f55b5258 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "client:dev": "nx run client:dev", "server:dev": "nx run server:start:dev", "server:start": "nx run server:start:prod", - "email:dev": "nx run @docmost/transactional:dev" + "email:dev": "nx run @docmost/transactional:dev", + "dev": "pnpm concurrently -n \"frontend,backend\" -c \"cyan,green\" \"pnpm run client:dev\" \"pnpm run server:dev\"" }, "dependencies": { "@docmost/editor-ext": "workspace:*", @@ -66,6 +67,7 @@ "@nx/js": "19.3.2", "@types/uuid": "^10.0.0", "nx": "19.3.2", + "concurrently": "^8.2.2", "tsx": "^4.15.7" }, "workspaces": {