Skip to content

Commit db9ad1e

Browse files
committed
adding health checks for database and website
1 parent 99b9e09 commit db9ad1e

File tree

7 files changed

+69
-6
lines changed

7 files changed

+69
-6
lines changed

package-lock.json

Lines changed: 13 additions & 0 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
@@ -24,6 +24,7 @@
2424
"@nestjs/microservices": "^8.1.2",
2525
"@nestjs/passport": "^8.0.1",
2626
"@nestjs/platform-express": "^8.1.2",
27+
"@nestjs/terminus": "^8.0.1",
2728
"@nestjs/typeorm": "^8.0.2",
2829
"axios": "^0.24.0",
2930
"class-transformer": "^0.4.0",

src/app.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import { CorsMiddleware } from './middlewares/cors/cors.middleware';
2929
import { ConfigModule, ConfigService } from '@nestjs/config';
3030
import { CatalogModule } from './catalog/catalog.module';
3131
import { AuthModule } from './auth/auth.module';
32+
import { TerminusModule } from '@nestjs/terminus';
33+
import { HealthController } from './management/health/health.controller';
3234
import databaseConfiguration from './config/database/database.configuration';
3335
import thirdpartyConfiguration from './config/thirdparty/thirdparty.configuration';
3436

@@ -49,6 +51,7 @@ import thirdpartyConfiguration from './config/thirdparty/thirdparty.configuratio
4951
}),
5052
CatalogModule,
5153
AuthModule,
54+
TerminusModule,
5255
],
5356
controllers: [
5457
AppController,
@@ -58,6 +61,7 @@ import thirdpartyConfiguration from './config/thirdparty/thirdparty.configuratio
5861
ItemController,
5962
CategoryController,
6063
AuthController,
64+
HealthController,
6165
],
6266
providers: [
6367
AppService,

src/main.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@ import { useContainer } from 'class-validator';
55

66
async function bootstrap() {
77
const app = await NestFactory.create(AppModule);
8-
// useContainer(app, {fallbackOnErrors: true});
8+
99
useContainer(app.select(AppModule), { fallbackOnErrors: true });
1010
app.enableCors({
1111
origin: 'http://localhost:3000',
1212
});
1313
app.useGlobalPipes(
1414
new ValidationPipe({
1515
transform: true,
16-
// whitelist: true,
17-
// forbidNonWhitelisted: false,
18-
// disableErrorMessages: true,
1916
}),
2017
);
21-
await app.listen(3001);
18+
await app.listen(3000);
2219
}
2320
bootstrap();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { HealthController } from './health.controller';
3+
4+
describe('Health Controller', () => {
5+
let controller: HealthController;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
controllers: [HealthController],
10+
}).compile();
11+
12+
controller = module.get<HealthController>(HealthController);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(controller).toBeDefined();
17+
});
18+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Controller, Get } from '@nestjs/common';
2+
import {
3+
HealthCheck,
4+
HealthCheckService,
5+
HttpHealthIndicator,
6+
TypeOrmHealthIndicator,
7+
} from '@nestjs/terminus';
8+
9+
@Controller('health')
10+
export class HealthController {
11+
constructor(
12+
private health: HealthCheckService,
13+
private http: HttpHealthIndicator,
14+
private db: TypeOrmHealthIndicator,
15+
) {}
16+
17+
@Get('nestjs')
18+
@HealthCheck()
19+
checkServiceStatus() {
20+
return this.health.check([
21+
() => this.http.pingCheck('nestjs-docs', 'https://docs.nestjs.com'),
22+
]);
23+
}
24+
25+
@Get('database')
26+
@HealthCheck()
27+
checkDatabaseStatus() {
28+
return this.health.check([() => this.db.pingCheck('myapp')]);
29+
}
30+
}

src/services/item.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,4 @@ export class ItemService implements IItemService {
143143
);
144144
});
145145
}
146-
}
146+
}

0 commit comments

Comments
 (0)