Skip to content

Commit

Permalink
feat: enable health check
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Tian committed Dec 11, 2019
1 parent 2249c8d commit b76249c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
language: node_js

node_js:
- 10
- 12
18 changes: 18 additions & 0 deletions README.md
@@ -0,0 +1,18 @@
# nestjs-hero-grpc-sample-with-health-check

> Add standard grpc health check to the nest js hero grpc sample application.
## Run locally

```bash
git clone https://github.com/Jeff-Tian/nestjs-hero-grpc-sample-with-health-check
cd nestjs-hero-grpc-sample-with-health-check
npm i
npm start
```

## Run test

```bash
npm test
```
2 changes: 2 additions & 0 deletions src/app.module.ts
@@ -1,7 +1,9 @@
import { Module } from '@nestjs/common';
import { HeroModule } from './hero/hero.module';
import {HealthController} from './health.controller';

@Module({
imports: [HeroModule],
controllers: [HealthController],
})
export class AppModule {}
43 changes: 43 additions & 0 deletions src/health.controller.ts
@@ -0,0 +1,43 @@
import {Controller, Get, OnModuleInit} from '@nestjs/common';
import {Client, ClientGrpc, GrpcMethod} from '@nestjs/microservices';
import {Observable} from 'rxjs';
import { grpcClientOptions } from 'grpc-health/dist/health/grpc-client.options'
import {grpc} from 'grpc-health/src/health/interfaces/compiled';
import ServingStatus = grpc.health.v1.HealthCheckResponse.ServingStatus;
import {
HealthCheckRequest,
HealthCheckResponse,
} from 'grpc-ts-health-check';

export interface HealthService {
check(data: HealthCheckRequest.AsObject): Observable<any>;
}

@Controller()
export class HealthController implements OnModuleInit {
@Client(grpcClientOptions)
private readonly client: ClientGrpc;

private healthService: HealthService;

onModuleInit() {
this.healthService = this.client.getService<HealthService>('Health');
}

@Get('/version')
version(): Observable<any> {
return require('../../package.json').version;
}

@Get()
execute(): Observable<any> {
return this.healthService.check({service: 'whatever'});
}

@GrpcMethod('Health')
Check(data: HealthCheckRequest.AsObject): HealthCheckResponse.AsObject {
return {
status: ServingStatus.SERVING,
};
}
}
4 changes: 3 additions & 1 deletion src/main.ts
@@ -1,6 +1,8 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { grpcClientOptions } from './grpc-client.options';
import { extendedGrpcOptions } from 'grpc-health/dist/health/grpc-client.options';
import {GrpcOptions} from "@nestjs/microservices"

async function bootstrap() {
/**
Expand All @@ -18,7 +20,7 @@ async function bootstrap() {
*
*/
const app = await NestFactory.create(AppModule);
app.connectMicroservice(grpcClientOptions);
app.connectMicroservice(extendedGrpcOptions(grpcClientOptions as GrpcOptions));

await app.startAllMicroservicesAsync();
await app.listen(3001);
Expand Down

0 comments on commit b76249c

Please sign in to comment.