Skip to content

Commit 3596a3c

Browse files
authored
Merge pull request #72 from boostcampwm-2024/chore/redis-env-be
📦 chore: redis 환경 구축
2 parents 12a81b2 + 24afaf5 commit 3596a3c

File tree

7 files changed

+211
-1
lines changed

7 files changed

+211
-1
lines changed

.github/workflows/deploy.yml

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ jobs:
3636
echo "DB_PORT=${{ secrets.PRODUCT_DB_PORT }}" >> configs/.env.db.production
3737
echo "DB_USERNAME=${{ secrets.PRODUCT_DB_USERNAME }}" >> configs/.env.db.production
3838
echo "DB_PASSWORD=${{ secrets.PRODUCT_DB_PASSWORD }}" >> configs/.env.db.production
39+
echo "REDIS_HOST=${{secrets.REDIS_HOST }}" >> configs/.env.db.production
40+
echo "REDIS_PORT=${{secrets.REDIS_PORT}}" >> configs/.env.db.production
41+
echo "REDIS_USERNAME=${{secrets.REDIS_USERNAME}}" >> configs/.env.db.production
42+
echo "REDIS_PASSWORD=${{secrets.REDIS_PASSWORD}}" >> configs/.env.db.production
3943
4044
npm ci
4145
npm run build

server/package-lock.json

+159
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@nestjs/swagger": "^8.0.1",
2929
"@nestjs/typeorm": "^10.0.2",
3030
"cross-env": "^7.0.3",
31+
"ioredis": "^5.4.1",
3132
"mysql2": "^3.11.3",
3233
"nest-winston": "^1.9.7",
3334
"pm2": "^5.4.2",
@@ -50,6 +51,7 @@
5051
"eslint": "^9.0.0",
5152
"eslint-config-prettier": "^9.0.0",
5253
"eslint-plugin-prettier": "^5.0.0",
54+
"ioredis-mock": "^8.9.0",
5355
"jest": "^29.5.0",
5456
"prettier": "^3.0.0",
5557
"source-map-support": "^0.5.21",

server/src/app.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { winstonModule } from './common/logger/logger.module';
33
import { TypeOrmModule } from '@nestjs/typeorm';
44
import { ConfigModule, ConfigService } from '@nestjs/config';
55
import { loadDBSetting } from './common/database/load.config';
6+
import { RedisModule } from './redis/redis.module';
67

78
@Module({
89
imports: [
@@ -19,6 +20,7 @@ import { loadDBSetting } from './common/database/load.config';
1920
useFactory: (configService: ConfigService) =>
2021
loadDBSetting(configService),
2122
}),
23+
RedisModule,
2224
],
2325
controllers: [],
2426
providers: [],

server/src/common/database/load.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ConfigService } from '@nestjs/config';
33
export function loadDBSetting(configService: ConfigService) {
44
const type = configService.get<'mysql' | 'sqlite'>('DB_TYPE');
55
const database = configService.get<string>('DB_DATABASE');
6-
const host = configService.get<string | null>('DB_HOST');
6+
const host = configService.get<string>('DB_HOST');
77
const port = configService.get<number>('DB_PORT');
88
const username = configService.get<string>('DB_USERNAME');
99
const password = configService.get<string>('DB_PASSWORD');

server/src/redis/redis.module.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Global, Module } from '@nestjs/common';
2+
import { ConfigModule, ConfigService } from '@nestjs/config';
3+
import { RedisService } from './redis.service';
4+
import Redis from 'ioredis';
5+
import Redis_Mock from 'ioredis-mock';
6+
7+
@Global()
8+
@Module({
9+
imports: [ConfigModule],
10+
providers: [
11+
{
12+
provide: 'REDIS_CLIENT',
13+
inject: [ConfigService],
14+
useFactory: async (configService: ConfigService) => {
15+
const envType = process.env.NODE_ENV;
16+
let redis: Redis;
17+
if (envType === 'test') {
18+
redis = new Redis_Mock();
19+
} else {
20+
redis = new Redis({
21+
host: configService.get<string>('REDIS_HOST'),
22+
port: configService.get<number>('REDIS_PORT'),
23+
username: configService.get<string>('REDIS_USERNAME'),
24+
password: configService.get<string>('REDIS_PASSWORD'),
25+
});
26+
}
27+
return redis;
28+
},
29+
},
30+
RedisService,
31+
],
32+
exports: ['REDIS_CLIENT'],
33+
})
34+
export class RedisModule {}

server/src/redis/redis.service.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Inject, Injectable } from '@nestjs/common';
2+
import Redis from 'ioredis';
3+
4+
@Injectable()
5+
export class RedisService extends Redis {
6+
constructor(@Inject('REDIS_CLIENT') private readonly redisClient: Redis) {
7+
super(redisClient.options);
8+
}
9+
}

0 commit comments

Comments
 (0)