Skip to content

Commit 7b0b6b1

Browse files
authored
add healthcheck endpoint (#171)
1 parent f6f9c0e commit 7b0b6b1

File tree

5 files changed

+140
-60
lines changed

5 files changed

+140
-60
lines changed

package-lock.json

Lines changed: 92 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@nestjs/platform-socket.io": "^8.4.1",
3131
"@nestjs/schedule": "^1.0.2",
3232
"@nestjs/swagger": "^5.2.0",
33+
"@nestjs/terminus": "^8.0.8",
3334
"@nestjs/websockets": "^8.4.1",
3435
"@prisma/client": "^2.12.1",
3536
"ajv": "^8.10.0",

src/app.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { APP_FILTER, APP_INTERCEPTOR } from '@nestjs/core';
1111
import { HttpExceptionFilter } from './http-exception.filter';
1212
import { CompareModule } from './compare/compare.module';
1313
import { ScheduleModule } from '@nestjs/schedule';
14+
import { TerminusModule } from '@nestjs/terminus';
15+
import { HealthController } from './health/health.controller';
1416

1517
@Module({
1618
imports: [
@@ -23,6 +25,7 @@ import { ScheduleModule } from '@nestjs/schedule';
2325
ProjectsModule,
2426
TestRunsModule,
2527
TestVariationsModule,
28+
TerminusModule,
2629
CompareModule,
2730
],
2831
providers: [
@@ -36,5 +39,6 @@ import { ScheduleModule } from '@nestjs/schedule';
3639
useClass: CacheInterceptor,
3740
},
3841
],
42+
controllers: [HealthController],
3943
})
4044
export class AppModule {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { HealthCheckService } from '@nestjs/terminus';
3+
import { PrismaService } from '../prisma/prisma.service';
4+
import { HealthController } from './health.controller';
5+
6+
describe('Health Controller', () => {
7+
let controller: HealthController;
8+
9+
beforeEach(async () => {
10+
const module: TestingModule = await Test.createTestingModule({
11+
controllers: [HealthController],
12+
providers: [
13+
{ provide: HealthCheckService, useValue: {} },
14+
{ provide: PrismaService, useValue: {} },
15+
],
16+
}).compile();
17+
18+
controller = module.get<HealthController>(HealthController);
19+
});
20+
21+
it('should be defined', () => {
22+
expect(controller).toBeDefined();
23+
});
24+
});

src/health/health.controller.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Controller, Get } from '@nestjs/common';
2+
import { HealthCheckService, HealthCheck } from '@nestjs/terminus';
3+
import { PrismaService } from '../prisma/prisma.service';
4+
5+
@Controller('health')
6+
export class HealthController {
7+
constructor(
8+
private health: HealthCheckService,
9+
private readonly prismaService: PrismaService
10+
) {}
11+
12+
@Get()
13+
@HealthCheck()
14+
check() {
15+
return this.health.check([
16+
() => this.prismaService.$queryRaw('SELECT 1'),
17+
]);
18+
}
19+
}

0 commit comments

Comments
 (0)