Skip to content

Commit

Permalink
feat(health): add health controller
Browse files Browse the repository at this point in the history
Remove App controller and service
  • Loading branch information
leosuncin committed Sep 14, 2022
1 parent 23458c4 commit ed93a8b
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 46 deletions.
21 changes: 0 additions & 21 deletions src/app.controller.spec.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/app.controller.ts

This file was deleted.

6 changes: 2 additions & 4 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
import { TerminusModule } from '@nestjs/terminus';
import { TypeOrmModule } from '@nestjs/typeorm';

import { AppController } from '~app/app.controller';
import { AppService } from '~app/app.service';
import { HealthController } from '~app/health.controller';
import { AuthModule } from '~auth/auth.module';
import { BlogModule } from '~blog/blog.module';
import { configuration } from '~config/configuration';
Expand All @@ -28,7 +27,6 @@ import { dataSource } from '~config/data-source';
AuthModule,
BlogModule,
],
controllers: [AppController],
providers: [AppService],
controllers: [HealthController],
})
export class AppModule {}
8 changes: 0 additions & 8 deletions src/app.service.ts

This file was deleted.

80 changes: 80 additions & 0 deletions src/health.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
HealthCheckService,
HealthIndicatorFunction,
HealthIndicatorResult,
MemoryHealthIndicator,
TypeOrmHealthIndicator,
} from '@nestjs/terminus';
import { type TestingModule, Test } from '@nestjs/testing';
import { createMock } from 'ts-auto-mock';

import { HealthController } from '~app/health.controller';

function getStatus(key: string): HealthIndicatorResult {
return { [key]: { status: 'up' } };
}

describe('HealthController', () => {
let controller: HealthController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [HealthController],
})
.useMocker((token) => {
if (token === HealthCheckService) {
return createMock<HealthCheckService>({
check: jest
.fn()
.mockImplementation((indicators: HealthIndicatorFunction[]) =>
Promise.all(indicators.map((indicator) => indicator())),
),
});
}

if (token === TypeOrmHealthIndicator) {
return createMock<TypeOrmHealthIndicator>({
pingCheck: jest.fn().mockImplementation(getStatus),
});
}

if (token === MemoryHealthIndicator) {
return createMock<MemoryHealthIndicator>({
checkHeap: jest.fn().mockImplementation(getStatus),
checkRSS: jest.fn().mockImplementation(getStatus),
});
}

return;
})
.compile();

controller = module.get<HealthController>(HealthController);
});

it('should be an instance of HealthController', () => {
expect(controller).toBeInstanceOf(HealthController);
});

it('should check the health', async () => {
await expect(controller.healthCheck()).resolves.toMatchInlineSnapshot(`
[
{
"database": {
"status": "up",
},
},
{
"mem_rss": {
"status": "up",
},
},
{
"mem_heap": {
"status": "up",
},
},
]
`);
});
});
27 changes: 27 additions & 0 deletions src/health.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Controller, Get } from '@nestjs/common';
import {
type HealthCheckResult,
HealthCheck,
HealthCheckService,
MemoryHealthIndicator,
TypeOrmHealthIndicator,
} from '@nestjs/terminus';

@Controller('health')
export class HealthController {
constructor(
private readonly health: HealthCheckService,
private readonly mem: MemoryHealthIndicator,
private readonly orm: TypeOrmHealthIndicator,
) {}

@Get()
@HealthCheck()
healthCheck(): Promise<HealthCheckResult> {
return this.health.check([
() => this.orm.pingCheck('database'),
() => this.mem.checkRSS('mem_rss', 768 * 2 ** 20 /* 768 MB */),
() => this.mem.checkHeap('mem_heap', 512 * 2 ** 20 /* 512 MB */),
]);
}
}

0 comments on commit ed93a8b

Please sign in to comment.