Skip to content

Commit

Permalink
[BE#296] config분리 및 ENV const로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
yeongbinim authored Dec 4, 2023
1 parent 17ee111 commit 98d99a9
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 53 deletions.
26 changes: 5 additions & 21 deletions BE/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,24 @@ import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { StudyLogsModule } from './study-logs/study-logs.module';
import { StudyLogs } from './study-logs/study-logs.entity';
import { Categories } from './categories/categories.entity';
import { CategoriesModule } from './categories/categories.module';
import { MatesModule } from './mates/mates.module';
import { UsersModule } from './users/users.module';
import { UsersModel } from './users/entity/users.entity';
import { PassportModule } from '@nestjs/passport';
import { AuthModule } from './auth/auth.module';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { Mates } from './mates/mates.entity';
import { LoggingMiddleware } from './common/logging.middleware';
import { LoggingMiddleware } from './common/middleware/logging.middleware';
import { typeormConfig } from './common/config/typeorm.config';
import { staticConfig } from './common/config/static.config';

@Module({
imports: [
ServeStaticModule.forRoot({
rootPath: join(__dirname, '../../..', 'apps'),
serveRoot: '/',
renderPath: '/',
}),
ServeStaticModule.forRoot(staticConfig),
ConfigModule.forRoot({
isGlobal: true,
}),
TypeOrmModule.forRootAsync({
useFactory: (config: ConfigService) => ({
type: 'mysql',
host: config.get<string>('DATABASE_HOST'),
port: config.get<number>('DATABASE_PORT'),
username: config.get<string>('DATABASE_USERNAME'),
password: config.get<string>('DATABASE_PASSWORD'),
database: config.get<string>('DATABASE_NAME'),
entities: [StudyLogs, Categories, UsersModel, Mates],
synchronize: true,
}),
useFactory: typeormConfig,
inject: [ConfigService],
}),
StudyLogsModule,
Expand Down
2 changes: 1 addition & 1 deletion BE/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { UpdateUserDto } from 'src/users/dto/update-user.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { ConfigService } from '@nestjs/config';
import { ENV } from 'src/common/const/env-keys.const';
import { getImageUrl } from 'src/common/utils';
import { getImageUrl } from 'src/common/utils/utils';

@ApiTags('로그인 페이지')
@Controller('auth')
Expand Down
8 changes: 3 additions & 5 deletions BE/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import { UsersModule } from 'src/users/users.module';
import { JwtModule } from '@nestjs/jwt';
import { GoogleStrategy } from './google.strategy';
import { MulterModule } from '@nestjs/platform-express';
import { multerConfig } from 'src/common/multer.config';
import { multerConfig } from 'src/common/config/multer.config';
import { jwtConfig } from 'src/common/config/jwt.config';

@Module({
imports: [
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => ({
secret: configService.get('JWT_SECRET'),
signOptions: { expiresIn: configService.get('JWT_EXPIRES_IN') },
}),
useFactory: jwtConfig,
}),
MulterModule.registerAsync({
imports: [ConfigModule],
Expand Down
7 changes: 7 additions & 0 deletions BE/src/common/config/jwt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ConfigService } from '@nestjs/config';
import { ENV } from '../const/env-keys.const';

export const jwtConfig = (configService: ConfigService) => ({
secret: configService.get<string>(ENV.JWT_SECRET),
signOptions: { expiresIn: configService.get<number>(ENV.JWT_EXPIRES_IN) },
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import winston from 'winston';
import winston from 'winston';

const customFormat = winston.format.printf(
({ level, message, timestamp, context }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import path from 'path';
import { BadRequestException } from '@nestjs/common';
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
import multer from 'multer';
import multer from 'multer';

export const multerConfig = (): MulterOptions => {
const storage = multer.memoryStorage();
Expand All @@ -15,7 +15,6 @@ export const multerConfig = (): MulterOptions => {
}
return callback(null, true);
};

const limits = { fileSize: 1024 * 1024 * 10 }; //10MB

return {
Expand Down
7 changes: 7 additions & 0 deletions BE/src/common/config/static.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import path from 'path';

export const staticConfig = {
rootPath: path.join(__dirname, '../../../../..', 'apps'),
serveRoot: '/',
renderPath: '/',
};
8 changes: 8 additions & 0 deletions BE/src/common/config/swagger.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DocumentBuilder } from '@nestjs/swagger';

export const swaggerConfig = new DocumentBuilder()
.setTitle('StudyLog API')
.setDescription('StudyLog 애플리케이션 API 문서')
.setVersion('2.0')
.addBearerAuth()
.build();
19 changes: 19 additions & 0 deletions BE/src/common/config/typeorm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ConfigService } from '@nestjs/config';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { Categories } from 'src/categories/categories.entity';
import { Mates } from 'src/mates/mates.entity';
import { StudyLogs } from 'src/study-logs/study-logs.entity';
import { UsersModel } from 'src/users/entity/users.entity';
import { ENV } from '../const/env-keys.const';

export const typeormConfig = (config: ConfigService): TypeOrmModuleOptions => ({
type: 'mysql',
host: config.get<string>(ENV.DATABASE_HOST),
port: config.get<number>(ENV.DATABASE_PORT),
username: config.get<string>(ENV.DATABASE_USERNAME),
password: config.get<string>(ENV.DATABASE_PASSWORD),
database: config.get<string>(ENV.DATABASE_NAME),
entities: [StudyLogs, Categories, UsersModel, Mates],
timezone: 'Z',
synchronize: true,
});
13 changes: 13 additions & 0 deletions BE/src/common/const/env-keys.const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,17 @@ export enum ENV {
IMAGE_REGION = 'IMAGE_REGION',
IMAGE_BUCKET = 'IMAGE_BUCKET',
CDN_ENDPOINT = 'CDN_ENDPOINT',
JWT_SECRET = 'JWT_SECRET',
JWT_EXPIRES_IN = 'JWT_EXPIRES_IN',
DATABASE_HOST = 'DATABASE_HOST',
DATABASE_PORT = 'DATABASE_PORT',
DATABASE_USERNAME = 'DATABASE_USERNAME',
DATABASE_PASSWORD = 'DATABASE_PASSWORD',
DATABASE_NAME = 'DATABASE_NAME',
SSL = 'SSL',
HTTPS_KEY_PATH = 'HTTPS_KEY_PATH',
HTTPS_CERT_PATH = 'HTTPS_CERT_PATH',
PORT = 'PORT',
GREENEYE_URL = 'GREENEYE_URL',
GREENEYE_SECRET = 'GREENEYE_SECRET',
}
2 changes: 1 addition & 1 deletion BE/src/common/exception-filter/http-exception-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
HttpException,
Logger,
} from '@nestjs/common';
import { LoggingInterceptor } from '../logging.interceptor';
import { LoggingInterceptor } from '../interceptor/logging.interceptor';

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion BE/src/common/utils.ts → BE/src/common/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
import path from 'path';

export function transformDate(date: Date): string {
const year = date.getFullYear();
Expand Down
24 changes: 10 additions & 14 deletions BE/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { SwaggerModule } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { readFileSync } from 'fs';
import { WinstonModule } from 'nest-winston';
import { loggerConfig } from './common/logging.config';
import { LoggingInterceptor } from './common/logging.interceptor';
import { loggerConfig } from './common/config/logging.config';
import { LoggingInterceptor } from './common/interceptor/logging.interceptor';
import { HttpExceptionFilter } from './common/exception-filter/http-exception-filter';
import { swaggerConfig } from './common/config/swagger.config';
import { ENV } from './common/const/env-keys.const';

async function bootstrap() {
const configService = new ConfigService();

const ssl = configService.get<string>('SSL');
const ssl = configService.get<string>(ENV.SSL);
let httpsOptions = null;
if (ssl == 'true') {
httpsOptions = {
key: readFileSync(configService.get<string>('HTTPS_KEY_PATH')),
cert: readFileSync(configService.get<string>('HTTPS_CERT_PATH')),
key: readFileSync(configService.get<string>(ENV.HTTPS_KEY_PATH)),
cert: readFileSync(configService.get<string>(ENV.HTTPS_CERT_PATH)),
};
}

const app = await NestFactory.create(AppModule, {
httpsOptions,
logger: WinstonModule.createLogger(loggerConfig),
});
const config = new DocumentBuilder()
.setTitle('StudyLog API')
.setDescription('StudyLog 애플리케이션 API 문서')
.setVersion('2.0')
.addBearerAuth()
.build();

const document = SwaggerModule.createDocument(app, config);
const document = SwaggerModule.createDocument(app, swaggerConfig);
SwaggerModule.setup('api', app, document);

app.useGlobalPipes(new ValidationPipe());
app.useGlobalInterceptors(new LoggingInterceptor());
app.useGlobalFilters(new HttpExceptionFilter());

await app.listen(configService.get<number>('PORT') || 3000);
await app.listen(configService.get<number>(ENV.PORT) || 3000);
}
bootstrap();
2 changes: 1 addition & 1 deletion BE/src/mates/mates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Repository } from 'typeorm';
import { MatesDto } from './dto/response/mates.dto';
import { UsersModel } from 'src/users/entity/users.entity';
import { RedisService } from 'src/common/redis.service';
import { getImageUrl } from 'src/common/utils';
import { getImageUrl } from 'src/common/utils/utils';
import { ConfigService } from '@nestjs/config';
import { ENV } from 'src/common/const/env-keys.const';
import { StudyLogsService } from 'src/study-logs/study-logs.service';
Expand Down
3 changes: 2 additions & 1 deletion BE/src/study-logs/study-logs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StudyLogsCreateDto } from './dto/request/create-study-logs.dto';
import { UsersModel } from 'src/users/entity/users.entity';
import { Categories } from 'src/categories/categories.entity';
import { StudyLogsDto } from './dto/response/study-logs.dto';
import { transformDate } from 'src/common/utils';
import { transformDate } from 'src/common/utils/utils';
import { RedisService } from 'src/common/redis.service';
import moment from 'moment';

Expand Down Expand Up @@ -49,6 +49,7 @@ export class StudyLogsService {
}

calculateStartDay(created_at: Date, learning_time: number): Date {
console.log(created_at.toLocaleString());
const STANDARD = 0;
const standardMS = STANDARD * 60 * 60 * 1000;
const millisecond =
Expand Down
6 changes: 3 additions & 3 deletions BE/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { GreenEyeResponse } from './interface/greeneye.interface';
import { S3Service } from 'src/common/s3.service';
import { ENV } from 'src/common/const/env-keys.const';
import { UpdateUserDto } from './dto/update-user.dto';
import { getImageUrl } from 'src/common/utils';
import { getImageUrl } from 'src/common/utils/utils';

@Injectable()
export class UsersService {
Expand Down Expand Up @@ -115,8 +115,8 @@ export class UsersService {
async requestClovaGreenEye(
image: Express.Multer.File,
): Promise<GreenEyeResponse> {
const APIURL = this.config.get<string>('GREENEYE_URL');
const CLIENT_SECRET = this.config.get<string>('GREENEYE_SECRET');
const APIURL = this.config.get<string>(ENV.GREENEYE_URL);
const CLIENT_SECRET = this.config.get<string>(ENV.GREENEYE_SECRET);
const UUID = v4();
try {
const response = await fetch(APIURL, {
Expand Down
2 changes: 1 addition & 1 deletion BE/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';
import request from 'supertest';
import { AppModule } from '../src/app.module';

describe('AppController (e2e)', () => {
Expand Down

0 comments on commit 98d99a9

Please sign in to comment.