diff --git a/apps/billing/.env b/apps/billing/.env new file mode 100644 index 0000000..badde21 --- /dev/null +++ b/apps/billing/.env @@ -0,0 +1,4 @@ +APP_PORT=3001 + +RMQ_URI=amqp://localhost:5672 +RMQ_BILLING_QUEUE=billing \ No newline at end of file diff --git a/apps/billing/src/billing/billing.controller.ts b/apps/billing/src/billing/billing.controller.ts index 07f4388..ea9cc62 100644 --- a/apps/billing/src/billing/billing.controller.ts +++ b/apps/billing/src/billing/billing.controller.ts @@ -1,7 +1,9 @@ -import { Controller, Get } from '@nestjs/common'; +import { Controller, Get, } from '@nestjs/common'; import { BillingService } from './billing.service'; -import { EventPattern, Payload, RmqContext, Ctx } from '@nestjs/microservices' +import { MessagePattern, Payload, RmqContext, Ctx } from '@nestjs/microservices' import { RmqService } from '@app/common' +import { RMQ_EVENTS } from '../constants/rmq.constants'; +import { Observable, from } from 'rxjs'; @Controller() export class BillingController { constructor( @@ -9,13 +11,14 @@ export class BillingController { private readonly rmqService: RmqService ) { } - @Get() + @Get('u') getHello(): string { return this.billingService.getHello(); } - @EventPattern('order_created') - order(@Payload() data: any, @Ctx() context: RmqContext) { + @MessagePattern('GET_USER_BY_ID') + order(@Payload() data: number[], @Ctx() context: RmqContext): Observable { this.billingService.order(data); - this.rmqService.ack(context) + // this.rmqService.ack(context) + return from(data); } } diff --git a/apps/billing/src/billing/billing.service.ts b/apps/billing/src/billing/billing.service.ts index 9de435c..6db6e2d 100644 --- a/apps/billing/src/billing/billing.service.ts +++ b/apps/billing/src/billing/billing.service.ts @@ -3,10 +3,11 @@ import { Injectable } from '@nestjs/common'; @Injectable() export class BillingService { getHello(): string { - return 'Hello World!'; + return 'Hello World!!!!!!'; } order(data) { - return console.log('bill is cretaed ', data) + console.log('bill is cretaed ', data) + return 'hi' } } diff --git a/apps/billing/src/constants/rmq.constants.ts b/apps/billing/src/constants/rmq.constants.ts new file mode 100644 index 0000000..c1d5e25 --- /dev/null +++ b/apps/billing/src/constants/rmq.constants.ts @@ -0,0 +1,7 @@ +export const RMQ_SERVICE = { + USERS: 'USERS' +} +export enum RMQ_EVENTS { + NEW_USER_CREATED, + GET_USER_BY_ID +} \ No newline at end of file diff --git a/apps/billing/src/main.ts b/apps/billing/src/main.ts index 35bc007..7c25f81 100644 --- a/apps/billing/src/main.ts +++ b/apps/billing/src/main.ts @@ -2,6 +2,7 @@ import { RmqService } from '@app/common'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { ValidationPipe } from '@nestjs/common'; +import { RMQ_SERVICE } from './constants/rmq.constants'; async function bootstrap() { const app = await NestFactory.create(AppModule); diff --git a/apps/gateway/.env b/apps/gateway/.env index 1d09303..6dd5eeb 100644 --- a/apps/gateway/.env +++ b/apps/gateway/.env @@ -2,4 +2,5 @@ MONGODB_URI=mongodb://localhost:27017/orderApp APP_PORT=3000 RMQ_URI=amqp://localhost:5672 -RMQ_USERS_QUEUE=users \ No newline at end of file +RMQ_BILLING_QUEUE=billing + diff --git a/apps/gateway/env/local.env b/apps/gateway/env/local.env index 1d09303..68feb19 100644 --- a/apps/gateway/env/local.env +++ b/apps/gateway/env/local.env @@ -2,4 +2,4 @@ MONGODB_URI=mongodb://localhost:27017/orderApp APP_PORT=3000 RMQ_URI=amqp://localhost:5672 -RMQ_USERS_QUEUE=users \ No newline at end of file +RMQ_BILLING_QUEUE=billing \ No newline at end of file diff --git a/apps/gateway/src/app.module.ts b/apps/gateway/src/app.module.ts index 62549a8..641c664 100644 --- a/apps/gateway/src/app.module.ts +++ b/apps/gateway/src/app.module.ts @@ -2,9 +2,13 @@ import { Module } from '@nestjs/common'; import { ControllersModule } from './infrastructure/controllers/controllers.module'; import { ServicesModule } from './infrastructure/services/services.module'; import { EnvironmentModule } from './infrastructure/environment/environment.module'; +import { ConfigModule } from '@nestjs/config'; @Module({ - imports: [ControllersModule, ServicesModule, EnvironmentModule], + imports: [ControllersModule, ServicesModule, + + + ], controllers: [], providers: [], }) diff --git a/apps/gateway/src/infrastructure/controllers/user.controller.ts b/apps/gateway/src/infrastructure/controllers/user.controller.ts index 98ff631..824b9b8 100644 --- a/apps/gateway/src/infrastructure/controllers/user.controller.ts +++ b/apps/gateway/src/infrastructure/controllers/user.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get } from '@nestjs/common'; +import { Controller, Get, Param } from '@nestjs/common'; import { UserService } from '../services/user.service'; @Controller('users') @@ -6,7 +6,7 @@ export class UserController { constructor(private readonly userService: UserService) { } @Get(':id') - getUserById(): string { - return this.userService.getHello(); + getUserById(@Param('id') id: string) { + return this.userService.getUserById(id); } } diff --git a/apps/gateway/src/infrastructure/services/services.module.ts b/apps/gateway/src/infrastructure/services/services.module.ts index 3ecae40..8922e56 100644 --- a/apps/gateway/src/infrastructure/services/services.module.ts +++ b/apps/gateway/src/infrastructure/services/services.module.ts @@ -2,12 +2,58 @@ import { Module } from '@nestjs/common'; import { UserService } from './user.service'; import { RmqModule } from '@app/common'; import { RMQ_SERVICE } from '../constants/rmq.constants'; -import { EnvironmentModule } from '../environment/environment.module'; -import { EnvironmentService } from '../environment/environment.service'; - +// import { EnvironmentModule } from '../environment/environment.module'; +// import { EnvironmentService } from '../environment/environment.service'; +// +import { ClientsModule, Transport } from '@nestjs/microservices'; +import { ConfigModule, ConfigService } from '@nestjs/config'; @Module({ - imports: [RmqModule.register({ name: RMQ_SERVICE.USERS }, EnvironmentService), EnvironmentModule], + imports: [ + RmqModule.register({ name: 'BILLING' }), + ConfigModule.forRoot({ + isGlobal: true, + envFilePath: 'apps/gateway/env/local.env' + }) + ], + // imports: [ + // RmqModule.registerAsync({ + // imports: [ + // ClientsModule.registerAsync([ + // { + // name: RMQ_SERVICE.USERS, + // useFactory: () => ({ + // transport: Transport.RMQ, + // options: { + // urls: ['amqp://localhost:5672'], + // queue: 'users', + // }, + // }), + // inject: [], + // }, + // ]), + + // ], + + // }), + + // ], controllers: [], providers: [UserService], + exports: [UserService] }) export class ServicesModule { } +// imports: [ +// ClientsModule.registerAsync([ +// { +// name, +// useFactory: (EnvironmentService) => ({ +// transport: Transport.RMQ, +// options: { +// urls: [EnvironmentService.getRmqUri], +// queue: EnvironmentService.getRmqUsersQueue, +// }, +// }), +// inject: [EnvironmentService], +// }, +// ]), +// ], \ No newline at end of file diff --git a/apps/gateway/src/infrastructure/services/user.service.ts b/apps/gateway/src/infrastructure/services/user.service.ts index 5648309..37cad31 100644 --- a/apps/gateway/src/infrastructure/services/user.service.ts +++ b/apps/gateway/src/infrastructure/services/user.service.ts @@ -7,16 +7,16 @@ import { RMQ_EVENTS } from '../constants/rmq.constants' @Injectable() export class UserService { constructor( - @Inject(RMQ_SERVICE.USERS) private usersClient: ClientProxy + @Inject('BILLING') private usersClient: ClientProxy ) { } getHello(): string { return 'Hello World!'; } - async getUserById(id: number) { + async getUserById(id: string) { try { - const v = await lastValueFrom(this.usersClient.emit(RMQ_EVENTS.GET_USER_BY_ID, id)) + const v = await lastValueFrom(this.usersClient.send('GET_USER_BY_ID', id)) console.log('v**', v) } catch (error) { console.log('error in order service', error) diff --git a/libs/common/src/index.ts b/libs/common/src/index.ts index 8e33b4a..583d770 100644 --- a/libs/common/src/index.ts +++ b/libs/common/src/index.ts @@ -3,3 +3,4 @@ export * from './database/abstract.repository'; export * from './database/database.module'; export * from './rmq/rmq.module'; export * from './rmq/rmq.service'; +// export * from './myconfig/myconfig.module'; diff --git a/libs/common/src/myconfig/myconfig.module.ts b/libs/common/src/myconfig/myconfig.module.ts new file mode 100644 index 0000000..e7c9a0e --- /dev/null +++ b/libs/common/src/myconfig/myconfig.module.ts @@ -0,0 +1,27 @@ +import { DynamicModule, Module } from "@nestjs/common"; +import { ConfigModule } from "@nestjs/config"; +import { MongooseModule, MongooseModuleOptions } from "@nestjs/mongoose"; + +@Module({}) +export class myConfigModule { + static register(envPath: string): DynamicModule { + return { + module: myConfigModule, + imports: [ + ConfigModule.forRoot({ + envFilePath: envPath + }), + + ], + providers: [ + { + provide: 'MY_CONFIG', + useFactory(mongoose: MongooseModuleOptions) { + + }, + inject: [MongooseModule] + } + ] + } + } +} \ No newline at end of file diff --git a/libs/common/src/rmq/config.module-definition.ts b/libs/common/src/rmq/config.module-definition.ts new file mode 100644 index 0000000..b35d5f7 --- /dev/null +++ b/libs/common/src/rmq/config.module-definition.ts @@ -0,0 +1,6 @@ +import { ConfigurableModuleBuilder } from '@nestjs/common'; +import { ConfigModuleOptions } from './configModuleOption.interface'; +import { RmqOptions } from '@nestjs/microservices'; + +export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = + new ConfigurableModuleBuilder().build(); \ No newline at end of file diff --git a/libs/common/src/rmq/configModuleOption.interface.ts b/libs/common/src/rmq/configModuleOption.interface.ts new file mode 100644 index 0000000..c32d6df --- /dev/null +++ b/libs/common/src/rmq/configModuleOption.interface.ts @@ -0,0 +1,9 @@ +import { Transport } from "@nestjs/microservices" + +export interface ConfigModuleOptions { + transport: any + options: { + urls: string[] + queue: string + }, +} \ No newline at end of file diff --git a/libs/common/src/rmq/rmq.module.ts b/libs/common/src/rmq/rmq.module.ts index 8f4d3ed..f758497 100644 --- a/libs/common/src/rmq/rmq.module.ts +++ b/libs/common/src/rmq/rmq.module.ts @@ -2,7 +2,7 @@ import { Module, DynamicModule } from '@nestjs/common'; import { RmqService } from './rmq.service'; import { ClientsModule, Transport } from '@nestjs/microservices'; import { ConfigService } from '@nestjs/config'; - +import { ConfigurableModuleClass } from './config.module-definition'; interface RmqModuleOptions { name: string; @@ -13,21 +13,22 @@ interface RmqModuleOptions { exports: [RmqService] }) export class RmqModule { - static register({ name }: RmqModuleOptions, configS): DynamicModule { + static register({ name }: RmqModuleOptions): DynamicModule { return { module: RmqModule, imports: [ ClientsModule.registerAsync([ { name, - useFactory: (configS: any) => ({ + // اینحا رو میشد بدون فکتوری هم نوشت + useFactory: (configService: ConfigService) => ({ transport: Transport.RMQ, options: { - urls: [configS.getRmqUri], - queue: configS.getRmqUsersQueue, + urls: [configService.get('RMQ_URI')], + queue: configService.get(`RMQ_${name}_QUEUE`), }, }), - inject: [configS], + inject: [ConfigService], }, ]), ], @@ -35,3 +36,6 @@ export class RmqModule { }; } } + + +// export class RmqModule extends ConfigurableModuleClass { } \ No newline at end of file diff --git a/libs/common/src/rmq/rmq.service.ts b/libs/common/src/rmq/rmq.service.ts index cec27dc..d52b8cf 100644 --- a/libs/common/src/rmq/rmq.service.ts +++ b/libs/common/src/rmq/rmq.service.ts @@ -11,12 +11,13 @@ export class RmqService { ) { } getOptions(queue: string, noAck = false): RmqOptions { + console.log('rec', this.configService.get(`RMQ_${queue}_QUEUE`)) return { transport: Transport.RMQ, options: { urls: [this.configService.get('RMQ_URI')], queue: this.configService.get(`RMQ_${queue}_QUEUE`), - noAck, + // noAck, persistent: true } } diff --git a/nest-cli.json b/nest-cli.json index a319ce5..41b6c2d 100644 --- a/nest-cli.json +++ b/nest-cli.json @@ -45,6 +45,15 @@ "compilerOptions": { "tsConfigPath": "libs/common/tsconfig.lib.json" } + }, + "config": { + "type": "library", + "root": "libs/config", + "entryFile": "index", + "sourceRoot": "libs/config/src", + "compilerOptions": { + "tsConfigPath": "libs/config/tsconfig.lib.json" + } } } } \ No newline at end of file diff --git a/package.json b/package.json index 97fd79b..3e38319 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,8 @@ "/libs/" ], "moduleNameMapper": { - "^@app/common(|/.*)$": "/libs/common/src/$1" + "^@app/common(|/.*)$": "/libs/common/src/$1", + "^@app/config(|/.*)$": "/libs/config/src/$1" } } -} +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index f7224cc..d33e48d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -23,6 +23,12 @@ ], "@app/common/*": [ "libs/common/src/*" + ], + "@app/config": [ + "libs/config/src" + ], + "@app/config/*": [ + "libs/config/src/*" ] } }