From b0ca30cfe5d69c8166153e7c5201b4aac2d03908 Mon Sep 17 00:00:00 2001 From: peageon Date: Tue, 5 Dec 2023 18:57:00 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20Main.ts=20=EB=A1=9C=EA=B7=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 몇번 포트에서 작동하는지 로그 --- back/src/main.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/back/src/main.ts b/back/src/main.ts index 8016ed2..081aa2d 100644 --- a/back/src/main.ts +++ b/back/src/main.ts @@ -2,8 +2,10 @@ import { NestFactory } from '@nestjs/core'; import { setupSwagger } from 'src/common/util/swagger'; import { AppModule } from './app.module'; import { ValidationPipe } from '@nestjs/common/pipes'; +import { Logger } from '@nestjs/common'; async function bootstrap() { + const logger = new Logger('Main'); const app = await NestFactory.create(AppModule); app.setGlobalPrefix('api'); app.useGlobalPipes( @@ -15,6 +17,7 @@ async function bootstrap() { }) ); setupSwagger(app); - await app.listen(3000); + logger.log(`Server is running on port ${process.env.PORT}`); + await app.listen(process.env.PORT); } bootstrap(); From e0476ba947de4b41d9895f962ff7544a9f443d4d Mon Sep 17 00:00:00 2001 From: peageon Date: Tue, 5 Dec 2023 19:43:08 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=EC=BB=A8=ED=8A=B8=EB=A1=A4?= =?UTF-8?q?=EB=9F=AC=20=EB=A1=9C=EA=B7=B8=20=EC=9D=B8=ED=84=B0=EC=85=89?= =?UTF-8?q?=ED=84=B0=EC=83=9D=EC=84=B1=ED=9B=84=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/interceptors/log.interceptor.ts | 28 +++++++++++++++++++ back/src/modules/auth/auth.controller.ts | 17 ++++++++++- .../src/modules/message/message.controller.ts | 9 +++++- .../modules/snowball/snowball.controller.ts | 5 ++++ back/src/modules/user/user.controller.ts | 5 +++- 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 back/src/common/interceptors/log.interceptor.ts diff --git a/back/src/common/interceptors/log.interceptor.ts b/back/src/common/interceptors/log.interceptor.ts new file mode 100644 index 0000000..aec9328 --- /dev/null +++ b/back/src/common/interceptors/log.interceptor.ts @@ -0,0 +1,28 @@ +import { + Injectable, + NestInterceptor, + ExecutionContext, + CallHandler, + Logger +} from '@nestjs/common'; +import { Observable } from 'rxjs'; +import { tap } from 'rxjs/operators'; + +@Injectable() +export class ControllerLoggerInterceptor implements NestInterceptor { + intercept(context: ExecutionContext, next: CallHandler): Observable { + const controllerName = context.getClass().name; + const logger = new Logger(controllerName); + const request = context.switchToHttp().getRequest(); + const method = request.method; + const url = request.url; + const start = Date.now(); + logger.verbose(`>>> IN: ${method} ${url}`); + return next.handle().pipe( + tap(() => { + const end = Date.now(); + logger.verbose(`<<< OUT: ${method} ${url} ${end - start}ms`); + }) + ); + } +} diff --git a/back/src/modules/auth/auth.controller.ts b/back/src/modules/auth/auth.controller.ts index 990e4d7..5c74d63 100644 --- a/back/src/modules/auth/auth.controller.ts +++ b/back/src/modules/auth/auth.controller.ts @@ -1,14 +1,23 @@ -import { Controller, Get, Req, Res, UseGuards } from '@nestjs/common'; +import { + Controller, + Get, + Req, + Res, + UseGuards, + UseInterceptors +} from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger'; import { AuthService } from './auth.service'; import { payload } from './auth.service'; +import { ControllerLoggerInterceptor } from 'src/common/interceptors/log.interceptor'; @ApiTags('Oauth API') @Controller('auth') export class AuthController { constructor(private readonly authService: AuthService) {} @Get('google') + @UseInterceptors(ControllerLoggerInterceptor) @ApiOperation({ summary: 'Google 로그인 요청 API', description: 'Google OAuth API에 로그인 요청을 보냅니다.' @@ -25,6 +34,7 @@ export class AuthController { async googleLogin(): Promise {} @Get('google/redirect') + @UseInterceptors(ControllerLoggerInterceptor) @UseGuards(AuthGuard('google')) @ApiResponse({ status: 200, @@ -45,6 +55,7 @@ export class AuthController { } @Get('naver') + @UseInterceptors(ControllerLoggerInterceptor) @ApiOperation({ summary: 'Naver 로그인 요청 API', description: 'Naver OAuth API에 로그인 요청을 보냅니다.' @@ -61,6 +72,7 @@ export class AuthController { async naverLogin(): Promise {} @Get('naver/redirect') + @UseInterceptors(ControllerLoggerInterceptor) @UseGuards(AuthGuard('naver')) @ApiResponse({ status: 200, @@ -81,6 +93,7 @@ export class AuthController { } @Get('kakao') + @UseInterceptors(ControllerLoggerInterceptor) @ApiOperation({ summary: 'Kakao 로그인 요청 API', description: 'Kakao OAuth API에 로그인 요청을 보냅니다.' @@ -97,6 +110,7 @@ export class AuthController { async kakaoLogin(): Promise {} @Get('kakao/redirect') + @UseInterceptors(ControllerLoggerInterceptor) @UseGuards(AuthGuard('kakao')) @ApiResponse({ status: 200, @@ -118,6 +132,7 @@ export class AuthController { } @Get('logout') + @UseInterceptors(ControllerLoggerInterceptor) @ApiResponse({ status: 200, description: '로그아웃 성공' diff --git a/back/src/modules/message/message.controller.ts b/back/src/modules/message/message.controller.ts index a077d25..e4fe664 100644 --- a/back/src/modules/message/message.controller.ts +++ b/back/src/modules/message/message.controller.ts @@ -8,7 +8,8 @@ import { HttpCode, UseGuards, Put, - Req + Req, + UseInterceptors } from '@nestjs/common'; import { MessageService } from './message.service'; import { ReqCreateMessageDto } from './dto/request/req-create-message.dto'; @@ -31,6 +32,7 @@ import { JWTGuard } from 'src/common/guards/jwt.guard'; import { UpdateMessageLocationDto } from './dto/update-message-location.dto'; import { JWTRequest } from 'src/common/interface/request.interface'; import { ClovaService } from './clova.service'; +import { ControllerLoggerInterceptor } from 'src/common/interceptors/log.interceptor'; @ApiTags('Message API') @Controller('message') export class MessageController { @@ -40,6 +42,7 @@ export class MessageController { ) {} @Post('/:snowball_id') + @UseInterceptors(ControllerLoggerInterceptor) @HttpCode(201) @ApiOperation({ summary: '메세지 생성 API', @@ -71,6 +74,7 @@ export class MessageController { } @UseGuards(JWTGuard) + @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Delete(':message_id') @HttpCode(204) @@ -96,6 +100,7 @@ export class MessageController { } @UseGuards(JWTGuard) + @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Get('/') @HttpCode(200) @@ -118,6 +123,7 @@ export class MessageController { } @UseGuards(JWTGuard) + @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Put('/:message_id/open') @HttpCode(200) @@ -148,6 +154,7 @@ export class MessageController { } @UseGuards(JWTGuard) + @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Put('/:message_id/location') @ApiOperation({ diff --git a/back/src/modules/snowball/snowball.controller.ts b/back/src/modules/snowball/snowball.controller.ts index 2b1deed..0aa5b12 100644 --- a/back/src/modules/snowball/snowball.controller.ts +++ b/back/src/modules/snowball/snowball.controller.ts @@ -28,6 +28,7 @@ import { JWTGuard } from 'src/common/guards/jwt.guard'; import { UpdateMainDecoDto } from './dto/update-main-decoration.dto'; import { JWTRequest } from '../../common/interface/request.interface'; import { hasJWTInterceptor } from '../../common/interceptors/hasJwt.interceptor'; +import { ControllerLoggerInterceptor } from '../../common/interceptors/log.interceptor'; @ApiTags('Snowball API') @Controller('snowball') @@ -35,6 +36,7 @@ export class SnowballController { constructor(private readonly snowballService: SnowballService) {} @UseGuards(JWTGuard) + @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Post() @HttpCode(201) @@ -59,6 +61,7 @@ export class SnowballController { } @UseGuards(JWTGuard) + @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Put('/:snowball_id') @HttpCode(200) @@ -86,6 +89,7 @@ export class SnowballController { } @Get('/:snowball_id') + @UseInterceptors(ControllerLoggerInterceptor) @UseInterceptors(hasJWTInterceptor) @HttpCode(200) @ApiResponse({ @@ -110,6 +114,7 @@ export class SnowballController { } @UseGuards(JWTGuard) + @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Put('/:snowball_id/decoration') @ApiResponse({ diff --git a/back/src/modules/user/user.controller.ts b/back/src/modules/user/user.controller.ts index e7e85f7..8cf3405 100644 --- a/back/src/modules/user/user.controller.ts +++ b/back/src/modules/user/user.controller.ts @@ -21,6 +21,7 @@ import { ResInfoDto } from './dto/response/res-info.dto'; import { NicknameDto } from './dto/nickname.dto'; import { JWTRequest } from 'src/common/interface/request.interface'; import { TransactionInterceptor } from 'src/common/interceptors/transaction.interceptor'; +import { ControllerLoggerInterceptor } from 'src/common/interceptors/log.interceptor'; @ApiTags('User API') @Controller('user') @@ -28,6 +29,7 @@ export class UserController { constructor(private readonly userService: UserService) {} @UseGuards(JWTGuard) + @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Get() @ApiOperation({ @@ -44,6 +46,7 @@ export class UserController { } @Get('/:auth_id') + @UseInterceptors(ControllerLoggerInterceptor) @ApiOperation({ summary: '방문자 유저 조회 API', description: '방문자가 접속한 유저의 정보를 반환합니다' @@ -62,7 +65,7 @@ export class UserController { } @UseGuards(JWTGuard) - @UseInterceptors(TransactionInterceptor) + @UseInterceptors(ControllerLoggerInterceptor, TransactionInterceptor) @ApiCookieAuth('access_token') @Put('/nickname') @ApiBody({ type: NicknameDto }) From ae9367e692323c0559e8fa558f4612875252ead2 Mon Sep 17 00:00:00 2001 From: peageon Date: Tue, 5 Dec 2023 20:11:33 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=EC=95=B1=EB=AA=A8=EB=93=88=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EC=95=B1=20=EC=9D=B8=ED=84=B0=EC=85=89=ED=84=B0?= =?UTF-8?q?=EB=A1=9C=20=EA=B8=80=EB=A1=9C=EB=B2=8C=EB=A1=9C=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=ED=95=B4=EC=A3=BC=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/src/app.module.ts | 7 ++++++- back/src/common/interceptors/log.interceptor.ts | 2 +- back/src/modules/auth/auth.controller.ts | 17 +---------------- back/src/modules/message/message.controller.ts | 9 +-------- .../src/modules/snowball/snowball.controller.ts | 5 ----- back/src/modules/user/user.controller.ts | 5 +---- 6 files changed, 10 insertions(+), 35 deletions(-) diff --git a/back/src/app.module.ts b/back/src/app.module.ts index ece5421..9673795 100644 --- a/back/src/app.module.ts +++ b/back/src/app.module.ts @@ -6,6 +6,8 @@ import { AuthModule } from './modules/auth/auth.module'; import typeOrmConfig from './config/ormconfig'; import { JwtModule } from '@nestjs/jwt'; import { UserModule } from './modules/user/user.module'; +import { APP_INTERCEPTOR } from '@nestjs/core'; +import { ControllerLoggerInterceptor } from './common/interceptors/log.interceptor'; @Module({ imports: [ @@ -17,6 +19,9 @@ import { UserModule } from './modules/user/user.module'; }) ], controllers: [AppController], - providers: [AppService] + providers: [ + AppService, + { provide: APP_INTERCEPTOR, useClass: ControllerLoggerInterceptor } + ] }) export class AppModule {} diff --git a/back/src/common/interceptors/log.interceptor.ts b/back/src/common/interceptors/log.interceptor.ts index aec9328..6633470 100644 --- a/back/src/common/interceptors/log.interceptor.ts +++ b/back/src/common/interceptors/log.interceptor.ts @@ -15,7 +15,7 @@ export class ControllerLoggerInterceptor implements NestInterceptor { const logger = new Logger(controllerName); const request = context.switchToHttp().getRequest(); const method = request.method; - const url = request.url; + const url = request.originalUrl; const start = Date.now(); logger.verbose(`>>> IN: ${method} ${url}`); return next.handle().pipe( diff --git a/back/src/modules/auth/auth.controller.ts b/back/src/modules/auth/auth.controller.ts index 5c74d63..990e4d7 100644 --- a/back/src/modules/auth/auth.controller.ts +++ b/back/src/modules/auth/auth.controller.ts @@ -1,23 +1,14 @@ -import { - Controller, - Get, - Req, - Res, - UseGuards, - UseInterceptors -} from '@nestjs/common'; +import { Controller, Get, Req, Res, UseGuards } from '@nestjs/common'; import { AuthGuard } from '@nestjs/passport'; import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger'; import { AuthService } from './auth.service'; import { payload } from './auth.service'; -import { ControllerLoggerInterceptor } from 'src/common/interceptors/log.interceptor'; @ApiTags('Oauth API') @Controller('auth') export class AuthController { constructor(private readonly authService: AuthService) {} @Get('google') - @UseInterceptors(ControllerLoggerInterceptor) @ApiOperation({ summary: 'Google 로그인 요청 API', description: 'Google OAuth API에 로그인 요청을 보냅니다.' @@ -34,7 +25,6 @@ export class AuthController { async googleLogin(): Promise {} @Get('google/redirect') - @UseInterceptors(ControllerLoggerInterceptor) @UseGuards(AuthGuard('google')) @ApiResponse({ status: 200, @@ -55,7 +45,6 @@ export class AuthController { } @Get('naver') - @UseInterceptors(ControllerLoggerInterceptor) @ApiOperation({ summary: 'Naver 로그인 요청 API', description: 'Naver OAuth API에 로그인 요청을 보냅니다.' @@ -72,7 +61,6 @@ export class AuthController { async naverLogin(): Promise {} @Get('naver/redirect') - @UseInterceptors(ControllerLoggerInterceptor) @UseGuards(AuthGuard('naver')) @ApiResponse({ status: 200, @@ -93,7 +81,6 @@ export class AuthController { } @Get('kakao') - @UseInterceptors(ControllerLoggerInterceptor) @ApiOperation({ summary: 'Kakao 로그인 요청 API', description: 'Kakao OAuth API에 로그인 요청을 보냅니다.' @@ -110,7 +97,6 @@ export class AuthController { async kakaoLogin(): Promise {} @Get('kakao/redirect') - @UseInterceptors(ControllerLoggerInterceptor) @UseGuards(AuthGuard('kakao')) @ApiResponse({ status: 200, @@ -132,7 +118,6 @@ export class AuthController { } @Get('logout') - @UseInterceptors(ControllerLoggerInterceptor) @ApiResponse({ status: 200, description: '로그아웃 성공' diff --git a/back/src/modules/message/message.controller.ts b/back/src/modules/message/message.controller.ts index e4fe664..a077d25 100644 --- a/back/src/modules/message/message.controller.ts +++ b/back/src/modules/message/message.controller.ts @@ -8,8 +8,7 @@ import { HttpCode, UseGuards, Put, - Req, - UseInterceptors + Req } from '@nestjs/common'; import { MessageService } from './message.service'; import { ReqCreateMessageDto } from './dto/request/req-create-message.dto'; @@ -32,7 +31,6 @@ import { JWTGuard } from 'src/common/guards/jwt.guard'; import { UpdateMessageLocationDto } from './dto/update-message-location.dto'; import { JWTRequest } from 'src/common/interface/request.interface'; import { ClovaService } from './clova.service'; -import { ControllerLoggerInterceptor } from 'src/common/interceptors/log.interceptor'; @ApiTags('Message API') @Controller('message') export class MessageController { @@ -42,7 +40,6 @@ export class MessageController { ) {} @Post('/:snowball_id') - @UseInterceptors(ControllerLoggerInterceptor) @HttpCode(201) @ApiOperation({ summary: '메세지 생성 API', @@ -74,7 +71,6 @@ export class MessageController { } @UseGuards(JWTGuard) - @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Delete(':message_id') @HttpCode(204) @@ -100,7 +96,6 @@ export class MessageController { } @UseGuards(JWTGuard) - @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Get('/') @HttpCode(200) @@ -123,7 +118,6 @@ export class MessageController { } @UseGuards(JWTGuard) - @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Put('/:message_id/open') @HttpCode(200) @@ -154,7 +148,6 @@ export class MessageController { } @UseGuards(JWTGuard) - @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Put('/:message_id/location') @ApiOperation({ diff --git a/back/src/modules/snowball/snowball.controller.ts b/back/src/modules/snowball/snowball.controller.ts index 0aa5b12..2b1deed 100644 --- a/back/src/modules/snowball/snowball.controller.ts +++ b/back/src/modules/snowball/snowball.controller.ts @@ -28,7 +28,6 @@ import { JWTGuard } from 'src/common/guards/jwt.guard'; import { UpdateMainDecoDto } from './dto/update-main-decoration.dto'; import { JWTRequest } from '../../common/interface/request.interface'; import { hasJWTInterceptor } from '../../common/interceptors/hasJwt.interceptor'; -import { ControllerLoggerInterceptor } from '../../common/interceptors/log.interceptor'; @ApiTags('Snowball API') @Controller('snowball') @@ -36,7 +35,6 @@ export class SnowballController { constructor(private readonly snowballService: SnowballService) {} @UseGuards(JWTGuard) - @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Post() @HttpCode(201) @@ -61,7 +59,6 @@ export class SnowballController { } @UseGuards(JWTGuard) - @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Put('/:snowball_id') @HttpCode(200) @@ -89,7 +86,6 @@ export class SnowballController { } @Get('/:snowball_id') - @UseInterceptors(ControllerLoggerInterceptor) @UseInterceptors(hasJWTInterceptor) @HttpCode(200) @ApiResponse({ @@ -114,7 +110,6 @@ export class SnowballController { } @UseGuards(JWTGuard) - @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Put('/:snowball_id/decoration') @ApiResponse({ diff --git a/back/src/modules/user/user.controller.ts b/back/src/modules/user/user.controller.ts index 8cf3405..e7e85f7 100644 --- a/back/src/modules/user/user.controller.ts +++ b/back/src/modules/user/user.controller.ts @@ -21,7 +21,6 @@ import { ResInfoDto } from './dto/response/res-info.dto'; import { NicknameDto } from './dto/nickname.dto'; import { JWTRequest } from 'src/common/interface/request.interface'; import { TransactionInterceptor } from 'src/common/interceptors/transaction.interceptor'; -import { ControllerLoggerInterceptor } from 'src/common/interceptors/log.interceptor'; @ApiTags('User API') @Controller('user') @@ -29,7 +28,6 @@ export class UserController { constructor(private readonly userService: UserService) {} @UseGuards(JWTGuard) - @UseInterceptors(ControllerLoggerInterceptor) @ApiCookieAuth('access_token') @Get() @ApiOperation({ @@ -46,7 +44,6 @@ export class UserController { } @Get('/:auth_id') - @UseInterceptors(ControllerLoggerInterceptor) @ApiOperation({ summary: '방문자 유저 조회 API', description: '방문자가 접속한 유저의 정보를 반환합니다' @@ -65,7 +62,7 @@ export class UserController { } @UseGuards(JWTGuard) - @UseInterceptors(ControllerLoggerInterceptor, TransactionInterceptor) + @UseInterceptors(TransactionInterceptor) @ApiCookieAuth('access_token') @Put('/nickname') @ApiBody({ type: NicknameDto })