refactor: replace connection grpc server
This commit is contained in:
parent
4216498dfc
commit
e294b8355f
12
src/infra/grpc/grpc.options.ts
Normal file
12
src/infra/grpc/grpc.options.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { GrpcOptions } from '@nestjs/microservices'
|
||||||
|
import { PROTO_PATHS } from '@teacinema/contracts'
|
||||||
|
|
||||||
|
export const grpcPackages = ['auth.v1']
|
||||||
|
export const grpcProtoPaths = [PROTO_PATHS.AUTH]
|
||||||
|
export const grpcLoader: GrpcOptions['options']['loader'] = {
|
||||||
|
keepCase: false,
|
||||||
|
longs: String,
|
||||||
|
enums: String,
|
||||||
|
defaults: true,
|
||||||
|
oneofs: true
|
||||||
|
}
|
||||||
26
src/infra/grpc/grpc.server.ts
Normal file
26
src/infra/grpc/grpc.server.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import { INestApplication } from '@nestjs/common'
|
||||||
|
import { ConfigService } from '@nestjs/config'
|
||||||
|
import { MicroserviceOptions, Transport } from '@nestjs/microservices'
|
||||||
|
|
||||||
|
import { AllConfigs } from '@/config'
|
||||||
|
|
||||||
|
import { grpcLoader, grpcPackages, grpcProtoPaths } from './grpc.options'
|
||||||
|
|
||||||
|
export function createGrpcServer(
|
||||||
|
app: INestApplication,
|
||||||
|
config: ConfigService<AllConfigs>
|
||||||
|
) {
|
||||||
|
const host = config.get('grpc.host', { infer: true })
|
||||||
|
const port = config.get('grpc.port', { infer: true })
|
||||||
|
const url = `${host}:${port}`
|
||||||
|
|
||||||
|
app.connectMicroservice<MicroserviceOptions>({
|
||||||
|
transport: Transport.GRPC,
|
||||||
|
options: {
|
||||||
|
url,
|
||||||
|
package: grpcPackages,
|
||||||
|
protoPath: grpcProtoPaths,
|
||||||
|
loader: grpcLoader
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
26
src/main.ts
26
src/main.ts
@ -1,33 +1,15 @@
|
|||||||
import { ConfigService } from '@nestjs/config'
|
import { ConfigService } from '@nestjs/config'
|
||||||
import { NestFactory } from '@nestjs/core'
|
import { NestFactory } from '@nestjs/core'
|
||||||
import { MicroserviceOptions, Transport } from '@nestjs/microservices'
|
|
||||||
|
import { createGrpcServer } from '@/infra/grpc/grpc.server'
|
||||||
|
|
||||||
import { AppModule } from './app.module'
|
import { AppModule } from './app.module'
|
||||||
import { AllConfigs } from './config'
|
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule)
|
const app = await NestFactory.create(AppModule)
|
||||||
|
|
||||||
const config = app.get(ConfigService<AllConfigs>)
|
const config = app.get(ConfigService)
|
||||||
const host = config.get('grpc.host', { infer: true })
|
createGrpcServer(app, config)
|
||||||
const port = config.get('grpc.port', { infer: true })
|
|
||||||
const url = `${host}:${port}`
|
|
||||||
|
|
||||||
app.connectMicroservice<MicroserviceOptions>({
|
|
||||||
transport: Transport.GRPC,
|
|
||||||
options: {
|
|
||||||
url,
|
|
||||||
package: 'auth.v1',
|
|
||||||
protoPath: 'node_modules/@teacinema/contracts/proto/auth.proto',
|
|
||||||
loader: {
|
|
||||||
keepCase: false,
|
|
||||||
longs: String,
|
|
||||||
enums: String,
|
|
||||||
defaults: true,
|
|
||||||
oneofs: true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
await app.startAllMicroservices()
|
await app.startAllMicroservices()
|
||||||
await app.init()
|
await app.init()
|
||||||
|
|||||||
@ -1360,9 +1360,9 @@
|
|||||||
integrity sha512-L006mySvhRpUlRHf3ONbXA0RG42gbQ9QAnmQuZx4zejHEsWB9702xe18MSYcBilv+lmYkQpiX/g3p8P7suLfqQ==
|
integrity sha512-L006mySvhRpUlRHf3ONbXA0RG42gbQ9QAnmQuZx4zejHEsWB9702xe18MSYcBilv+lmYkQpiX/g3p8P7suLfqQ==
|
||||||
|
|
||||||
"@teacinema/contracts@^1.0.0":
|
"@teacinema/contracts@^1.0.0":
|
||||||
version "1.0.1"
|
version "1.0.4"
|
||||||
resolved "https://git.ksv741.keenetic.pro/api/packages/teacinema/npm/%40teacinema%2Fcontracts/-/1.0.1/contracts-1.0.1.tgz#25021fecb1d33d0505fd11ca3d8201660cb1bcbe"
|
resolved "https://git.ksv741.keenetic.pro/api/packages/teacinema/npm/%40teacinema%2Fcontracts/-/1.0.4/contracts-1.0.4.tgz#178dc6392aae52c4dc2b60bb1470a0a2885a8715"
|
||||||
integrity sha512-gvdbQ3Xowr4/h9Kq2P+pO2lr9vyWvIvRoU74mG3LSq2IiKKuwQUTxRaJnzI5MdtNqiq0WIUMo30cfqnBzNAJ1A==
|
integrity sha512-3jHP4NAoH9munaN2Iegvg0X6KN/SR1rwZU7KpbuBtlcTJqsLljTaHwG+XKjEL84Y9b+sF73jGGbg/w2Y8IBQmw==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@nestjs/microservices" "^11.1.12"
|
"@nestjs/microservices" "^11.1.12"
|
||||||
protoc "33.4.0"
|
protoc "33.4.0"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user