Skip to content

Commit 9d1f489

Browse files
committed
fix(api-core): implemented redis and nats modules
1 parent a36b38a commit 9d1f489

File tree

12 files changed

+399
-27
lines changed

12 files changed

+399
-27
lines changed

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
.PHONY: docker dev-docker docker-dowm setup deploy-server
2+
3+
# Target to build Docker containers
4+
docker:
5+
docker compose -f docker-compose.yml --env-file ./.env up --build -d
6+
7+
# Target to build Docker containers
8+
dev-docker:
9+
docker compose -f docker-compose.yml --env-file ./.env.development up --build -V
10+
11+
docker-clean:
12+
docker compose rm -f
13+
14+
# Target to stop Docker containers
15+
docker-down:
16+
docker compose down
17+
118
setup:
219
./scripts/project-setup.sh
320

apps/api-gateway/Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ ENV PNPM_HOME="/pnpm"
44
ENV PATH="$PNPM_HOME:$PATH"
55
RUN corepack enable pnpm
66

7+
# Install Python and build tools
8+
RUN apk add --no-cache python3 make g++ build-base
9+
710
# DEFINING DEVELOPMENT STAGE
811
FROM base AS dev
912

@@ -28,6 +31,4 @@ COPY --from=dev app/ .
2831

2932
USER node
3033

31-
EXPOSE 8080
32-
#RUN sleep 500;
33-
#CMD ["node", "/apps/api-gateway/dist/main"]
34+
CMD ["node", "apps/api-gateway/dist/main"]

apps/api-gateway/package.json

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@
66
"license": "UNLICENSED",
77
"author": "",
88
"scripts": {
9+
"prebuild": "rm -rf dist",
910
"build": "pnpm with-env nest build",
10-
"dev": "pnpm with-env-dev nest start --watch",
11+
"clean": "rm -rf .turbo node_modules dist",
12+
"dev": "pnpm with-env nest start --watch",
1113
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
12-
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
13-
"start": "pnpm with-env-dev nest start",
14-
"start:debug": "nest start --debug --watch",
14+
"lint": "eslint ./src",
15+
"lint:fix": "eslint ./src --fix",
16+
"start": "pnpm with-env nest start",
17+
"start:debug": "pnpm with-env nest start --debug --watch",
1518
"start:prod": "pnpm with-env node dist/main",
16-
"test": "jest",
17-
"test:cov": "jest --coverage",
18-
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
19-
"test:e2e": "jest --config ./test/jest-e2e.json",
20-
"test:watch": "jest --watch",
19+
"test": "pnpm with-env jest",
20+
"test:cov": "pnpm with-env jest --coverage",
21+
"test:debug": "pnpm with-env node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
22+
"test:e2e": "pnpm with-env jest --config ./test/jest-e2e.json",
23+
"test:watch": "pnpm with-env jest --watch",
2124
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
22-
"with-env": "dotenv -e ../../.env --",
23-
"with-env-dev": "dotenv -e ../../.env.development --"
25+
"with-env": "dotenv -e ../../.env --"
2426
},
2527
"prettier": "@app/prettier-config",
2628
"jest": {
@@ -50,6 +52,7 @@
5052
"@nestjs/platform-fastify": "catalog:nestjs",
5153
"class-transformer": "catalog:nestjs",
5254
"class-validator": "catalog:nestjs",
55+
"dotenv-cli": "catalog:",
5356
"nats": "catalog:nestjs",
5457
"reflect-metadata": "catalog:nestjs",
5558
"rxjs": "catalog:nestjs",
@@ -60,15 +63,19 @@
6063
"@app/constants": "workspace:*",
6164
"@app/eslint-config-nest": "workspace:*",
6265
"@app/tsconfig": "workspace:*",
66+
"@nestjs/cache-manager": "catalog:nestjs",
6367
"@nestjs/cli": "catalog:nestjs",
6468
"@nestjs/schematics": "catalog:nestjs",
6569
"@nestjs/testing": "catalog:nestjs",
6670
"@types/jest": "catalog:",
6771
"@types/node": "catalog:",
6872
"@types/supertest": "catalog:nestjs",
73+
"cache-manager": "catalog:nestjs",
74+
"cache-manager-redis-store": "catalog:nestjs",
6975
"eslint": "catalog:nestjs",
7076
"jest": "catalog:",
7177
"prettier": "catalog:",
78+
"redis": "catalog:nestjs",
7279
"source-map-support": "catalog:nestjs",
7380
"supertest": "catalog:nestjs",
7481
"ts-jest": "catalog:nestjs",

apps/webapp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"@types/node": "catalog:",
3535
"@types/react": "catalog:react18",
3636
"@types/react-dom": "catalog:react18",
37-
"dotenv-cli": "^7.4.2",
37+
"dotenv-cli": "catalog:",
3838
"eslint": "catalog:",
3939
"jiti": "^1.21.6",
4040
"prettier": "catalog:",

docker-compose.yml

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
services:
2-
api-gateway:
2+
nats:
3+
image: nats:latest
4+
ports:
5+
# Used for NATS itself
6+
- "4222:4222"
7+
# Used for NATS GUI
8+
- "8222:8222"
9+
command: "--jetstream"
10+
11+
redis:
12+
image: redis:latest
13+
ports:
14+
- "${REDIS_PORT}:6379"
15+
16+
# DEV
17+
18+
dev-api-gateway:
319
build:
420
context: .
521
dockerfile: apps/api-gateway/Dockerfile
6-
target: prod
22+
target: dev
23+
image: api-gateway:latest
724
ports:
825
- "8080:8080"
26+
volumes:
27+
- .:/app
28+
- /app/node_modules
929
env_file:
10-
- .env
30+
- .env.development
31+
environment:
32+
- NATS_HOST=nats
33+
depends_on:
34+
- nats
35+
command: ["pnpm", "run", "-C", "apps/api-gateway", "start"]
1136

1237
networks:
1338
default:

packages/api-core/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,23 @@
1515
},
1616
"prettier": "@app/prettier-config",
1717
"dependencies": {
18+
"@nestjs/cache-manager": "catalog:nestjs",
1819
"@nestjs/common": "catalog:nestjs",
1920
"@nestjs/config": "catalog:nestjs",
21+
"cache-manager": "catalog:nestjs",
22+
"cache-manager-redis-store": "catalog:nestjs",
2023
"class-transformer": "catalog:nestjs",
2124
"class-validator": "catalog:nestjs",
2225
"pino": "^9.3.1",
23-
"pino-pretty": "^11.2.1"
26+
"pino-pretty": "^11.2.1",
27+
"redis": "catalog:nestjs"
2428
},
2529
"devDependencies": {
2630
"@app/constants": "workspace:*",
2731
"@app/eslint-config-nest": "workspace:*",
2832
"@app/prettier-config": "workspace:*",
2933
"@app/tsconfig": "workspace:*",
34+
"dotenv-cli": "catalog:",
3035
"eslint": "catalog:nestjs",
3136
"prettier": "catalog:",
3237
"typescript": "catalog:"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const SERVICE_NAMES = {
2+
NATS: "NATS_SERVICE",
3+
};

packages/api-core/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import {
44
AllConfigType,
55
APIConfigModule,
66
} from "./modules/api-config/api-config.module";
7+
import { NatsModule } from "./modules/nats/nats.module";
8+
import { RedisModule } from "./modules/redis/redis.module";
79
import { logger } from "./utils/logger";
810

911
@Module({
10-
imports: [APIConfigModule],
12+
imports: [APIConfigModule, NatsModule, RedisModule],
1113
controllers: [],
1214
})
1315
export class APICoreModule {}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Module } from "@nestjs/common";
2+
import { ConfigService } from "@nestjs/config";
3+
import { ClientsModule, Transport } from "@nestjs/microservices";
4+
5+
import { SERVICE_NAMES } from "../../constants/service-names";
6+
import { APIConfigModule } from "../api-config/api-config.module";
7+
8+
@Module({
9+
imports: [
10+
ClientsModule.registerAsync([
11+
{
12+
name: SERVICE_NAMES.NATS,
13+
imports: [APIConfigModule],
14+
inject: [ConfigService],
15+
useFactory: async (configService: ConfigService) => {
16+
const natsPort = await configService.get("app.NATS_PORT");
17+
const natsHost = await configService.get("app.NATS_HOST");
18+
return {
19+
transport: Transport.NATS,
20+
options: {
21+
servers: [`nats://${natsHost}:${natsPort}`],
22+
},
23+
};
24+
},
25+
},
26+
]),
27+
],
28+
exports: [
29+
ClientsModule.registerAsync([
30+
{
31+
name: SERVICE_NAMES.NATS,
32+
imports: [APIConfigModule],
33+
inject: [ConfigService],
34+
useFactory: async (configService: ConfigService) => {
35+
const natsPort = await configService.get("app.NATS_PORT");
36+
const natsHost = await configService.get("app.NATS_HOST");
37+
return {
38+
transport: Transport.NATS,
39+
options: {
40+
servers: [`nats://${natsHost}:${natsPort}`],
41+
},
42+
};
43+
},
44+
},
45+
]),
46+
],
47+
})
48+
export class NatsModule {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { CacheModule } from "@nestjs/cache-manager";
2+
import { Module } from "@nestjs/common";
3+
import { ConfigModule, ConfigService } from "@nestjs/config";
4+
import * as redisStore from "cache-manager-redis-store";
5+
6+
@Module({
7+
imports: [
8+
CacheModule.registerAsync({
9+
imports: [ConfigModule],
10+
useFactory: async (configService: ConfigService): Promise<unknown> => ({
11+
store: redisStore,
12+
host: configService.get("app.REDIS_HOST"),
13+
port: configService.get("app.REDIS_PORT"),
14+
ttl: 300,
15+
}),
16+
inject: [ConfigService],
17+
}),
18+
],
19+
})
20+
export class RedisModule {}

0 commit comments

Comments
 (0)