Skip to content

Commit 644b37a

Browse files
committed
feat: add usecase Nest microservices
1 parent 1d38a98 commit 644b37a

25 files changed

+5650
-0
lines changed

Nest-microservices/.eslintrc.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
parserOptions: {
4+
project: 'tsconfig.json',
5+
sourceType: 'module',
6+
},
7+
plugins: ['@typescript-eslint/eslint-plugin'],
8+
extends: [
9+
'plugin:@typescript-eslint/recommended',
10+
'prettier/@typescript-eslint',
11+
'plugin:prettier/recommended',
12+
],
13+
root: true,
14+
env: {
15+
node: true,
16+
jest: true,
17+
},
18+
ignorePatterns: ['.eslintrc.js'],
19+
rules: {
20+
'@typescript-eslint/interface-name-prefix': 'off',
21+
'@typescript-eslint/explicit-function-return-type': 'off',
22+
'@typescript-eslint/explicit-module-boundary-types': 'off',
23+
'@typescript-eslint/no-explicit-any': 'off',
24+
},
25+
};

Nest-microservices/.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# compiled output
2+
/dist
3+
/node_modules
4+
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
yarn-debug.log*
10+
yarn-error.log*
11+
lerna-debug.log*
12+
13+
# OS
14+
.DS_Store
15+
16+
# Tests
17+
/coverage
18+
/.nyc_output
19+
20+
# IDEs and editors
21+
/.idea
22+
.project
23+
.classpath
24+
.c9/
25+
*.launch
26+
.settings/
27+
*.sublime-workspace
28+
29+
# IDE - VSCode
30+
.vscode/*
31+
!.vscode/settings.json
32+
!.vscode/tasks.json
33+
!.vscode/launch.json
34+
!.vscode/extensions.json

Nest-microservices/.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
import {
3+
ExceptionFilter,
4+
Catch,
5+
ArgumentsHost,
6+
HttpException,
7+
HttpStatus,
8+
} from '@nestjs/common';
9+
import { HttpAdapterHost } from '@nestjs/core';
10+
11+
@Catch()
12+
export class AllExceptionsFilter implements ExceptionFilter {
13+
constructor(private readonly httpAdapterHost: HttpAdapterHost) { }
14+
15+
catch(exception: unknown, host: ArgumentsHost): void {
16+
// In certain situations `httpAdapter` might not be available in the
17+
// constructor method, thus we should resolve it here.
18+
const { httpAdapter } = this.httpAdapterHost;
19+
20+
const ctx = host.switchToHttp();
21+
22+
console.log('INTERCEPT CONTEXT', ctx)
23+
const httpStatus =
24+
exception instanceof HttpException
25+
? exception.getStatus()
26+
: HttpStatus.INTERNAL_SERVER_ERROR;
27+
28+
const responseBody = {
29+
statusCode: httpStatus,
30+
timestamp: new Date().toISOString(),
31+
path: httpAdapter.getRequestUrl(ctx.getRequest()),
32+
};
33+
34+
httpAdapter.reply(ctx.getResponse(), responseBody, httpStatus);
35+
}
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM node:alpine
2+
3+
WORKDIR /usr/src/app
4+
5+
COPY package*.json ./
6+
7+
RUN npm install
8+
9+
COPY . .
10+
11+
RUN npm run build gateway
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Controller, Get } from '@nestjs/common';
2+
import { v4 as uuid } from 'uuid';
3+
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
4+
5+
@Controller()
6+
export class AppController {
7+
constructor(private readonly amqpConnection: AmqpConnection) {
8+
}
9+
10+
@Get('create-stock')
11+
async createStock() {
12+
13+
const backResponse = await this.amqpConnection.request({
14+
exchange: 'stock',
15+
routingKey: 'stock-route',
16+
payload: { stockId: uuid(), quantity: 5, name: 'Jaffa Cake' }
17+
});
18+
19+
console.log('1. msg published', 'stock', 'stock-route')
20+
21+
console.log('3 Result back:', backResponse)
22+
23+
return backResponse
24+
25+
26+
}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
2+
import { Module } from '@nestjs/common';
3+
import { AppController } from './app.controller';
4+
5+
@Module({
6+
imports: [
7+
RabbitMQModule.forRoot(RabbitMQModule, {
8+
exchanges: [
9+
{
10+
name: 'stock',
11+
type: 'topic',
12+
},
13+
],
14+
uri: 'amqp://user:password@rabbitmq:5672',
15+
connectionInitOptions: { wait: true },
16+
}),
17+
AppModule,
18+
],
19+
controllers: [AppController],
20+
})
21+
export class AppModule { }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { HttpAdapterHost, NestFactory } from '@nestjs/core';
2+
import { AppModule } from './app.module';
3+
import { AllExceptionsFilter } from 'apps/common/AllExceptionFilter';
4+
5+
async function bootstrap() {
6+
const app = await NestFactory.create(AppModule);
7+
8+
await app.listen(3000);
9+
}
10+
11+
bootstrap();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"declaration": false,
5+
"outDir": "../../dist/apps/gateway"
6+
},
7+
"include": ["src/**/*"],
8+
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM node:alpine
2+
3+
WORKDIR /usr/src/app
4+
5+
COPY package*.json ./
6+
7+
RUN npm install
8+
9+
COPY . .
10+
11+
RUN npm run build service-stock

0 commit comments

Comments
 (0)