-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Feature/#243] λμ€μ½λ μλ¦Ό μ°λ
- Loading branch information
Showing
11 changed files
with
231 additions
and
13 deletions.
There are no files selected for viewing
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
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
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
10 changes: 10 additions & 0 deletions
10
linkmind/src/main/java/com/app/toaster/controller/HealthCheckController.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
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
14 changes: 14 additions & 0 deletions
14
linkmind/src/main/java/com/app/toaster/external/client/discord/DiscordClient.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,14 @@ | ||
package com.app.toaster.external.client.discord; | ||
|
||
import java.net.URI; | ||
|
||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
|
||
@FeignClient(name = "discord-feign-client", url = "URI") | ||
public interface DiscordClient { | ||
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE) | ||
void sendMessage(URI uri, @RequestBody DiscordMessage discordMessage); | ||
} |
29 changes: 29 additions & 0 deletions
29
linkmind/src/main/java/com/app/toaster/external/client/discord/DiscordMessage.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,29 @@ | ||
package com.app.toaster.external.client.discord; | ||
|
||
import java.util.List; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Builder | ||
@AllArgsConstructor(access = AccessLevel.PROTECTED) | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
public class DiscordMessage { | ||
|
||
private String content; | ||
private List<Embed> embeds; | ||
|
||
@Builder | ||
@AllArgsConstructor(access = AccessLevel.PROTECTED) | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
public static class Embed { | ||
|
||
private String title; | ||
private String description; | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
linkmind/src/main/java/com/app/toaster/external/client/discord/DiscordMessageProvider.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,118 @@ | ||
package com.app.toaster.external.client.discord; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.net.URI; | ||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.event.TransactionPhase; | ||
import org.springframework.transaction.event.TransactionalEventListener; | ||
import org.springframework.web.context.request.ServletWebRequest; | ||
import org.springframework.web.context.request.WebRequest; | ||
|
||
import com.app.toaster.exception.Error; | ||
import com.app.toaster.exception.model.CustomException; | ||
import com.app.toaster.infrastructure.UserRepository; | ||
|
||
import feign.FeignException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
@Slf4j | ||
public class DiscordMessageProvider { | ||
private final DiscordClient discordClient; | ||
private final UserRepository userRepository; | ||
private final Environment environment; | ||
|
||
@Value("${discord.webhook-url-error}") | ||
private String webhookUrlError; | ||
|
||
|
||
@Value("${discord.webhook-url-sign}") | ||
private String webhookUrlSign; | ||
|
||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
public void sendNotification(NotificationDto notification) { | ||
if (!Arrays.asList(environment.getActiveProfiles()).contains("local")) { | ||
try { | ||
switch (notification.type()){ | ||
case ERROR -> discordClient.sendMessage(URI.create(webhookUrlError), createErrorMessage(notification.e(), notification.request())); | ||
case SIGNUP -> discordClient.sendMessage(URI.create(webhookUrlSign), createSignUpMessage()); | ||
} | ||
} catch (Exception error) { | ||
log.warn("discord notification fail : " + error); | ||
} | ||
} | ||
} | ||
|
||
private DiscordMessage createSignUpMessage() { | ||
return DiscordMessage.builder() | ||
.content("# π νμκ°μ μ΄λ²€νΈκ° λ°μνμ΅λλ€.") | ||
.embeds( | ||
List.of( | ||
DiscordMessage.Embed.builder() | ||
.title("βΉοΈ νμκ°μ μ 보") | ||
.description( | ||
"### π λ°μ μκ°\n" | ||
+ LocalDateTime.now() | ||
+ "\n" | ||
+ "### π μ μ κ°μ μ 보\n" | ||
+ "ν μ€ν°μ " + userRepository.count() + "λ²μ§Έ μ μ κ° μμ±λμμ΅λλ€!! β€οΈ" | ||
+ "\n") | ||
.build() | ||
) | ||
) | ||
.build(); | ||
} | ||
|
||
|
||
private DiscordMessage createErrorMessage(Exception e, String requestUrl) { | ||
return DiscordMessage.builder() | ||
.content("# π¨ μμ©μμ© μλ¬λ¬μ΄μ μλ¬λ¬μ΄μ") | ||
.embeds( | ||
List.of( | ||
DiscordMessage.Embed.builder() | ||
.title("βΉοΈ μλ¬ μ 보") | ||
.description( | ||
"### π λ°μ μκ°\n" | ||
+ LocalDateTime.now() | ||
+ "\n" | ||
+ "### π μμ² URL\n" | ||
+ requestUrl | ||
+ "\n" | ||
+ "### π Stack Trace\n" | ||
+ "```\n" | ||
+ getStackTrace(e).substring(0, 1000) | ||
+ "\n```") | ||
.build() | ||
) | ||
) | ||
.build(); | ||
} | ||
|
||
private String createRequestFullPath(WebRequest webRequest) { | ||
HttpServletRequest request = ((ServletWebRequest) webRequest).getRequest(); | ||
String fullPath = request.getMethod() + " " + request.getRequestURL(); | ||
|
||
String queryString = request.getQueryString(); | ||
if (queryString != null) { | ||
fullPath += "?" + queryString; | ||
} | ||
|
||
return fullPath; | ||
} | ||
|
||
private String getStackTrace(Exception e) { | ||
StringWriter stringWriter = new StringWriter(); | ||
e.printStackTrace(new PrintWriter(stringWriter)); | ||
return stringWriter.toString(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
linkmind/src/main/java/com/app/toaster/external/client/discord/NotificationDto.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,9 @@ | ||
package com.app.toaster.external.client.discord; | ||
|
||
|
||
public record NotificationDto( | ||
NotificationType type, | ||
Exception e, | ||
String request | ||
) { | ||
} |
6 changes: 6 additions & 0 deletions
6
linkmind/src/main/java/com/app/toaster/external/client/discord/NotificationType.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,6 @@ | ||
package com.app.toaster.external.client.discord; | ||
|
||
public enum NotificationType { | ||
ERROR, | ||
SIGNUP | ||
} |
Oops, something went wrong.