From ee8a00b92a65eff80e9f6dad5c2bf5714656cfc7 Mon Sep 17 00:00:00 2001 From: Ssuamje Date: Fri, 15 Sep 2023 12:06:36 +0900 Subject: [PATCH 1/9] [BE] branch create From 4a06247fb6b7447b6066f4f5c79b328f3030a068 Mon Sep 17 00:00:00 2001 From: Ssuamje Date: Fri, 15 Sep 2023 13:19:47 +0900 Subject: [PATCH 2/9] =?UTF-8?q?[BE]=20FEAT:=20Discord=20Webhook=EC=97=90?= =?UTF-8?q?=20=EB=A9=94=EC=8B=9C=EC=A7=80=EB=A5=BC=20=EC=A0=84=EB=8B=AC?= =?UTF-8?q?=ED=95=98=EB=8A=94=20messenger=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cabinet/log/DiscordWebHookMessenger.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 backend/src/main/java/org/ftclub/cabinet/log/DiscordWebHookMessenger.java diff --git a/backend/src/main/java/org/ftclub/cabinet/log/DiscordWebHookMessenger.java b/backend/src/main/java/org/ftclub/cabinet/log/DiscordWebHookMessenger.java new file mode 100644 index 000000000..ab2c444c2 --- /dev/null +++ b/backend/src/main/java/org/ftclub/cabinet/log/DiscordWebHookMessenger.java @@ -0,0 +1,23 @@ +package org.ftclub.cabinet.log; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.client.WebClient; + +@Component +public class DiscordWebHookMessenger { + private final String discordWebHookUrl; + + DiscordWebHookMessenger(@Value("${webhook.discord-admin}") String discordWebHookUrl) { + this.discordWebHookUrl = discordWebHookUrl; + } + + public void sendMessage(String message) { + WebClient.create().post() + .uri(discordWebHookUrl) + .bodyValue(message) + .retrieve() + .bodyToMono(String.class) + .block(); + } +} From a9c2d1c72f7c18eee26fea38872d4fb3d035f388 Mon Sep 17 00:00:00 2001 From: Ssuamje Date: Fri, 15 Sep 2023 13:30:31 +0900 Subject: [PATCH 3/9] =?UTF-8?q?[BE]=20FEAT:=20Webhook=EC=97=90=20sendMessa?= =?UTF-8?q?ge=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cabinet/log/DiscordWebHookMessenger.java | 9 ++++++++- .../cabinet/log/WebHookTestController.java | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/java/org/ftclub/cabinet/log/WebHookTestController.java diff --git a/backend/src/main/java/org/ftclub/cabinet/log/DiscordWebHookMessenger.java b/backend/src/main/java/org/ftclub/cabinet/log/DiscordWebHookMessenger.java index ab2c444c2..91d570dde 100644 --- a/backend/src/main/java/org/ftclub/cabinet/log/DiscordWebHookMessenger.java +++ b/backend/src/main/java/org/ftclub/cabinet/log/DiscordWebHookMessenger.java @@ -4,8 +4,12 @@ import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; +import java.util.HashMap; +import java.util.Map; + @Component public class DiscordWebHookMessenger { + private final static String DISCORD_WEBHOOK_MESSAGE_KEY = "content"; private final String discordWebHookUrl; DiscordWebHookMessenger(@Value("${webhook.discord-admin}") String discordWebHookUrl) { @@ -13,9 +17,12 @@ public class DiscordWebHookMessenger { } public void sendMessage(String message) { + Map body = new HashMap<>(); + body.put(DISCORD_WEBHOOK_MESSAGE_KEY, message); + WebClient.create().post() .uri(discordWebHookUrl) - .bodyValue(message) + .bodyValue(body) .retrieve() .bodyToMono(String.class) .block(); diff --git a/backend/src/main/java/org/ftclub/cabinet/log/WebHookTestController.java b/backend/src/main/java/org/ftclub/cabinet/log/WebHookTestController.java new file mode 100644 index 000000000..dbd55ea67 --- /dev/null +++ b/backend/src/main/java/org/ftclub/cabinet/log/WebHookTestController.java @@ -0,0 +1,19 @@ +package org.ftclub.cabinet.log; + +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +public class WebHookTestController { + private final DiscordWebHookMessenger discordWebHookMessenger; + + @PostMapping("/test/discord") + public void post( + @RequestBody String message + ) { + discordWebHookMessenger.sendMessage(message); + } +} From c51a1e3a9e50aec447178c2160ee2b61fde875c1 Mon Sep 17 00:00:00 2001 From: Ssuamje Date: Fri, 15 Sep 2023 13:33:29 +0900 Subject: [PATCH 4/9] [BE] FIX: test application profile depedency fix --- backend/src/test/resources/application.yml | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/backend/src/test/resources/application.yml b/backend/src/test/resources/application.yml index fa3165899..59db39375 100644 --- a/backend/src/test/resources/application.yml +++ b/backend/src/test/resources/application.yml @@ -8,25 +8,17 @@ spring: port: 2424 datasource: - driver-class-name: org.h2.Driver - url: jdbc:h2:mem:;MODE=MySQL + driver-class-name: org.mariadb.jdbc.Driver + url: jdbc:mariadb://localhost:3310/test_db username: root password: test_password - sql: - init: - mode: embedded - jpa: hibernate: ddl-auto: none database-platform: org.hibernate.dialect.MySQL5InnoDBDialect properties: - hibernate: - globally_quoted_identifiers: true - dialect: org.hibernate.dialect.MySQL5InnoDBDialect - format_sql: true - defer-datasource-initialization: true + globally_quoted_identifiers: true cabinet: lent: @@ -56,7 +48,6 @@ spring: no-risk-of-blackhole: 0 42 1 1 * * # 매월 1일 1시 42분 0초 mail: - display-sender-name: "42CABI" soonoverdue: term: -1 subject: "42CABI 사물함 연체 예정 알림" @@ -76,4 +67,5 @@ spring: auth: true starttls: enable: true - +webhook: + discord-admin: https://discord.com/api/webhooks/for-test \ No newline at end of file From 03e621378c4b1b2208f1f3e312c4da28050de354 Mon Sep 17 00:00:00 2001 From: Ssuamje Date: Fri, 15 Sep 2023 13:46:17 +0900 Subject: [PATCH 5/9] [BE] FIX: test application profile depedency fix --- backend/src/test/resources/application.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/src/test/resources/application.yml b/backend/src/test/resources/application.yml index fa3165899..01779d904 100644 --- a/backend/src/test/resources/application.yml +++ b/backend/src/test/resources/application.yml @@ -76,4 +76,6 @@ spring: auth: true starttls: enable: true - + +webhook: + discord-admin: https://discord.com/api/webhooks/for-test \ No newline at end of file From b7f51fa4b57833d181f0fc1d0e84b2cdf9c68cb6 Mon Sep 17 00:00:00 2001 From: Ssuamje Date: Fri, 15 Sep 2023 13:46:42 +0900 Subject: [PATCH 6/9] [BE] FIX: test application profile depedency fix --- config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config b/config index ab48c1c5f..60dc56398 160000 --- a/config +++ b/config @@ -1 +1 @@ -Subproject commit ab48c1c5f2153aaf8ca10e6e9bddbf01b94162f8 +Subproject commit 60dc563984b71c8cc905cfe55cb77d5ae42d3e4d From 70645610520bffc34da1fc26cd3af87b1a28f3de Mon Sep 17 00:00:00 2001 From: Ssuamje Date: Fri, 15 Sep 2023 13:53:51 +0900 Subject: [PATCH 7/9] [BE] FIX: test application profile depedency fix --- backend/src/test/resources/application.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backend/src/test/resources/application.yml b/backend/src/test/resources/application.yml index f51f3acd5..e75a43c32 100644 --- a/backend/src/test/resources/application.yml +++ b/backend/src/test/resources/application.yml @@ -22,7 +22,11 @@ spring: ddl-auto: none database-platform: org.hibernate.dialect.MySQL5InnoDBDialect properties: - globally_quoted_identifiers: true + hibernate: + globally_quoted_identifiers: true + dialect: org.hibernate.dialect.MySQL5InnoDBDialect + format_sql: true + defer-datasource-initialization: true cabinet: lent: From 7af64818e566e8f34e518ddd949a8ec53e2fe1ef Mon Sep 17 00:00:00 2001 From: Ssuamje Date: Fri, 15 Sep 2023 16:20:15 +0900 Subject: [PATCH 8/9] =?UTF-8?q?[BE]=20FEAT:=20log=20parser=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EB=B0=8F=20discordmessage=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cabinet/alarm/DiscordAlarmMessage.java | 27 +++++++++++++++++++ .../DiscordWebHookMessenger.java | 6 ++--- .../org/ftclub/cabinet/log/LogParser.java | 21 +++++++++++++++ .../cabinet/log/WebHookTestController.java | 8 ++++-- 4 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 backend/src/main/java/org/ftclub/cabinet/alarm/DiscordAlarmMessage.java rename backend/src/main/java/org/ftclub/cabinet/{log => alarm}/DiscordWebHookMessenger.java (82%) create mode 100644 backend/src/main/java/org/ftclub/cabinet/log/LogParser.java diff --git a/backend/src/main/java/org/ftclub/cabinet/alarm/DiscordAlarmMessage.java b/backend/src/main/java/org/ftclub/cabinet/alarm/DiscordAlarmMessage.java new file mode 100644 index 000000000..bc3de70b7 --- /dev/null +++ b/backend/src/main/java/org/ftclub/cabinet/alarm/DiscordAlarmMessage.java @@ -0,0 +1,27 @@ +package org.ftclub.cabinet.alarm; + +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +public class DiscordAlarmMessage { + private final String subject; + private final String packageName; + private final String httpMethod; + private final String methodName; + private final String parameters; + private final String returnValue; + + @Override + public String toString() { + return "```java" + + "Subject: \"" + subject + "\"\n" + + "Package: \"" + packageName + "\"\n" + + "HTTP: \"" + httpMethod + "\"\n" + + "Method: \"" + methodName + "\"\n" + + "Parameters: \"" + parameters + "\"\n" + + "Return: \"" + returnValue + "\"\n" + + "```"; + } +} diff --git a/backend/src/main/java/org/ftclub/cabinet/log/DiscordWebHookMessenger.java b/backend/src/main/java/org/ftclub/cabinet/alarm/DiscordWebHookMessenger.java similarity index 82% rename from backend/src/main/java/org/ftclub/cabinet/log/DiscordWebHookMessenger.java rename to backend/src/main/java/org/ftclub/cabinet/alarm/DiscordWebHookMessenger.java index 91d570dde..af30a714b 100644 --- a/backend/src/main/java/org/ftclub/cabinet/log/DiscordWebHookMessenger.java +++ b/backend/src/main/java/org/ftclub/cabinet/alarm/DiscordWebHookMessenger.java @@ -1,4 +1,4 @@ -package org.ftclub.cabinet.log; +package org.ftclub.cabinet.alarm; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -16,9 +16,9 @@ public class DiscordWebHookMessenger { this.discordWebHookUrl = discordWebHookUrl; } - public void sendMessage(String message) { + public void sendMessage(DiscordAlarmMessage message) { Map body = new HashMap<>(); - body.put(DISCORD_WEBHOOK_MESSAGE_KEY, message); + body.put(DISCORD_WEBHOOK_MESSAGE_KEY, message.toString()); WebClient.create().post() .uri(discordWebHookUrl) diff --git a/backend/src/main/java/org/ftclub/cabinet/log/LogParser.java b/backend/src/main/java/org/ftclub/cabinet/log/LogParser.java new file mode 100644 index 000000000..7fb01a020 --- /dev/null +++ b/backend/src/main/java/org/ftclub/cabinet/log/LogParser.java @@ -0,0 +1,21 @@ +package org.ftclub.cabinet.log; + +import org.ftclub.cabinet.alarm.DiscordAlarmMessage; +import org.springframework.stereotype.Component; + +@Component +public class LogParser { + private static final String delimiter = "#"; + + public DiscordAlarmMessage parseToDiscordAlarmMessage(String log) { + String[] tokens = log.split(delimiter); + return DiscordAlarmMessage.builder() + .subject(tokens[0]) + .packageName(tokens[1]) + .httpMethod(tokens[2]) + .methodName(tokens[3]) + .parameters(tokens[4]) + .returnValue(tokens[5]) + .build(); + } +} diff --git a/backend/src/main/java/org/ftclub/cabinet/log/WebHookTestController.java b/backend/src/main/java/org/ftclub/cabinet/log/WebHookTestController.java index dbd55ea67..942efc4df 100644 --- a/backend/src/main/java/org/ftclub/cabinet/log/WebHookTestController.java +++ b/backend/src/main/java/org/ftclub/cabinet/log/WebHookTestController.java @@ -1,6 +1,8 @@ package org.ftclub.cabinet.log; import lombok.RequiredArgsConstructor; +import org.ftclub.cabinet.alarm.DiscordAlarmMessage; +import org.ftclub.cabinet.alarm.DiscordWebHookMessenger; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @@ -9,11 +11,13 @@ @RequiredArgsConstructor public class WebHookTestController { private final DiscordWebHookMessenger discordWebHookMessenger; + private final LogParser logParser; @PostMapping("/test/discord") public void post( - @RequestBody String message + @RequestBody String log ) { - discordWebHookMessenger.sendMessage(message); + DiscordAlarmMessage discordAlarmMessage = logParser.parseToDiscordAlarmMessage(log); + discordWebHookMessenger.sendMessage(discordAlarmMessage); } } From 5b44edcdff6bd92caca0f52ea6ebd6ac07bb8dc7 Mon Sep 17 00:00:00 2001 From: Ssuamje Date: Fri, 15 Sep 2023 16:34:51 +0900 Subject: [PATCH 9/9] =?UTF-8?q?[BE]=20FIX:=20=EC=95=8C=EB=9E=8C=20?= =?UTF-8?q?=EB=A9=94=EC=8B=9C=EC=A7=80=20=EC=A1=B0=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/ftclub/cabinet/alarm/DiscordAlarmMessage.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/org/ftclub/cabinet/alarm/DiscordAlarmMessage.java b/backend/src/main/java/org/ftclub/cabinet/alarm/DiscordAlarmMessage.java index bc3de70b7..4ad5ebb12 100644 --- a/backend/src/main/java/org/ftclub/cabinet/alarm/DiscordAlarmMessage.java +++ b/backend/src/main/java/org/ftclub/cabinet/alarm/DiscordAlarmMessage.java @@ -3,9 +3,13 @@ import lombok.Builder; import lombok.Getter; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + @Builder @Getter public class DiscordAlarmMessage { + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); private final String subject; private final String packageName; private final String httpMethod; @@ -15,13 +19,14 @@ public class DiscordAlarmMessage { @Override public String toString() { - return "```java" + + return "```java\n" + "Subject: \"" + subject + "\"\n" + + "Issued at: \"" + LocalDateTime.now().format(formatter) + "\"\n" + "Package: \"" + packageName + "\"\n" + "HTTP: \"" + httpMethod + "\"\n" + "Method: \"" + methodName + "\"\n" + "Parameters: \"" + parameters + "\"\n" + - "Return: \"" + returnValue + "\"\n" + + "Return: " + returnValue + "\n" + "```"; } }