-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1385 from innovationacademy-kr/be/dev/feat_admin_…
…prod/#1384 [BE] Discord Webhook Messenger 추가
- Loading branch information
Showing
6 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
backend/src/main/java/org/ftclub/cabinet/alarm/DiscordAlarmMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.ftclub.cabinet.alarm; | ||
|
||
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; | ||
private final String methodName; | ||
private final String parameters; | ||
private final String returnValue; | ||
|
||
@Override | ||
public String toString() { | ||
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" + | ||
"```"; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
backend/src/main/java/org/ftclub/cabinet/alarm/DiscordWebHookMessenger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.ftclub.cabinet.alarm; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
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) { | ||
this.discordWebHookUrl = discordWebHookUrl; | ||
} | ||
|
||
public void sendMessage(DiscordAlarmMessage message) { | ||
Map<String, String> body = new HashMap<>(); | ||
body.put(DISCORD_WEBHOOK_MESSAGE_KEY, message.toString()); | ||
|
||
WebClient.create().post() | ||
.uri(discordWebHookUrl) | ||
.bodyValue(body) | ||
.retrieve() | ||
.bodyToMono(String.class) | ||
.block(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
backend/src/main/java/org/ftclub/cabinet/log/LogParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
backend/src/main/java/org/ftclub/cabinet/log/WebHookTestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class WebHookTestController { | ||
private final DiscordWebHookMessenger discordWebHookMessenger; | ||
private final LogParser logParser; | ||
|
||
@PostMapping("/test/discord") | ||
public void post( | ||
@RequestBody String log | ||
) { | ||
DiscordAlarmMessage discordAlarmMessage = logParser.parseToDiscordAlarmMessage(log); | ||
discordWebHookMessenger.sendMessage(discordAlarmMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,3 +77,5 @@ spring: | |
starttls: | ||
enable: true | ||
|
||
webhook: | ||
discord-admin: https://discord.com/api/webhooks/for-test |
Submodule config
updated
from 73abe8 to 60dc56