Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jeff Tian
committed
Dec 11, 2019
1 parent
2249c8d
commit b76249c
Showing
5 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- 10 | ||
- 12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters