├── .gitignore
├── distrubuted-cache--redis
├── .eslintrc.js
├── .gitignore
├── .prettierrc
├── README.md
├── nest-cli.json
├── package-lock.json
├── package.json
├── src
│ ├── app.controller.ts
│ ├── app.module.ts
│ ├── app.service.ts
│ └── main.ts
├── test
│ ├── app.e2e-spec.ts
│ └── jest-e2e.json
├── tsconfig.build.json
└── tsconfig.json
├── in-memory-cache
├── .eslintrc.js
├── .gitignore
├── .prettierrc
├── README.md
├── nest-cli.json
├── package-lock.json
├── package.json
├── src
│ ├── app.controller.ts
│ ├── app.module.ts
│ ├── app.service.ts
│ └── main.ts
├── test
│ ├── app.e2e-spec.ts
│ └── jest-e2e.json
├── tsconfig.build.json
└── tsconfig.json
├── microservices--pub-sub
├── redis-publisher
│ ├── .eslintrc.js
│ ├── .gitignore
│ ├── .prettierrc
│ ├── README.md
│ ├── nest-cli.json
│ ├── package-lock.json
│ ├── package.json
│ ├── src
│ │ ├── app.module.ts
│ │ ├── main.ts
│ │ └── publisher
│ │ │ ├── publisher.controller.ts
│ │ │ ├── publisher.module.ts
│ │ │ └── publisher.service.ts
│ ├── test
│ │ ├── app.e2e-spec.ts
│ │ └── jest-e2e.json
│ ├── tsconfig.build.json
│ └── tsconfig.json
└── redis-subscribe
│ ├── .eslintrc.js
│ ├── .gitignore
│ ├── .prettierrc
│ ├── README.md
│ ├── nest-cli.json
│ ├── package-lock.json
│ ├── package.json
│ ├── src
│ ├── app.module.ts
│ ├── main.ts
│ └── subscribe
│ │ ├── subscribe.controller.ts
│ │ ├── subscribe.module.ts
│ │ └── subscribe.service.ts
│ ├── test
│ ├── app.e2e-spec.ts
│ └── jest-e2e.json
│ ├── tsconfig.build.json
│ └── tsconfig.json
└── redis-sentinel
└── sentinel.conf
/.gitignore:
--------------------------------------------------------------------------------
1 | # compiled output
2 | /dist
3 | /node_modules
4 | /build
5 |
6 | # Logs
7 | logs
8 | *.log
9 | npm-debug.log*
10 | pnpm-debug.log*
11 | yarn-debug.log*
12 | yarn-error.log*
13 | lerna-debug.log*
14 |
15 | # OS
16 | .DS_Store
17 |
18 | # Tests
19 | /coverage
20 | /.nyc_output
21 |
22 | # IDEs and editors
23 | /.idea
24 | .project
25 | .classpath
26 | .c9/
27 | *.launch
28 | .settings/
29 | *.sublime-workspace
30 |
31 | # IDE - VSCode
32 | .vscode/*
33 | !.vscode/settings.json
34 | !.vscode/tasks.json
35 | !.vscode/launch.json
36 | !.vscode/extensions.json
37 |
38 | # dotenv environment variable files
39 | .env
40 | .env.development.local
41 | .env.test.local
42 | .env.production.local
43 | .env.local
44 |
45 | # temp directory
46 | .temp
47 | .tmp
48 |
49 | # Runtime data
50 | pids
51 | *.pid
52 | *.seed
53 | *.pid.lock
54 |
55 | # Diagnostic reports (https://nodejs.org/api/report.html)
56 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
57 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | parser: '@typescript-eslint/parser',
3 | parserOptions: {
4 | project: 'tsconfig.json',
5 | tsconfigRootDir: __dirname,
6 | sourceType: 'module',
7 | },
8 | plugins: ['@typescript-eslint/eslint-plugin'],
9 | extends: [
10 | 'plugin:@typescript-eslint/recommended',
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 | };
26 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/.gitignore:
--------------------------------------------------------------------------------
1 | # compiled output
2 | /dist
3 | /node_modules
4 | /build
5 |
6 | # Logs
7 | logs
8 | *.log
9 | npm-debug.log*
10 | pnpm-debug.log*
11 | yarn-debug.log*
12 | yarn-error.log*
13 | lerna-debug.log*
14 |
15 | # OS
16 | .DS_Store
17 |
18 | # Tests
19 | /coverage
20 | /.nyc_output
21 |
22 | # IDEs and editors
23 | /.idea
24 | .project
25 | .classpath
26 | .c9/
27 | *.launch
28 | .settings/
29 | *.sublime-workspace
30 |
31 | # IDE - VSCode
32 | .vscode/*
33 | !.vscode/settings.json
34 | !.vscode/tasks.json
35 | !.vscode/launch.json
36 | !.vscode/extensions.json
37 |
38 | # dotenv environment variable files
39 | .env
40 | .env.development.local
41 | .env.test.local
42 | .env.production.local
43 | .env.local
44 |
45 | # temp directory
46 | .temp
47 | .tmp
48 |
49 | # Runtime data
50 | pids
51 | *.pid
52 | *.seed
53 | *.pid.lock
54 |
55 | # Diagnostic reports (https://nodejs.org/api/report.html)
56 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
57 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true,
3 | "trailingComma": "all"
4 | }
--------------------------------------------------------------------------------
/distrubuted-cache--redis/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6 | [circleci-url]: https://circleci.com/gh/nestjs/nest
7 |
8 | A progressive Node.js framework for building efficient and scalable server-side applications.
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
24 |
25 | ## Description
26 |
27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
28 |
29 | ## Installation
30 |
31 | ```bash
32 | $ npm install
33 | ```
34 |
35 | ## Running the app
36 |
37 | ```bash
38 | # development
39 | $ npm run start
40 |
41 | # watch mode
42 | $ npm run start:dev
43 |
44 | # production mode
45 | $ npm run start:prod
46 | ```
47 |
48 | ## Test
49 |
50 | ```bash
51 | # unit tests
52 | $ npm run test
53 |
54 | # e2e tests
55 | $ npm run test:e2e
56 |
57 | # test coverage
58 | $ npm run test:cov
59 | ```
60 |
61 | ## Support
62 |
63 | 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).
64 |
65 | ## Stay in touch
66 |
67 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
68 | - Website - [https://nestjs.com](https://nestjs.com/)
69 | - Twitter - [@nestframework](https://twitter.com/nestframework)
70 |
71 | ## License
72 |
73 | Nest is [MIT licensed](LICENSE).
74 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/nest-cli.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://json.schemastore.org/nest-cli",
3 | "collection": "@nestjs/schematics",
4 | "sourceRoot": "src",
5 | "compilerOptions": {
6 | "deleteOutDir": true
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "distrubuted-cache--redis",
3 | "version": "0.0.1",
4 | "description": "",
5 | "author": "",
6 | "private": true,
7 | "license": "UNLICENSED",
8 | "scripts": {
9 | "build": "nest build",
10 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
11 | "start": "nest start",
12 | "start:dev": "nest start --watch",
13 | "start:debug": "nest start --debug --watch",
14 | "start:prod": "node dist/main",
15 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
16 | "test": "jest",
17 | "test:watch": "jest --watch",
18 | "test:cov": "jest --coverage",
19 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
20 | "test:e2e": "jest --config ./test/jest-e2e.json"
21 | },
22 | "dependencies": {
23 | "@nestjs/cache-manager": "^2.2.2",
24 | "@nestjs/common": "^10.0.0",
25 | "@nestjs/core": "^10.0.0",
26 | "@nestjs/platform-express": "^10.0.0",
27 | "cache-manager": "^5.7.6",
28 | "cache-manager-redis-store": "^2.0.0",
29 | "reflect-metadata": "^0.2.0",
30 | "rxjs": "^7.8.1"
31 | },
32 | "devDependencies": {
33 | "@nestjs/cli": "^10.0.0",
34 | "@nestjs/schematics": "^10.0.0",
35 | "@nestjs/testing": "^10.0.0",
36 | "@types/cache-manager-redis-store": "^2.0.4",
37 | "@types/express": "^4.17.17",
38 | "@types/jest": "^29.5.2",
39 | "@types/node": "^20.3.1",
40 | "@types/supertest": "^6.0.0",
41 | "@typescript-eslint/eslint-plugin": "^6.0.0",
42 | "@typescript-eslint/parser": "^6.0.0",
43 | "eslint": "^8.42.0",
44 | "eslint-config-prettier": "^9.0.0",
45 | "eslint-plugin-prettier": "^5.0.0",
46 | "jest": "^29.5.0",
47 | "prettier": "^3.0.0",
48 | "source-map-support": "^0.5.21",
49 | "supertest": "^6.3.3",
50 | "ts-jest": "^29.1.0",
51 | "ts-loader": "^9.4.3",
52 | "ts-node": "^10.9.1",
53 | "tsconfig-paths": "^4.2.0",
54 | "typescript": "^5.1.3"
55 | },
56 | "jest": {
57 | "moduleFileExtensions": [
58 | "js",
59 | "json",
60 | "ts"
61 | ],
62 | "rootDir": "src",
63 | "testRegex": ".*\\.spec\\.ts$",
64 | "transform": {
65 | "^.+\\.(t|j)s$": "ts-jest"
66 | },
67 | "collectCoverageFrom": [
68 | "**/*.(t|j)s"
69 | ],
70 | "coverageDirectory": "../coverage",
71 | "testEnvironment": "node"
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/src/app.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get, Param } from '@nestjs/common';
2 | import { AppService } from './app.service';
3 |
4 | @Controller()
5 | export class AppController {
6 | constructor(private readonly appService: AppService) {}
7 |
8 | //We had taken name into route param
9 | //We cached redis name data
10 | @Get('/set/:name')
11 | async setName(
12 | @Param('name') name:string
13 | ): Promise {
14 | await this.appService.setData(name)
15 | }
16 |
17 | // We got username from redis | You should control redis insight
18 | @Get('/get')
19 | async getName(): Promise {
20 | return await this.appService.getData()
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/src/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import * as redisStore from 'cache-manager-redis-store';
3 | import { AppController } from './app.controller';
4 | import { AppService } from './app.service';
5 | import { CacheModule } from '@nestjs/cache-manager';
6 |
7 | @Module({
8 | imports: [
9 | // We are setting redis config as async format
10 | CacheModule.registerAsync(
11 | {
12 | useFactory:()=>({
13 | ttl:5,
14 | isGlobal: true,
15 | store: redisStore,
16 | host: 'localhost',
17 | port: 6379,
18 | })
19 | }
20 | ),
21 | ],
22 | controllers: [AppController],
23 | providers: [AppService],
24 | })
25 | export class AppModule {}
26 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/src/app.service.ts:
--------------------------------------------------------------------------------
1 | import { CACHE_MANAGER } from '@nestjs/cache-manager';
2 | import { Inject, Injectable } from '@nestjs/common';
3 | import { Cache } from 'cache-manager';
4 |
5 | @Injectable()
6 | export class AppService {
7 | constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
8 |
9 |
10 | async getData(): Promise {
11 | const name=this.cacheManager.get('username')
12 | return name
13 | }
14 | async setData(name:string): Promise {
15 | await this.cacheManager.set('username',name)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/src/main.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { AppModule } from './app.module';
3 |
4 | async function bootstrap() {
5 | const app = await NestFactory.create(AppModule);
6 | await app.listen(3000);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/test/app.e2e-spec.ts:
--------------------------------------------------------------------------------
1 | import { Test, TestingModule } from '@nestjs/testing';
2 | import { INestApplication } from '@nestjs/common';
3 | import * as request from 'supertest';
4 | import { AppModule } from './../src/app.module';
5 |
6 | describe('AppController (e2e)', () => {
7 | let app: INestApplication;
8 |
9 | beforeEach(async () => {
10 | const moduleFixture: TestingModule = await Test.createTestingModule({
11 | imports: [AppModule],
12 | }).compile();
13 |
14 | app = moduleFixture.createNestApplication();
15 | await app.init();
16 | });
17 |
18 | it('/ (GET)', () => {
19 | return request(app.getHttpServer())
20 | .get('/')
21 | .expect(200)
22 | .expect('Hello World!');
23 | });
24 | });
25 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/test/jest-e2e.json:
--------------------------------------------------------------------------------
1 | {
2 | "moduleFileExtensions": ["js", "json", "ts"],
3 | "rootDir": ".",
4 | "testEnvironment": "node",
5 | "testRegex": ".e2e-spec.ts$",
6 | "transform": {
7 | "^.+\\.(t|j)s$": "ts-jest"
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/tsconfig.build.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "./tsconfig.json",
3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
4 | }
5 |
--------------------------------------------------------------------------------
/distrubuted-cache--redis/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": true,
5 | "removeComments": true,
6 | "emitDecoratorMetadata": true,
7 | "experimentalDecorators": true,
8 | "allowSyntheticDefaultImports": true,
9 | "target": "ES2021",
10 | "sourceMap": true,
11 | "outDir": "./dist",
12 | "baseUrl": "./",
13 | "incremental": true,
14 | "skipLibCheck": true,
15 | "strictNullChecks": false,
16 | "noImplicitAny": false,
17 | "strictBindCallApply": false,
18 | "forceConsistentCasingInFileNames": false,
19 | "noFallthroughCasesInSwitch": false
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/in-memory-cache/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | parser: '@typescript-eslint/parser',
3 | parserOptions: {
4 | project: 'tsconfig.json',
5 | tsconfigRootDir: __dirname,
6 | sourceType: 'module',
7 | },
8 | plugins: ['@typescript-eslint/eslint-plugin'],
9 | extends: [
10 | 'plugin:@typescript-eslint/recommended',
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 | };
26 |
--------------------------------------------------------------------------------
/in-memory-cache/.gitignore:
--------------------------------------------------------------------------------
1 | # compiled output
2 | /dist
3 | /node_modules
4 | /build
5 |
6 | # Logs
7 | logs
8 | *.log
9 | npm-debug.log*
10 | pnpm-debug.log*
11 | yarn-debug.log*
12 | yarn-error.log*
13 | lerna-debug.log*
14 |
15 | # OS
16 | .DS_Store
17 |
18 | # Tests
19 | /coverage
20 | /.nyc_output
21 |
22 | # IDEs and editors
23 | /.idea
24 | .project
25 | .classpath
26 | .c9/
27 | *.launch
28 | .settings/
29 | *.sublime-workspace
30 |
31 | # IDE - VSCode
32 | .vscode/*
33 | !.vscode/settings.json
34 | !.vscode/tasks.json
35 | !.vscode/launch.json
36 | !.vscode/extensions.json
37 |
38 | # dotenv environment variable files
39 | .env
40 | .env.development.local
41 | .env.test.local
42 | .env.production.local
43 | .env.local
44 |
45 | # temp directory
46 | .temp
47 | .tmp
48 |
49 | # Runtime data
50 | pids
51 | *.pid
52 | *.seed
53 | *.pid.lock
54 |
55 | # Diagnostic reports (https://nodejs.org/api/report.html)
56 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
57 |
--------------------------------------------------------------------------------
/in-memory-cache/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true,
3 | "trailingComma": "all"
4 | }
--------------------------------------------------------------------------------
/in-memory-cache/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6 | [circleci-url]: https://circleci.com/gh/nestjs/nest
7 |
8 | A progressive Node.js framework for building efficient and scalable server-side applications.
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
24 |
25 | ## Description
26 |
27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
28 |
29 | ## Installation
30 |
31 | ```bash
32 | $ npm install
33 | ```
34 |
35 | ## Running the app
36 |
37 | ```bash
38 | # development
39 | $ npm run start
40 |
41 | # watch mode
42 | $ npm run start:dev
43 |
44 | # production mode
45 | $ npm run start:prod
46 | ```
47 |
48 | ## Test
49 |
50 | ```bash
51 | # unit tests
52 | $ npm run test
53 |
54 | # e2e tests
55 | $ npm run test:e2e
56 |
57 | # test coverage
58 | $ npm run test:cov
59 | ```
60 |
61 | ## Support
62 |
63 | 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).
64 |
65 | ## Stay in touch
66 |
67 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
68 | - Website - [https://nestjs.com](https://nestjs.com/)
69 | - Twitter - [@nestframework](https://twitter.com/nestframework)
70 |
71 | ## License
72 |
73 | Nest is [MIT licensed](LICENSE).
74 |
--------------------------------------------------------------------------------
/in-memory-cache/nest-cli.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://json.schemastore.org/nest-cli",
3 | "collection": "@nestjs/schematics",
4 | "sourceRoot": "src",
5 | "compilerOptions": {
6 | "deleteOutDir": true
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/in-memory-cache/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "in-memory-cache",
3 | "version": "0.0.1",
4 | "description": "",
5 | "author": "",
6 | "private": true,
7 | "license": "UNLICENSED",
8 | "scripts": {
9 | "build": "nest build",
10 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
11 | "start": "nest start",
12 | "start:dev": "nest start --watch",
13 | "start:debug": "nest start --debug --watch",
14 | "start:prod": "node dist/main",
15 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
16 | "test": "jest",
17 | "test:watch": "jest --watch",
18 | "test:cov": "jest --coverage",
19 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
20 | "test:e2e": "jest --config ./test/jest-e2e.json"
21 | },
22 | "dependencies": {
23 | "@nestjs/cache-manager": "^2.2.2",
24 | "@nestjs/common": "^10.0.0",
25 | "@nestjs/core": "^10.0.0",
26 | "@nestjs/platform-express": "^10.0.0",
27 | "cache-manager": "^5.7.6",
28 | "reflect-metadata": "^0.2.0",
29 | "rxjs": "^7.8.1"
30 | },
31 | "devDependencies": {
32 | "@nestjs/cli": "^10.0.0",
33 | "@nestjs/schematics": "^10.0.0",
34 | "@nestjs/testing": "^10.0.0",
35 | "@types/express": "^4.17.17",
36 | "@types/jest": "^29.5.2",
37 | "@types/node": "^20.3.1",
38 | "@types/supertest": "^6.0.0",
39 | "@typescript-eslint/eslint-plugin": "^6.0.0",
40 | "@typescript-eslint/parser": "^6.0.0",
41 | "eslint": "^8.42.0",
42 | "eslint-config-prettier": "^9.0.0",
43 | "eslint-plugin-prettier": "^5.0.0",
44 | "jest": "^29.5.0",
45 | "prettier": "^3.0.0",
46 | "source-map-support": "^0.5.21",
47 | "supertest": "^6.3.3",
48 | "ts-jest": "^29.1.0",
49 | "ts-loader": "^9.4.3",
50 | "ts-node": "^10.9.1",
51 | "tsconfig-paths": "^4.2.0",
52 | "typescript": "^5.1.3"
53 | },
54 | "jest": {
55 | "moduleFileExtensions": [
56 | "js",
57 | "json",
58 | "ts"
59 | ],
60 | "rootDir": "src",
61 | "testRegex": ".*\\.spec\\.ts$",
62 | "transform": {
63 | "^.+\\.(t|j)s$": "ts-jest"
64 | },
65 | "collectCoverageFrom": [
66 | "**/*.(t|j)s"
67 | ],
68 | "coverageDirectory": "../coverage",
69 | "testEnvironment": "node"
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/in-memory-cache/src/app.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller, Get, Param, Query } from '@nestjs/common';
2 | import { AppService } from './app.service';
3 |
4 | @Controller()
5 | export class AppController {
6 | constructor(private readonly appService: AppService) {}
7 |
8 | //We had taken name into route param
9 | //We cached name data | default TTL:5 second
10 | @Get('/set/:name')
11 | async setName(
12 | @Param('name') name:string
13 | ): Promise {
14 | console.log(name);
15 |
16 | await this.appService.setData(name)
17 | }
18 |
19 | // We got username from in memory as cached
20 | @Get('/get')
21 | async getName(): Promise {
22 | return await this.appService.getData()
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/in-memory-cache/src/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { AppController } from './app.controller';
3 | import { AppService } from './app.service';
4 | import { CacheModule } from '@nestjs/cache-manager';
5 |
6 | @Module({
7 | imports: [CacheModule.register({
8 | //? If you want, chance absolute time and sliding time
9 | //ttl:5,
10 | })],
11 | controllers: [AppController],
12 | providers: [AppService],
13 | })
14 | export class AppModule {}
15 |
--------------------------------------------------------------------------------
/in-memory-cache/src/app.service.ts:
--------------------------------------------------------------------------------
1 | import { CACHE_MANAGER } from '@nestjs/cache-manager';
2 | import { Inject, Injectable } from '@nestjs/common';
3 | import { Cache } from 'cache-manager';
4 |
5 | @Injectable()
6 | export class AppService {
7 | constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}
8 |
9 |
10 | //! Cache manager stored data as key-value type
11 | async setData(username:string):Promise{
12 | await this.cacheManager.set('user', username);
13 | }
14 |
15 | async getData():Promise{
16 | const username= await this.cacheManager.get('user');
17 | return username
18 | }
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/in-memory-cache/src/main.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { AppModule } from './app.module';
3 |
4 | async function bootstrap() {
5 | const app = await NestFactory.create(AppModule);
6 | await app.listen(3000);
7 | }
8 | bootstrap();
9 |
--------------------------------------------------------------------------------
/in-memory-cache/test/app.e2e-spec.ts:
--------------------------------------------------------------------------------
1 | import { Test, TestingModule } from '@nestjs/testing';
2 | import { INestApplication } from '@nestjs/common';
3 | import * as request from 'supertest';
4 | import { AppModule } from '../src/app.module';
5 |
6 | describe('AppController (e2e)', () => {
7 | let app: INestApplication;
8 |
9 | beforeEach(async () => {
10 | const moduleFixture: TestingModule = await Test.createTestingModule({
11 | imports: [AppModule],
12 | }).compile();
13 |
14 | app = moduleFixture.createNestApplication();
15 | await app.init();
16 | });
17 |
18 | it('/ (GET)', () => {
19 | return request(app.getHttpServer())
20 | .get('/')
21 | .expect(200)
22 | .expect('Hello World!');
23 | });
24 | });
25 |
--------------------------------------------------------------------------------
/in-memory-cache/test/jest-e2e.json:
--------------------------------------------------------------------------------
1 | {
2 | "moduleFileExtensions": ["js", "json", "ts"],
3 | "rootDir": ".",
4 | "testEnvironment": "node",
5 | "testRegex": ".e2e-spec.ts$",
6 | "transform": {
7 | "^.+\\.(t|j)s$": "ts-jest"
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/in-memory-cache/tsconfig.build.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "./tsconfig.json",
3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
4 | }
5 |
--------------------------------------------------------------------------------
/in-memory-cache/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": true,
5 | "removeComments": true,
6 | "emitDecoratorMetadata": true,
7 | "experimentalDecorators": true,
8 | "allowSyntheticDefaultImports": true,
9 | "target": "ES2021",
10 | "sourceMap": true,
11 | "outDir": "./dist",
12 | "baseUrl": "./",
13 | "incremental": true,
14 | "skipLibCheck": true,
15 | "strictNullChecks": false,
16 | "noImplicitAny": false,
17 | "strictBindCallApply": false,
18 | "forceConsistentCasingInFileNames": false,
19 | "noFallthroughCasesInSwitch": false
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | parser: '@typescript-eslint/parser',
3 | parserOptions: {
4 | project: 'tsconfig.json',
5 | tsconfigRootDir: __dirname,
6 | sourceType: 'module',
7 | },
8 | plugins: ['@typescript-eslint/eslint-plugin'],
9 | extends: [
10 | 'plugin:@typescript-eslint/recommended',
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 | };
26 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/.gitignore:
--------------------------------------------------------------------------------
1 | # compiled output
2 | /dist
3 | /node_modules
4 | /build
5 |
6 | # Logs
7 | logs
8 | *.log
9 | npm-debug.log*
10 | pnpm-debug.log*
11 | yarn-debug.log*
12 | yarn-error.log*
13 | lerna-debug.log*
14 |
15 | # OS
16 | .DS_Store
17 |
18 | # Tests
19 | /coverage
20 | /.nyc_output
21 |
22 | # IDEs and editors
23 | /.idea
24 | .project
25 | .classpath
26 | .c9/
27 | *.launch
28 | .settings/
29 | *.sublime-workspace
30 |
31 | # IDE - VSCode
32 | .vscode/*
33 | !.vscode/settings.json
34 | !.vscode/tasks.json
35 | !.vscode/launch.json
36 | !.vscode/extensions.json
37 |
38 | # dotenv environment variable files
39 | .env
40 | .env.development.local
41 | .env.test.local
42 | .env.production.local
43 | .env.local
44 |
45 | # temp directory
46 | .temp
47 | .tmp
48 |
49 | # Runtime data
50 | pids
51 | *.pid
52 | *.seed
53 | *.pid.lock
54 |
55 | # Diagnostic reports (https://nodejs.org/api/report.html)
56 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
57 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true,
3 | "trailingComma": "all"
4 | }
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6 | [circleci-url]: https://circleci.com/gh/nestjs/nest
7 |
8 | A progressive Node.js framework for building efficient and scalable server-side applications.
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
24 |
25 | ## Description
26 |
27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
28 |
29 | ## Installation
30 |
31 | ```bash
32 | $ npm install
33 | ```
34 |
35 | ## Running the app
36 |
37 | ```bash
38 | # development
39 | $ npm run start
40 |
41 | # watch mode
42 | $ npm run start:dev
43 |
44 | # production mode
45 | $ npm run start:prod
46 | ```
47 |
48 | ## Test
49 |
50 | ```bash
51 | # unit tests
52 | $ npm run test
53 |
54 | # e2e tests
55 | $ npm run test:e2e
56 |
57 | # test coverage
58 | $ npm run test:cov
59 | ```
60 |
61 | ## Support
62 |
63 | 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).
64 |
65 | ## Stay in touch
66 |
67 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
68 | - Website - [https://nestjs.com](https://nestjs.com/)
69 | - Twitter - [@nestframework](https://twitter.com/nestframework)
70 |
71 | ## License
72 |
73 | Nest is [MIT licensed](LICENSE).
74 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/nest-cli.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://json.schemastore.org/nest-cli",
3 | "collection": "@nestjs/schematics",
4 | "sourceRoot": "src",
5 | "compilerOptions": {
6 | "deleteOutDir": true
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "redis-publisher",
3 | "version": "0.0.1",
4 | "description": "",
5 | "author": "",
6 | "private": true,
7 | "license": "UNLICENSED",
8 | "scripts": {
9 | "build": "nest build",
10 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
11 | "start": "nest start",
12 | "start:dev": "nest start --watch",
13 | "start:debug": "nest start --debug --watch",
14 | "start:prod": "node dist/main",
15 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
16 | "test": "jest",
17 | "test:watch": "jest --watch",
18 | "test:cov": "jest --coverage",
19 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
20 | "test:e2e": "jest --config ./test/jest-e2e.json"
21 | },
22 | "dependencies": {
23 | "@nestjs/common": "^10.0.0",
24 | "@nestjs/core": "^10.0.0",
25 | "@nestjs/microservices": "^10.4.1",
26 | "@nestjs/platform-express": "^10.0.0",
27 | "ioredis": "^5.4.1",
28 | "redis": "^4.7.0",
29 | "reflect-metadata": "^0.2.0",
30 | "rxjs": "^7.8.1"
31 | },
32 | "devDependencies": {
33 | "@nestjs/cli": "^10.0.0",
34 | "@nestjs/schematics": "^10.0.0",
35 | "@nestjs/testing": "^10.0.0",
36 | "@types/express": "^4.17.17",
37 | "@types/jest": "^29.5.2",
38 | "@types/node": "^20.3.1",
39 | "@types/supertest": "^6.0.0",
40 | "@typescript-eslint/eslint-plugin": "^6.0.0",
41 | "@typescript-eslint/parser": "^6.0.0",
42 | "eslint": "^8.42.0",
43 | "eslint-config-prettier": "^9.0.0",
44 | "eslint-plugin-prettier": "^5.0.0",
45 | "jest": "^29.5.0",
46 | "prettier": "^3.0.0",
47 | "source-map-support": "^0.5.21",
48 | "supertest": "^6.3.3",
49 | "ts-jest": "^29.1.0",
50 | "ts-loader": "^9.4.3",
51 | "ts-node": "^10.9.1",
52 | "tsconfig-paths": "^4.2.0",
53 | "typescript": "^5.1.3"
54 | },
55 | "jest": {
56 | "moduleFileExtensions": [
57 | "js",
58 | "json",
59 | "ts"
60 | ],
61 | "rootDir": "src",
62 | "testRegex": ".*\\.spec\\.ts$",
63 | "transform": {
64 | "^.+\\.(t|j)s$": "ts-jest"
65 | },
66 | "collectCoverageFrom": [
67 | "**/*.(t|j)s"
68 | ],
69 | "coverageDirectory": "../coverage",
70 | "testEnvironment": "node"
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/src/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { PublisherModule } from './publisher/publisher.module';
3 |
4 | @Module({
5 | imports: [PublisherModule],
6 | controllers: [],
7 | providers: [],
8 | })
9 | export class AppModule {}
10 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/src/main.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { AppModule } from './app.module';
3 |
4 | async function bootstrap() {
5 | // Ana NestJS uygulamasını oluştur
6 | const app = await NestFactory.create(AppModule);
7 |
8 | // HTTP sunucusunu 3000 portunda başlat
9 | await app.listen(3000);
10 | }
11 | bootstrap();
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/src/publisher/publisher.controller.ts:
--------------------------------------------------------------------------------
1 | import { Body, Controller, Post } from '@nestjs/common';
2 | import { PublisherService } from './publisher.service';
3 | import { MessagePattern, Payload } from '@nestjs/microservices';
4 |
5 | @Controller('publish')
6 | export class PublisherController {
7 | constructor(private readonly publisherService:PublisherService){}
8 |
9 | @Post()
10 | async publishMessage(
11 | @Body() data:{
12 | channel:string,
13 | message:string
14 | }
15 | ){
16 | await this.publisherService.publishMessage(data.channel,data.message)
17 | return {status:'success',message:'Mesaj yayınlandı'}
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/src/publisher/publisher.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { PublisherController } from './publisher.controller';
3 | import { PublisherService } from './publisher.service';
4 | import { ClientsModule, Transport } from '@nestjs/microservices';
5 |
6 | @Module({
7 | imports:[
8 | ClientsModule.register([
9 | {
10 | name: 'REDIS_PUBLISHER', // Redis istemcisi için benzersiz bir isim
11 | transport: Transport.REDIS, // Redis'i iletişim protokolü olarak kullan
12 | options: {
13 | host: 'localhost',
14 | port: 6379,
15 | }
16 | },
17 | ]),
18 | ],
19 | controllers: [PublisherController],
20 | providers: [PublisherService]
21 | })
22 | export class PublisherModule {}
23 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/src/publisher/publisher.service.ts:
--------------------------------------------------------------------------------
1 | import { Inject, Injectable } from '@nestjs/common';
2 | import { ClientProxy } from '@nestjs/microservices';
3 |
4 | @Injectable()
5 | export class PublisherService {
6 | constructor(
7 | // Redis istemcisini enjekte et
8 | @Inject('REDIS_PUBLISHER') private client: ClientProxy
9 | ){}
10 |
11 | // Belirtilen kanala mesaj yayınla
12 | async publishMessage(channel:string,message:string){
13 | return this.client.emit(channel,message)
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/test/app.e2e-spec.ts:
--------------------------------------------------------------------------------
1 | import { Test, TestingModule } from '@nestjs/testing';
2 | import { INestApplication } from '@nestjs/common';
3 | import * as request from 'supertest';
4 | import { AppModule } from './../src/app.module';
5 |
6 | describe('AppController (e2e)', () => {
7 | let app: INestApplication;
8 |
9 | beforeEach(async () => {
10 | const moduleFixture: TestingModule = await Test.createTestingModule({
11 | imports: [AppModule],
12 | }).compile();
13 |
14 | app = moduleFixture.createNestApplication();
15 | await app.init();
16 | });
17 |
18 | it('/ (GET)', () => {
19 | return request(app.getHttpServer())
20 | .get('/')
21 | .expect(200)
22 | .expect('Hello World!');
23 | });
24 | });
25 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/test/jest-e2e.json:
--------------------------------------------------------------------------------
1 | {
2 | "moduleFileExtensions": ["js", "json", "ts"],
3 | "rootDir": ".",
4 | "testEnvironment": "node",
5 | "testRegex": ".e2e-spec.ts$",
6 | "transform": {
7 | "^.+\\.(t|j)s$": "ts-jest"
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/tsconfig.build.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "./tsconfig.json",
3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
4 | }
5 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-publisher/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": true,
5 | "removeComments": true,
6 | "emitDecoratorMetadata": true,
7 | "experimentalDecorators": true,
8 | "allowSyntheticDefaultImports": true,
9 | "target": "ES2021",
10 | "sourceMap": true,
11 | "outDir": "./dist",
12 | "baseUrl": "./",
13 | "incremental": true,
14 | "skipLibCheck": true,
15 | "strictNullChecks": false,
16 | "noImplicitAny": false,
17 | "strictBindCallApply": false,
18 | "forceConsistentCasingInFileNames": false,
19 | "noFallthroughCasesInSwitch": false
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | parser: '@typescript-eslint/parser',
3 | parserOptions: {
4 | project: 'tsconfig.json',
5 | tsconfigRootDir: __dirname,
6 | sourceType: 'module',
7 | },
8 | plugins: ['@typescript-eslint/eslint-plugin'],
9 | extends: [
10 | 'plugin:@typescript-eslint/recommended',
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 | };
26 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/.gitignore:
--------------------------------------------------------------------------------
1 | # compiled output
2 | /dist
3 | /node_modules
4 | /build
5 |
6 | # Logs
7 | logs
8 | *.log
9 | npm-debug.log*
10 | pnpm-debug.log*
11 | yarn-debug.log*
12 | yarn-error.log*
13 | lerna-debug.log*
14 |
15 | # OS
16 | .DS_Store
17 |
18 | # Tests
19 | /coverage
20 | /.nyc_output
21 |
22 | # IDEs and editors
23 | /.idea
24 | .project
25 | .classpath
26 | .c9/
27 | *.launch
28 | .settings/
29 | *.sublime-workspace
30 |
31 | # IDE - VSCode
32 | .vscode/*
33 | !.vscode/settings.json
34 | !.vscode/tasks.json
35 | !.vscode/launch.json
36 | !.vscode/extensions.json
37 |
38 | # dotenv environment variable files
39 | .env
40 | .env.development.local
41 | .env.test.local
42 | .env.production.local
43 | .env.local
44 |
45 | # temp directory
46 | .temp
47 | .tmp
48 |
49 | # Runtime data
50 | pids
51 | *.pid
52 | *.seed
53 | *.pid.lock
54 |
55 | # Diagnostic reports (https://nodejs.org/api/report.html)
56 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
57 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "singleQuote": true,
3 | "trailingComma": "all"
4 | }
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | [circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6 | [circleci-url]: https://circleci.com/gh/nestjs/nest
7 |
8 | A progressive Node.js framework for building efficient and scalable server-side applications.
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
24 |
25 | ## Description
26 |
27 | [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
28 |
29 | ## Installation
30 |
31 | ```bash
32 | $ npm install
33 | ```
34 |
35 | ## Running the app
36 |
37 | ```bash
38 | # development
39 | $ npm run start
40 |
41 | # watch mode
42 | $ npm run start:dev
43 |
44 | # production mode
45 | $ npm run start:prod
46 | ```
47 |
48 | ## Test
49 |
50 | ```bash
51 | # unit tests
52 | $ npm run test
53 |
54 | # e2e tests
55 | $ npm run test:e2e
56 |
57 | # test coverage
58 | $ npm run test:cov
59 | ```
60 |
61 | ## Support
62 |
63 | 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).
64 |
65 | ## Stay in touch
66 |
67 | - Author - [Kamil Myśliwiec](https://kamilmysliwiec.com)
68 | - Website - [https://nestjs.com](https://nestjs.com/)
69 | - Twitter - [@nestframework](https://twitter.com/nestframework)
70 |
71 | ## License
72 |
73 | Nest is [MIT licensed](LICENSE).
74 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/nest-cli.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://json.schemastore.org/nest-cli",
3 | "collection": "@nestjs/schematics",
4 | "sourceRoot": "src",
5 | "compilerOptions": {
6 | "deleteOutDir": true
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "redis-subscribe",
3 | "version": "0.0.1",
4 | "description": "",
5 | "author": "",
6 | "private": true,
7 | "license": "UNLICENSED",
8 | "scripts": {
9 | "build": "nest build",
10 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
11 | "start": "nest start",
12 | "start:dev": "nest start --watch",
13 | "start:debug": "nest start --debug --watch",
14 | "start:prod": "node dist/main",
15 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
16 | "test": "jest",
17 | "test:watch": "jest --watch",
18 | "test:cov": "jest --coverage",
19 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
20 | "test:e2e": "jest --config ./test/jest-e2e.json"
21 | },
22 | "dependencies": {
23 | "@nestjs/common": "^10.0.0",
24 | "@nestjs/core": "^10.0.0",
25 | "@nestjs/microservices": "^10.4.1",
26 | "@nestjs/platform-express": "^10.0.0",
27 | "ioredis": "^5.4.1",
28 | "reflect-metadata": "^0.2.0",
29 | "rxjs": "^7.8.1"
30 | },
31 | "devDependencies": {
32 | "@nestjs/cli": "^10.0.0",
33 | "@nestjs/schematics": "^10.0.0",
34 | "@nestjs/testing": "^10.0.0",
35 | "@types/express": "^4.17.17",
36 | "@types/jest": "^29.5.2",
37 | "@types/node": "^20.3.1",
38 | "@types/supertest": "^6.0.0",
39 | "@typescript-eslint/eslint-plugin": "^6.0.0",
40 | "@typescript-eslint/parser": "^6.0.0",
41 | "eslint": "^8.42.0",
42 | "eslint-config-prettier": "^9.0.0",
43 | "eslint-plugin-prettier": "^5.0.0",
44 | "jest": "^29.5.0",
45 | "prettier": "^3.0.0",
46 | "source-map-support": "^0.5.21",
47 | "supertest": "^6.3.3",
48 | "ts-jest": "^29.1.0",
49 | "ts-loader": "^9.4.3",
50 | "ts-node": "^10.9.1",
51 | "tsconfig-paths": "^4.2.0",
52 | "typescript": "^5.1.3"
53 | },
54 | "jest": {
55 | "moduleFileExtensions": [
56 | "js",
57 | "json",
58 | "ts"
59 | ],
60 | "rootDir": "src",
61 | "testRegex": ".*\\.spec\\.ts$",
62 | "transform": {
63 | "^.+\\.(t|j)s$": "ts-jest"
64 | },
65 | "collectCoverageFrom": [
66 | "**/*.(t|j)s"
67 | ],
68 | "coverageDirectory": "../coverage",
69 | "testEnvironment": "node"
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/src/app.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { SubscribeModule } from './subscribe/subscribe.module';
3 |
4 | @Module({
5 | imports: [SubscribeModule],
6 | controllers: [],
7 | providers: [],
8 | })
9 | export class AppModule {}
10 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/src/main.ts:
--------------------------------------------------------------------------------
1 | import { NestFactory } from '@nestjs/core';
2 | import { AppModule } from './app.module';
3 | import { MicroserviceOptions, Transport } from '@nestjs/microservices';
4 |
5 | async function bootstrap() {
6 | // Uygulamayı doğrudan bir mikroservis (redis-microservice) olarak oluştur
7 | const app = await NestFactory.createMicroservice(AppModule, {
8 | transport: Transport.REDIS,
9 | options: {
10 | host: 'localhost',
11 | port: 6379,
12 | },
13 | });
14 |
15 | // Mikroservisi başlat ve dinlemeye başla
16 | await app.listen();
17 | }
18 | bootstrap();
19 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/src/subscribe/subscribe.controller.ts:
--------------------------------------------------------------------------------
1 | import { Controller } from '@nestjs/common';
2 | import { SubscribeService } from './subscribe.service';
3 | import { EventPattern, Payload } from '@nestjs/microservices';
4 |
5 | @Controller('subscribe')
6 | export class SubscribeController {
7 | constructor(private readonly subscriberService: SubscribeService) {}
8 |
9 | // 'kanal1' adlı Redis kanalını dinle
10 | @EventPattern('kanal1')
11 | handleMessage(
12 | @Payload() message: string
13 | ) {
14 | return this.subscriberService.handleMessage(message);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/src/subscribe/subscribe.module.ts:
--------------------------------------------------------------------------------
1 | import { Module } from '@nestjs/common';
2 | import { SubscribeController } from './subscribe.controller';
3 | import { SubscribeService } from './subscribe.service';
4 |
5 | @Module({
6 | controllers: [SubscribeController],
7 | providers: [SubscribeService]
8 | })
9 | export class SubscribeModule {}
10 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/src/subscribe/subscribe.service.ts:
--------------------------------------------------------------------------------
1 | import { Injectable } from '@nestjs/common';
2 |
3 | @Injectable()
4 | export class SubscribeService {
5 | handleMessage(message: string) {
6 | console.log('Alınan mesaj:', message);
7 | return { received: true, message };
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/test/app.e2e-spec.ts:
--------------------------------------------------------------------------------
1 | import { Test, TestingModule } from '@nestjs/testing';
2 | import { INestApplication } from '@nestjs/common';
3 | import * as request from 'supertest';
4 | import { AppModule } from './../src/app.module';
5 |
6 | describe('AppController (e2e)', () => {
7 | let app: INestApplication;
8 |
9 | beforeEach(async () => {
10 | const moduleFixture: TestingModule = await Test.createTestingModule({
11 | imports: [AppModule],
12 | }).compile();
13 |
14 | app = moduleFixture.createNestApplication();
15 | await app.init();
16 | });
17 |
18 | it('/ (GET)', () => {
19 | return request(app.getHttpServer())
20 | .get('/')
21 | .expect(200)
22 | .expect('Hello World!');
23 | });
24 | });
25 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/test/jest-e2e.json:
--------------------------------------------------------------------------------
1 | {
2 | "moduleFileExtensions": ["js", "json", "ts"],
3 | "rootDir": ".",
4 | "testEnvironment": "node",
5 | "testRegex": ".e2e-spec.ts$",
6 | "transform": {
7 | "^.+\\.(t|j)s$": "ts-jest"
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/tsconfig.build.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "./tsconfig.json",
3 | "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
4 | }
5 |
--------------------------------------------------------------------------------
/microservices--pub-sub/redis-subscribe/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "declaration": true,
5 | "removeComments": true,
6 | "emitDecoratorMetadata": true,
7 | "experimentalDecorators": true,
8 | "allowSyntheticDefaultImports": true,
9 | "target": "ES2021",
10 | "sourceMap": true,
11 | "outDir": "./dist",
12 | "baseUrl": "./",
13 | "incremental": true,
14 | "skipLibCheck": true,
15 | "strictNullChecks": false,
16 | "noImplicitAny": false,
17 | "strictBindCallApply": false,
18 | "forceConsistentCasingInFileNames": false,
19 | "noFallthroughCasesInSwitch": false
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/redis-sentinel/sentinel.conf:
--------------------------------------------------------------------------------
1 | # Monitor the master replica on 172.19.0.2, port 6379 with a name mymaster
2 | sentinel monitor mymaster 172.19.0.2 6379 1
3 |
4 | # Consider a master down after 5000 milliseconds
5 | sentinel down-after-milliseconds mymaster 5000
6 |
7 | # Wait 10000 milliseconds for a failover
8 | sentinel failover-timeout mymaster 10000
9 |
10 | # Attempt to synchronize up to 3 replicas in parallel
11 | sentinel parallel-syncs mymaster 3
12 |
13 |
14 | This configuration will help in monitoring and managing your Redis master and replicas.
15 |
--------------------------------------------------------------------------------