From 211774c262e6929bc77f0374b26ac48141b17cab Mon Sep 17 00:00:00 2001 From: Abdelrahman Essawy Date: Sun, 18 Feb 2024 00:32:02 +0200 Subject: [PATCH] feat(internal-communication): improve proto path Signed-off-by: Abdelrahman Essawy --- apps/backend/users/src/main.ts | 13 ++++++++----- .../internal-communications/src/index.ts | 1 + .../src/lib/grpc/constants.ts | 3 +++ .../src/lib/grpc/internal-grpc.module.ts | 18 +++++++++++------- .../src/lib/grpc/internal-grpc.service.ts | 4 ++-- 5 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 libs/backend/internal-communications/src/lib/grpc/constants.ts diff --git a/apps/backend/users/src/main.ts b/apps/backend/users/src/main.ts index 06cab6c9..8d2e0aad 100644 --- a/apps/backend/users/src/main.ts +++ b/apps/backend/users/src/main.ts @@ -5,14 +5,17 @@ import { Logger, ValidationPipe } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; +import { MicroserviceOptions, Transport } from '@nestjs/microservices'; import cors from 'cors'; -import { AppModule } from './app/app.module'; import { NestExpressApplication } from '@nestjs/platform-express'; import cookieParser from 'cookie-parser'; + import { TypeORMExceptionFilter } from '@backend/filters'; -import { MicroserviceOptions, Transport } from '@nestjs/microservices'; +import { AUTH_PACKAGE_NAME } from '@backend/proto'; +import { AUTH_SERVICE_PROTO_PATH } from '@backend/internal-communications'; + import { AuthModule } from './app/auth/auth.module'; -import { join } from 'path'; +import { AppModule } from './app/app.module'; async function bootstrap() { const appGRPC = await NestFactory.createMicroservice( @@ -20,8 +23,8 @@ async function bootstrap() { { transport: Transport.GRPC, options: { - package: 'auth', - protoPath: join(__dirname, 'proto/auth.proto'), + package: AUTH_PACKAGE_NAME, + protoPath: AUTH_SERVICE_PROTO_PATH, }, }, ); diff --git a/libs/backend/internal-communications/src/index.ts b/libs/backend/internal-communications/src/index.ts index 7cf4f4a5..bc3ea0a1 100644 --- a/libs/backend/internal-communications/src/index.ts +++ b/libs/backend/internal-communications/src/index.ts @@ -1,2 +1,3 @@ export * from './lib/internal-communications.module'; export * from './lib/internal-communications.service'; +export * from './lib/grpc/constants'; diff --git a/libs/backend/internal-communications/src/lib/grpc/constants.ts b/libs/backend/internal-communications/src/lib/grpc/constants.ts new file mode 100644 index 00000000..b71ac8f3 --- /dev/null +++ b/libs/backend/internal-communications/src/lib/grpc/constants.ts @@ -0,0 +1,3 @@ +import { join } from 'path'; + +export const AUTH_SERVICE_PROTO_PATH = join(__dirname, 'proto/auth.proto'); diff --git a/libs/backend/internal-communications/src/lib/grpc/internal-grpc.module.ts b/libs/backend/internal-communications/src/lib/grpc/internal-grpc.module.ts index 2d86d990..aa0ea21e 100644 --- a/libs/backend/internal-communications/src/lib/grpc/internal-grpc.module.ts +++ b/libs/backend/internal-communications/src/lib/grpc/internal-grpc.module.ts @@ -1,16 +1,20 @@ import { Module } from '@nestjs/common'; import { ClientGrpc, ClientsModule, Transport } from '@nestjs/microservices'; + +import { AUTH_PACKAGE_NAME, AUTH_SERVICE_NAME } from '@backend/proto'; + import { InternalGrpcService } from './internal-grpc.service'; +import { AUTH_SERVICE_PROTO_PATH } from './constants'; @Module({ imports: [ ClientsModule.register([ { - name: 'AUTH_PACKAGE', + name: AUTH_PACKAGE_NAME, transport: Transport.GRPC, options: { - package: 'auth', - protoPath: 'apps/backend/users/src/proto/auth.proto', + package: AUTH_PACKAGE_NAME, + protoPath: AUTH_SERVICE_PROTO_PATH, }, }, ]), @@ -18,12 +22,12 @@ import { InternalGrpcService } from './internal-grpc.service'; providers: [ InternalGrpcService, { - provide: 'AUTH_SERVICE', + provide: AUTH_SERVICE_NAME, useFactory: async (client: ClientGrpc) => - await client.getService('AuthService'), - inject: ['AUTH_PACKAGE'], + await client.getService(AUTH_SERVICE_NAME), + inject: [AUTH_PACKAGE_NAME], }, ], - exports: ['AUTH_SERVICE'], + exports: [AUTH_SERVICE_NAME], }) export class InternalGrpcModule {} diff --git a/libs/backend/internal-communications/src/lib/grpc/internal-grpc.service.ts b/libs/backend/internal-communications/src/lib/grpc/internal-grpc.service.ts index ccfdffb3..ce542225 100644 --- a/libs/backend/internal-communications/src/lib/grpc/internal-grpc.service.ts +++ b/libs/backend/internal-communications/src/lib/grpc/internal-grpc.service.ts @@ -1,10 +1,10 @@ import { Inject, Injectable } from '@nestjs/common'; -import { AuthServiceClient } from '@backend/proto'; +import { AUTH_SERVICE_NAME, AuthServiceClient } from '@backend/proto'; @Injectable() export class InternalGrpcService { constructor( - @Inject('AUTH_SERVICE') public readonly authService: AuthServiceClient, + @Inject(AUTH_SERVICE_NAME) public readonly authService: AuthServiceClient, // @Inject('USERS_SERVICE') // public readonly usersService: any, ) {}