Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chat and database consumer modules with message handling capabili… #116

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 7 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,26 @@
<p align="center">
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="200" alt="Nest Logo" /></a>
</p>

[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)-->

## Description

[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
[LeyuChat](https://dev.leyuchat.com/) A Message Automation tool

## Installation

```bash
$ npm install
```

## Running the app
## Running Mini App

```bash
# development
$ npm run start

# watch mode
$ npm run start:dev
# run database consumer
$ pm2 start dist/src/apps/consumers/database-consumer/main.js --no-daemon

# production mode
$ npm run start:prod
# run chat consumer
$ pm2 start dist/src/apps/consumers/chat-consumer/main.js --no-daemon
```

## Test

```bash
# 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).
LeyuChat is [MIT licensed](LICENSE).
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ services:
environment:
RABBITMQ_DEFAULT_USER: user
RABBITMQ_DEFAULT_PASS: password

chat-consumer:
build:
context: .
platform: linux/amd64
environment:
- RABBITMQ_URL=${RABBITMQ_URL}
command: ["node", "dist/src/apps/consumers/chat-consumer/main.js"]

database-consumer:
build:
context: .
platform: linux/amd64
environment:
- RABBITMQ_URL=${RABBITMQ_URL}
command: ["node", "dist/src/apps/consumers/database-consumer/main.js"]

volumes:
postgres_data:
23 changes: 23 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@nestjs/websockets": "^10.4.15",
"@prisma/client": "^5.22.0",
"@types/ioredis": "^4.28.10",
"amqp-connection-manager": "^4.1.14",
"amqplib": "^0.10.5",
"bcryptjs": "^2.4.3",
"class-transformer": "^0.5.1",
Expand Down
18 changes: 18 additions & 0 deletions src/apps/consumers/chat-consumer/chat-consumer.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ChatConsumerController } from './chat-consumer.controller';

describe('ChatConsumerController', () => {
let controller: ChatConsumerController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ChatConsumerController],
}).compile();

controller = module.get<ChatConsumerController>(ChatConsumerController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
4 changes: 4 additions & 0 deletions src/apps/consumers/chat-consumer/chat-consumer.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Controller } from '@nestjs/common';

@Controller('chat-consumer')
export class ChatConsumerController {}
18 changes: 18 additions & 0 deletions src/apps/consumers/chat-consumer/chat-consumer.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Module } from '@nestjs/common';
import { ChatConsumerController } from './chat-consumer.controller';
import { ChatConsumerService } from './chat-consumer.service';
import { ConfigModule } from '@nestjs/config';
import { PrismaModule } from 'src/modules/prisma/prisma.module';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.env',
}),
PrismaModule,
],
controllers: [ChatConsumerController],
providers: [ChatConsumerService],
})
export class ChatConsumerModule {}
18 changes: 18 additions & 0 deletions src/apps/consumers/chat-consumer/chat-consumer.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ChatConsumerService } from './chat-consumer.service';

describe('ChatConsumerService', () => {
let service: ChatConsumerService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ChatConsumerService],
}).compile();

service = module.get<ChatConsumerService>(ChatConsumerService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
4 changes: 4 additions & 0 deletions src/apps/consumers/chat-consumer/chat-consumer.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class ChatConsumerService {}
26 changes: 26 additions & 0 deletions src/apps/consumers/chat-consumer/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { ChatConsumerModule } from './chat-consumer.module';

async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
ChatConsumerModule,
{
transport: Transport.RMQ,
options: {
urls: [process.env.RABBITMQ_URL],
queue: 'chat',
queueOptions: {
durable: true,
messageTtl: 60000,
deadLetterExchange: 'message',
deadLetterRoutingKey: 'dead',
},
noAck: false,
prefetchCount: 1,
},
},
);
await app.listen();
}
bootstrap();
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { DatabaseConsumerController } from './database-consumer.controller';

describe('DatabaseConsumerController', () => {
let controller: DatabaseConsumerController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [DatabaseConsumerController],
}).compile();

controller = module.get<DatabaseConsumerController>(
DatabaseConsumerController,
);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Controller } from '@nestjs/common';
import { Ctx, EventPattern, Payload, RmqContext } from '@nestjs/microservices';
import { MessagePayload } from 'src/types/message';
import { DatabaseConsumerService } from './database-consumer.service';

@Controller('database-consumer')
export class DatabaseConsumerController {
constructor(private readonly databaseService: DatabaseConsumerService) {}
@EventPattern()
async handleMessage(
@Payload() data: MessagePayload,
@Ctx() context: RmqContext,
) {
this.databaseService.handleMessage(data, context);
}
}
18 changes: 18 additions & 0 deletions src/apps/consumers/database-consumer/database-consumer.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Module } from '@nestjs/common';
import { DatabaseConsumerController } from './database-consumer.controller';
import { DatabaseConsumerService } from './database-consumer.service';
import { ConfigModule } from '@nestjs/config';
import { PrismaModule } from 'src/modules/prisma/prisma.module';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.env',
}),
PrismaModule,
],
controllers: [DatabaseConsumerController],
providers: [DatabaseConsumerService],
})
export class DatabaseConsumerModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { DatabaseConsumerService } from './database-consumer.service';

describe('DatabaseConsumerService', () => {
let service: DatabaseConsumerService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [DatabaseConsumerService],
}).compile();

service = module.get<DatabaseConsumerService>(DatabaseConsumerService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
11 changes: 11 additions & 0 deletions src/apps/consumers/database-consumer/database-consumer.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Injectable } from '@nestjs/common';
import { RmqContext } from '@nestjs/microservices';
import { MessagePayload } from 'src/types/message';

@Injectable()
export class DatabaseConsumerService {
async handleMessage(data: MessagePayload, context: RmqContext) {
console.log(data);
console.log(context);
}
}
26 changes: 26 additions & 0 deletions src/apps/consumers/database-consumer/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { DatabaseConsumerModule } from './database-consumer.module';

async function bootstrap() {
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
DatabaseConsumerModule,
{
transport: Transport.RMQ,
options: {
urls: [process.env.RABBITMQ_URL],
queue: 'database',
queueOptions: {
durable: true,
messageTtl: 60000,
deadLetterExchange: 'message',
deadLetterRoutingKey: 'dead',
},
noAck: false,
prefetchCount: 1,
},
},
);
await app.listen();
}
bootstrap();
7 changes: 7 additions & 0 deletions src/types/message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Message } from '@prisma/client';

export interface MessagePayload {
type?: string;
metadata?: Record<string, any>;
payload?: Message;
}