Skip to content

Commit

Permalink
Merge pull request #2 from morteza-mortezai/rabbit-gateway
Browse files Browse the repository at this point in the history
Rabbit gateway
  • Loading branch information
morteza-mortezai authored Jun 9, 2023
2 parents aa26cac + 6d47701 commit c0a997b
Show file tree
Hide file tree
Showing 128 changed files with 6,298 additions and 1,342 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ lerna-debug.log*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/extensions.json

*.zip
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"vue.codeActions.enabled": false
}
81 changes: 40 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,40 @@
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
[circleci-url]: https://circleci.com/gh/nestjs/nest

<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
<p align="center">
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg"/></a>
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a>
</p>
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
<p align="center">Payever Test</p>



## Description

[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
this project is consist of three microservices ,*Gateway* ,*Users* and *Mailer*

they communicate each other throug rabbitMq .

there is also **lib** folder that contains common modules .

client request is recived by gateway and after validation, it is sent to related microservice and then result will be sent back .

![1](https://github.com/morteza-mortezai/nestjs-microservice-clean-architecture/assets/75200938/875f23fb-8fef-4e1f-9a5d-546b3af29673)
## architecture
clean architecture is used for this project

it is consist of three main layer ; **Domain** , **Usecase** and **Infrastucture** .


accroding to clean architecture rules domain layer should not depend on any thing .

and also usecase layer can just depend on domain layer

Infrastructure layer is where controller and other services are there .

![2](https://github.com/morteza-mortezai/nestjs-microservice-clean-architecture/assets/75200938/0c7e56de-9aab-4d77-b15d-821befc1f6d6)

## endpoints
post: localhost:3001/api/users
get: localhost:3001/api/user/:id
get : localhost:3000/api/user/:id/avatar
delete : localhost:3000/api/user/:id/avatar

## Installation

Expand All @@ -35,14 +49,15 @@ $ npm install
## Running the app

```bash
# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod
$ npm run start gateway
```
then in new terminal
```bash
$ npm run start users
```
then in new terminal
```bash
$ npm run start mailer
```

## Test
Expand All @@ -51,23 +66,7 @@ $ npm run start:prod
# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov
```

## Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).

## Stay in touch

- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
- Website - [https://nestjs.com](https://nestjs.com/)
- Twitter - [@nestframework](https://twitter.com/nestframework)

## License

Nest is [MIT licensed](LICENSE).
22 changes: 0 additions & 22 deletions apps/auth/src/auth.controller.spec.ts

This file was deleted.

12 changes: 0 additions & 12 deletions apps/auth/src/auth.controller.ts

This file was deleted.

10 changes: 0 additions & 10 deletions apps/auth/src/auth.module.ts

This file was deleted.

8 changes: 0 additions & 8 deletions apps/auth/src/auth.service.ts

This file was deleted.

8 changes: 0 additions & 8 deletions apps/auth/src/main.ts

This file was deleted.

24 changes: 0 additions & 24 deletions apps/auth/test/app.e2e-spec.ts

This file was deleted.

5 changes: 2 additions & 3 deletions apps/gateway/env/local.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
APP_PORT=3000

MONGODB_URI=mongodb://localhost:27017/orderApp

RMQ_URI=amqp://localhost:5672
RMQ_USERS_QUEUE=users
RMQ_USERS_QUEUE=users

10 changes: 3 additions & 7 deletions apps/gateway/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
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,
import { ControllerModule } from './infrastructure/controllers/controller.module';


],
@Module({
imports: [EnvironmentModule, ControllerModule],
controllers: [],
providers: [],
})
Expand Down
7 changes: 0 additions & 7 deletions apps/gateway/src/infrastructure/constants/rmq.constants.ts

This file was deleted.

12 changes: 12 additions & 0 deletions apps/gateway/src/infrastructure/controllers/controller.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { UserController } from './user.controller';
import { RabbitmqModule } from '../rabbit-mq/rabbit-mq.module';

@Module({
imports: [RabbitmqModule],
controllers: [
UserController
],
providers: []
})
export class ControllerModule { }
10 changes: 0 additions & 10 deletions apps/gateway/src/infrastructure/controllers/controllers.module.ts

This file was deleted.

58 changes: 24 additions & 34 deletions apps/gateway/src/infrastructure/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
import { Controller, Get, Param, ParseIntPipe, Post, Body, Delete } from '@nestjs/common';
import { UsersService } from '../services/users.service';
import { CreateUserDto } from '../dto/createUser.dto';
import { Controller, Post, Get, Inject, ParseIntPipe, Param, Delete, Body } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices'
import { CreateUserDto } from '../dto/create-user.dto';
import { RMQ_SERVICES, RMQ_CMD } from '@app/common/constants/rmq.constant';
import { lastValueFrom } from 'rxjs'

@Controller()
export class UserController {
constructor(private readonly usersService: UsersService) { }
constructor(
@Inject(RMQ_SERVICES.USERS) private readonly usersClient: ClientProxy
) { }

@Post('users')
async create(@Body() createUserDto: CreateUserDto) {
return this.usersService.createUser(createUserDto);
}
@Post('users')
createUser(@Body() createUser: CreateUserDto) {
return lastValueFrom(this.usersClient.send(RMQ_CMD.CREATE_NEW_USER, createUser))
}

@Get('user/:id')
getUserById(@Param('id', ParseIntPipe) id: number) {
return this.usersService.getUserById(id);
}
@Get('user/:userId')
async getUserById(@Param('userId', ParseIntPipe) userId: number) {
return lastValueFrom(this.usersClient.send(RMQ_CMD.GET_USER_BY_ID, userId))
}

@Get('user/:userId/avatar')
async getAvatarByUserId(@Param('userId', ParseIntPipe) userId: number) {
return lastValueFrom(this.usersClient.send(RMQ_CMD.GET_AVATAR_BY_ID, userId))
}

// @Get('user/:id/avatar')
// async getAvatar(@Param('id', ParseIntPipe) id: number): Promise<any> {
// try {
// // check if it exists already
// let fileName = await this.avatarService.avatarExist(id)
// if (!fileName) {
// //get user
// const user = await this.usersService.findOne(id)
// // get and save avatar
// fileName = await this.avatarService.getAndSaveAvatar(user)
// }
// return this.avatarService.readFile(fileName as string)

// } catch (error) {
// return error
// }
// }

// @Delete('user/:id/avatar')
// async delete(@Param('id', ParseIntPipe) id: number) {
// return this.avatarService.delete(id);
// }
@Delete('user/:userId/avatar')
async deleteAvatar(@Param('userId', ParseIntPipe) userId: number) {
return lastValueFrom(this.usersClient.send(RMQ_CMD.DELETE_AVATAR_BY_ID, userId))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { ClientOptions, Transport } from '@nestjs/microservices';

@Injectable()
export class EnvironmentService implements RmqConfig, AppConfig {
constructor(private configService: ConfigService) { }
constructor(
private configService: ConfigService
) { }

getAppPort(): number {
return this.configService.get<number>('APP_PORT');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Module } from '@nestjs/common';
import { EnvironmentModule } from '../environment/environment.module';
import { RMQ_SERVICES } from '../constants/rmq.constants';
import { ClientsModule, Transport, ClientProxyFactory } from '@nestjs/microservices';
import { RMQ_SERVICES } from '@app/common/constants/rmq.constant';
import { ClientProxyFactory } from '@nestjs/microservices';
import { EnvironmentService } from '../environment/environment.service';
import { EnvironmentModule } from '../environment/environment.module';

@Module({
imports: [EnvironmentModule],
controllers: [],
providers: [
//users service
{
provide: RMQ_SERVICES.USERS,
useFactory: (environmentService: EnvironmentService) => {
Expand Down
11 changes: 0 additions & 11 deletions apps/gateway/src/infrastructure/services/services.module.ts

This file was deleted.

Loading

0 comments on commit c0a997b

Please sign in to comment.