From 90a7cf2db9ed66455047dbca0f3593e0c8787244 Mon Sep 17 00:00:00 2001 From: unanchoi Date: Thu, 9 Nov 2023 19:30:11 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[CONFIG]=20.gitmessage.txt=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmessage.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .gitmessage.txt diff --git a/.gitmessage.txt b/.gitmessage.txt new file mode 100644 index 0000000..3836f9c --- /dev/null +++ b/.gitmessage.txt @@ -0,0 +1,11 @@ + +################ +# [FEAT] : 기능 추가 및 구현 +# [FIX] : 에러 발생 시, 버그 +# [REFACTOR] : 기존 코드에서 기능 추가 및 개선 +# [HOTFIX] : 급한 수정 , 배포 후 수정 +# [TEST] : Test code +# [RENAME] : 변수명, 메소드명, 클래스명 변경 +# [CHORE] : 사소한 코드 수정, 디렉토리 구조 변경, 변수명 변경, 불필요한 코드 삭제(import) +# [DOCS] : 주석 및 Readme +# [CONFIG] : 의존성 추가, 설정 파일 추가 \ No newline at end of file From f7d16d6c264285e5a6017fedf4c9da885686e9d1 Mon Sep 17 00:00:00 2001 From: unanchoi Date: Fri, 10 Nov 2023 00:27:38 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[CONFIG]=20slack=20api=20=EC=9D=98=EC=A1=B4?= =?UTF-8?q?=EC=84=B1=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=EC=84=A4=EC=A0=95?= =?UTF-8?q?=20=EA=B0=92=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 +++ .../katchupserver/external/slack/SlackService.java | 8 ++++++++ src/main/resources/application-dev.yml | 3 +++ src/main/resources/application-local.yml | 6 +++++- 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/main/java/site/katchup/katchupserver/external/slack/SlackService.java diff --git a/build.gradle b/build.gradle index 7789902..f3784ee 100644 --- a/build.gradle +++ b/build.gradle @@ -46,6 +46,9 @@ dependencies { annotationProcessor "jakarta.annotation:jakarta.annotation-api" annotationProcessor "jakarta.persistence:jakarta.persistence-api" + // slack + implementation("com.slack.api:slack-api-client:1.31.0") + // AWS sdk implementation("software.amazon.awssdk:bom:2.21.0") implementation("software.amazon.awssdk:s3:2.21.0") diff --git a/src/main/java/site/katchup/katchupserver/external/slack/SlackService.java b/src/main/java/site/katchup/katchupserver/external/slack/SlackService.java new file mode 100644 index 0000000..af2835a --- /dev/null +++ b/src/main/java/site/katchup/katchupserver/external/slack/SlackService.java @@ -0,0 +1,8 @@ +package site.katchup.katchupserver.external.slack; + + +import org.springframework.stereotype.Component; + +@Component +public class SlackService { +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index cdd566e..4fa9ac6 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -52,3 +52,6 @@ springdoc: path: /api-docs/json groups: enabled: true +slack: + bot-token: ${SLACK_BOT_TOKEN} + channel: ${SLACK_ALERT_CHANNEL} \ No newline at end of file diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 3993218..aa3c48f 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -34,4 +34,8 @@ aws-property: aws-region: ${AWS_REGION} s3-bucket-name: ${AWS_BUCKET_NAME} access-key: ${AWS_ACCESS_KEY} - secret-key: ${AWS_SECRET_KEY} \ No newline at end of file + secret-key: ${AWS_SECRET_KEY} + +slack: + bot-token: ${SLACK_BOT_TOKEN} + channel: ${SLACK_ALERT_CHANNEL} \ No newline at end of file From 3a6a9d73bd55c3a378e94bb2f465079a3b3bceae Mon Sep 17 00:00:00 2001 From: unanchoi Date: Fri, 10 Nov 2023 00:28:25 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[FEAT]=20slack=20=EC=95=8C=EB=A6=BC=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../advice/ControllerExceptionAdvice.java | 23 ++++++++++++ .../external/slack/SlackService.java | 35 +++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/main/java/site/katchup/katchupserver/common/advice/ControllerExceptionAdvice.java b/src/main/java/site/katchup/katchupserver/common/advice/ControllerExceptionAdvice.java index 85d6f91..d329e0d 100644 --- a/src/main/java/site/katchup/katchupserver/common/advice/ControllerExceptionAdvice.java +++ b/src/main/java/site/katchup/katchupserver/common/advice/ControllerExceptionAdvice.java @@ -1,5 +1,6 @@ package site.katchup.katchupserver.common.advice; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.MethodArgumentNotValidException; @@ -8,6 +9,9 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; import site.katchup.katchupserver.common.dto.ApiResponseDto; import site.katchup.katchupserver.common.exception.BaseException; +import site.katchup.katchupserver.common.exception.InternalServerException; +import site.katchup.katchupserver.common.response.ErrorCode; +import site.katchup.katchupserver.external.slack.SlackService; import static site.katchup.katchupserver.common.response.ErrorCode.VALIDATION_REQUEST_MISSING_EXCEPTION; @@ -15,12 +19,31 @@ @RestControllerAdvice public class ControllerExceptionAdvice { + private final String channel; + + public ControllerExceptionAdvice(@Value("${slack.channel}") final String channel, SlackService slackService) { + this.channel = channel; + this.slackService = slackService; + } + + private final SlackService slackService; + @ExceptionHandler(BaseException.class) public ResponseEntity handleGlobalException(BaseException ex) { + if (ex.getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR) { + slackService.sendMessage(channel, ex.getMessage()); + } return ResponseEntity.status(ex.getStatusCode()) .body(ApiResponseDto.error(ex.getCode())); } + @ExceptionHandler(InternalServerException.class) + public ResponseEntity handleInternalServerException(InternalServerException ex) { + slackService.sendMessage(channel, ex.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(ApiResponseDto.error(ErrorCode.INTERNAL_SERVER_ERROR.getCode())); + } + @ExceptionHandler(MissingServletRequestParameterException.class) public ResponseEntity handleMissingParameter(MissingServletRequestParameterException ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) diff --git a/src/main/java/site/katchup/katchupserver/external/slack/SlackService.java b/src/main/java/site/katchup/katchupserver/external/slack/SlackService.java index af2835a..d0b9503 100644 --- a/src/main/java/site/katchup/katchupserver/external/slack/SlackService.java +++ b/src/main/java/site/katchup/katchupserver/external/slack/SlackService.java @@ -1,8 +1,39 @@ package site.katchup.katchupserver.external.slack; -import org.springframework.stereotype.Component; +import com.slack.api.Slack; +import com.slack.api.methods.SlackApiException; +import com.slack.api.methods.response.chat.ChatPostMessageResponse; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Service; -@Component +import java.io.IOException; +import java.util.Arrays; + +@Service +@RequiredArgsConstructor public class SlackService { + + @Value("${slack.bot-token}") + private String SLACK_TOKEN; + + private final Environment env; + + public void sendMessage(String channel, String text) { + try { + Slack slack = Slack.getInstance(); + ChatPostMessageResponse response = slack.methods(SLACK_TOKEN).chatPostMessage(req -> req + .channel(channel) + .text("[" + getProfiles() + "]" + text)); + System.out.println(response); + } catch (IOException | SlackApiException e) { + throw new RuntimeException(e); + } + } + + private String getProfiles() { + return Arrays.stream(env.getActiveProfiles()).findFirst().orElse(""); + } }