Skip to content

Commit 2574086

Browse files
Merge pull request #123 from boostcampwm-2024/dev
[Deploy] 3주차 스프린트 배포
2 parents 1f0f1e2 + ba3081d commit 2574086

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+2693
-467
lines changed

backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
"@nestjs/platform-express": "^10.0.0",
2626
"@nestjs/platform-socket.io": "^10.4.6",
2727
"@nestjs/websockets": "^10.4.6",
28+
"dotenv": "^16.4.5",
29+
"ioredis": "^5.4.1",
2830
"reflect-metadata": "^0.2.0",
2931
"rxjs": "^7.8.1",
3032
"socket.io": "^4.8.1"

backend/src/app.module.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { Module } from "@nestjs/common";
22
import { AppController } from "./app.controller";
33
import { AppService } from "./app.service";
4-
import { SocketModule } from "./socket/socket.module";
4+
import { SocketModule } from "./signaling-server/socket.module";
5+
import { RoomModule } from "./room/room.module";
6+
import { RedisModule } from "./redis/redis.module";
7+
8+
import "dotenv/config";
59

610
@Module({
7-
imports: [SocketModule],
11+
imports: [SocketModule, RoomModule, RedisModule],
812
controllers: [AppController],
913
providers: [AppService],
1014
})

backend/src/config/redis.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "dotenv/config";
2+
3+
export const redisConfig = {
4+
host: process.env.REDIS_HOST,
5+
port: parseInt(process.env.REDIS_PORT),
6+
password: process.env.REDIS_PASSWORD,
7+
};

backend/src/redis/redis.module.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Module } from "@nestjs/common";
2+
import { RedisService } from "./redis.service";
3+
4+
@Module({
5+
providers: [RedisService],
6+
})
7+
export class RedisModule {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from "@nestjs/testing";
2+
import { RedisService } from "./redis.service";
3+
4+
describe("RedisService", () => {
5+
let service: RedisService;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [RedisService],
10+
}).compile();
11+
12+
service = module.get<RedisService>(RedisService);
13+
});
14+
15+
it("should be defined", () => {
16+
expect(service).toBeDefined();
17+
});
18+
});

backend/src/redis/redis.service.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { Injectable } from "@nestjs/common";
2+
import Redis from "ioredis";
3+
import { redisConfig } from "../config/redis.config";
4+
import { stringify } from "ts-jest";
5+
6+
@Injectable()
7+
export class RedisService {
8+
private readonly client: Redis = new Redis({
9+
host: redisConfig.host,
10+
port: redisConfig.port,
11+
password: redisConfig.password,
12+
});
13+
14+
async set(key: string, value: any, ttl: number = 0) {
15+
if (typeof value === "object") value = JSON.stringify(value);
16+
17+
await this.client.set(key, value, "KEEPTTL");
18+
await this.client.expire(key, ttl);
19+
}
20+
21+
async get(key: string) {
22+
return this.client.get(key);
23+
}
24+
25+
async getTTL(key: string) {
26+
return this.client.ttl(key);
27+
}
28+
29+
async getKeys(query: string) {
30+
const keys: string[] = [];
31+
let cursor = "0";
32+
33+
do {
34+
const [nextCursor, matchedKeys] = await this.client.scan(
35+
cursor,
36+
"MATCH",
37+
query,
38+
"COUNT",
39+
"100"
40+
);
41+
cursor = nextCursor;
42+
keys.push(...matchedKeys);
43+
} while (cursor !== "0");
44+
return keys;
45+
}
46+
47+
async getHashValueByField(key: string, field: string) {
48+
return this.client.hget(key, field);
49+
}
50+
51+
async setHashValueByField(key: string, field: string, value: any) {
52+
if (typeof value !== "string") value = stringify(value);
53+
return this.client.hset(key, field, value);
54+
}
55+
56+
async delete(...keys: string[]) {
57+
return this.client.del(...keys);
58+
}
59+
60+
async getValues(query: string) {
61+
const keys = await this.getKeys(query);
62+
if (!keys.length) return null;
63+
return this.client.mget(keys);
64+
}
65+
66+
async getMap(query: string, valueType: "object" | "primitive" = "object") {
67+
const keys = await this.getKeys(query);
68+
const values = await this.getValues(query);
69+
if (!values) return null;
70+
71+
return keys.reduce((acc, key, index) => {
72+
acc[key] =
73+
valueType === "object"
74+
? JSON.parse(values[index])
75+
: values[index];
76+
return acc;
77+
}, {});
78+
}
79+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export interface CreateRoomDto {
2+
title: string;
3+
status: "PUBLIC" | "PRIVATE";
4+
nickname: string;
5+
socketId: string;
6+
maxParticipants?: number;
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from "@nestjs/testing";
2+
import { RoomController } from "./room.controller";
3+
4+
describe("RoomController", () => {
5+
let controller: RoomController;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
controllers: [RoomController],
10+
}).compile();
11+
12+
controller = module.get<RoomController>(RoomController);
13+
});
14+
15+
it("should be defined", () => {
16+
expect(controller).toBeDefined();
17+
});
18+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Controller, Get } from "@nestjs/common";
2+
import { RoomService } from "./room.service";
3+
4+
@Controller("rooms")
5+
export class RoomController {
6+
constructor(private readonly roomService: RoomService) {}
7+
8+
@Get()
9+
getPublicRooms() {
10+
return this.roomService.getPublicRoom();
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from "@nestjs/testing";
2+
import { RoomGateway } from "./room.gateway";
3+
4+
describe("RoomGateway", () => {
5+
let gateway: RoomGateway;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [RoomGateway],
10+
}).compile();
11+
12+
gateway = module.get<RoomGateway>(RoomGateway);
13+
});
14+
15+
it("should be defined", () => {
16+
expect(gateway).toBeDefined();
17+
});
18+
});

0 commit comments

Comments
 (0)