-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(browser-service): add HTTP module and routes, including health c…
…hecks and error handling
- Loading branch information
Showing
25 changed files
with
246 additions
and
34 deletions.
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
GRPC_PORT=50051 | ||
HTTP_PORT=5001 | ||
NODE_ENV=development | ||
SERVICE_NAME=browser-service | ||
|
||
|
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
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
22 changes: 22 additions & 0 deletions
22
apps/browser-service/k8s/helm-chart/templates/http-route.yaml
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,22 @@ | ||
apiVersion: gateway.networking.k8s.io/v1 | ||
kind: HTTPRoute | ||
metadata: | ||
name: browser-service | ||
namespace: infra | ||
spec: | ||
parentRefs: | ||
- kind: Gateway | ||
name: gateway-external | ||
namespace: envoy-gateway-system | ||
sectionName: http | ||
rules: | ||
- timeouts: | ||
request: "60s" | ||
backendRequest: "60s" | ||
matches: | ||
- path: | ||
type: PathPrefix | ||
value: /api | ||
backendRefs: | ||
- name: browser-service | ||
port: 5001 |
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
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
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,7 @@ | ||
import { Module } from '@nestjs/common' | ||
import { GrpcModule } from './controller' | ||
import { GrpcModule, HttpModule } from './controller' | ||
|
||
@Module({ | ||
imports: [GrpcModule], | ||
imports: [GrpcModule, HttpModule], | ||
}) | ||
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
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
39 changes: 39 additions & 0 deletions
39
apps/browser-service/src/controller/http/health/controller.ts
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,39 @@ | ||
import { InjectPuppeteerService, PuppeteerService } from '@diut/nestjs-infra' | ||
import { Controller, Get, UseFilters } from '@nestjs/common' | ||
import { | ||
HealthCheck, | ||
HealthCheckResult, | ||
HealthCheckService, | ||
} from '@nestjs/terminus' | ||
import { setTimeout } from 'timers/promises' | ||
import { AllExceptionsFilter } from '../shared/exception-filter' | ||
|
||
@Controller('health') | ||
@UseFilters(AllExceptionsFilter) | ||
export class HealthController { | ||
constructor( | ||
private readonly health: HealthCheckService, | ||
@InjectPuppeteerService() | ||
private readonly puppeteerClient: PuppeteerService, | ||
) {} | ||
|
||
@Get('liveness') | ||
@HealthCheck() | ||
async liveness() { | ||
const result: HealthCheckResult = await this.health.check([ | ||
() => this.puppeteerClient.healthcheck(), | ||
]) | ||
|
||
return result | ||
} | ||
|
||
@Get('error') | ||
async error() { | ||
throw new Error('Error') | ||
} | ||
|
||
@Get('timeout') | ||
async timeout() { | ||
return setTimeout(120_000) | ||
} | ||
} |
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 @@ | ||
export * from './module' |
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,8 @@ | ||
import { ModuleMetadata } from '@nestjs/common' | ||
import { TerminusModule } from '@nestjs/terminus' | ||
import { HealthController } from './controller' | ||
|
||
export const healthModuleMetadata: ModuleMetadata = { | ||
imports: [TerminusModule], | ||
controllers: [HealthController], | ||
} |
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 @@ | ||
export * from './module' |
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,7 @@ | ||
import { concatModuleMetadata } from '@diut/nestjs-infra' | ||
import { Module } from '@nestjs/common' | ||
import { healthModuleMetadata } from './health' | ||
import { commonModuleMetadata } from './shared' | ||
|
||
@Module(concatModuleMetadata([...commonModuleMetadata, healthModuleMetadata])) | ||
export class HttpModule {} |
38 changes: 38 additions & 0 deletions
38
apps/browser-service/src/controller/http/shared/exception-filter.ts
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,38 @@ | ||
import { | ||
ArgumentsHost, | ||
Catch, | ||
ExceptionFilter, | ||
HttpException, | ||
HttpStatus, | ||
Logger, | ||
} from '@nestjs/common' | ||
import { HttpAdapterHost } from '@nestjs/core' | ||
|
||
@Catch() | ||
export class AllExceptionsFilter implements ExceptionFilter { | ||
private logger = new Logger(this.constructor.name) | ||
|
||
constructor(private readonly httpAdapterHost: HttpAdapterHost) {} | ||
|
||
catch(exception: unknown, host: ArgumentsHost) { | ||
const { httpAdapter } = this.httpAdapterHost | ||
const ctx = host.switchToHttp() | ||
|
||
const httpStatus = | ||
exception instanceof HttpException | ||
? exception.getStatus() | ||
: HttpStatus.INTERNAL_SERVER_ERROR | ||
const message = String(exception) | ||
|
||
this.logger.error({ | ||
message, | ||
httpStatus, | ||
}) | ||
|
||
const responseBody = { | ||
message, | ||
} | ||
|
||
httpAdapter.reply(ctx.getResponse(), responseBody, httpStatus) | ||
} | ||
} |
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 @@ | ||
export * from './module' |
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,10 @@ | ||
import { ModuleMetadata } from '@nestjs/common' | ||
import { appMetadata } from 'src/app' | ||
import { configMetadata } from 'src/config' | ||
import { infraMetadata } from 'src/infra' | ||
|
||
export const commonModuleMetadata: ModuleMetadata[] = [ | ||
configMetadata, | ||
infraMetadata, | ||
appMetadata, | ||
] |
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 +1,2 @@ | ||
export * from './grpc' | ||
export * from './http' |
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,37 +1,48 @@ | ||
import './otel' | ||
// | ||
import { | ||
GrpcListenBootstrap, | ||
HttpListenBootstrap, | ||
LifecycleBootstrap, | ||
PinoBootstrapFactory, | ||
PrefixBootstrap, | ||
bootstrapApp, | ||
} from '@diut/nestjs-infra' | ||
import { | ||
DIUT_PACKAGE_NAME, | ||
DiutGrpcService, | ||
resolveProtoPath, | ||
} from '@diut/services' | ||
import { INestMicroservice } from '@nestjs/common' | ||
import { INestApplication } from '@nestjs/common' | ||
import { NestFactory } from '@nestjs/core' | ||
import { MicroserviceOptions, Transport } from '@nestjs/microservices' | ||
import * as dotenv from 'dotenv' | ||
import { AppModule } from './app.module' | ||
|
||
dotenv.config() | ||
|
||
bootstrapApp<INestMicroservice>( | ||
(AppModule, options) => | ||
NestFactory.createMicroservice<MicroserviceOptions>(AppModule, { | ||
...options, | ||
transport: Transport.GRPC, | ||
options: { | ||
url: `0.0.0.0:${process.env.GRPC_PORT}`, | ||
package: DIUT_PACKAGE_NAME, | ||
protoPath: resolveProtoPath(DiutGrpcService.Browser, __dirname), | ||
maxReceiveMessageLength: 20_000_000, | ||
}, | ||
}), | ||
bootstrapApp<INestApplication>( | ||
(AppModule, options) => NestFactory.create(AppModule, options), | ||
AppModule, | ||
{ serviceName: process.env.SERVICE_NAME, nodeEnv: process.env.NODE_ENV }, | ||
[PinoBootstrapFactory(), LifecycleBootstrap, GrpcListenBootstrap], | ||
[ | ||
PinoBootstrapFactory(), | ||
LifecycleBootstrap, | ||
{ | ||
async afterInit(ctx) { | ||
ctx.app.connectMicroservice<MicroserviceOptions>({ | ||
transport: Transport.GRPC, | ||
options: { | ||
url: `0.0.0.0:${process.env.GRPC_PORT}`, | ||
package: DIUT_PACKAGE_NAME, | ||
protoPath: resolveProtoPath(DiutGrpcService.Browser, __dirname), | ||
maxReceiveMessageLength: 20_000_000, | ||
}, | ||
}) | ||
|
||
await ctx.app.startAllMicroservices() | ||
}, | ||
}, | ||
PrefixBootstrap, | ||
HttpListenBootstrap(process.env.HTTP_PORT), | ||
], | ||
) |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.