Skip to content

Commit

Permalink
Merge pull request #221 from TeamDilly/develop
Browse files Browse the repository at this point in the history
v1.4.0
  • Loading branch information
leeeeeyeon authored May 12, 2024
2 parents fd616fb + fbd50bf commit b002894
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ packy-api/src/test/resources/*.sql
packy-domain/src/main/resources/*.yml
packy-domain/src/main/resources/*.sql
packy-infra/src/main/resources/*.yml
packy-support/src/main/resources/*.yml

### QueryDsl QClass ###
packy-domain/src/main/generated
1 change: 1 addition & 0 deletions packy-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
implementation project(':packy-common')
implementation project(':packy-domain')
implementation project(':packy-infra')
implementation project(':packy-support')

testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
11 changes: 5 additions & 6 deletions packy-api/src/main/java/com/dilly/ApiApplication.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.dilly;

import java.util.TimeZone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.servers.Server;
import jakarta.annotation.PostConstruct;
import java.util.TimeZone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@OpenAPIDefinition(servers = {
@Server(url = "http://localhost:8081", description = "Local Server URL"),
Expand All @@ -22,7 +20,8 @@ public void init() {
}

public static void main(String[] args) {
System.setProperty("spring.config.name", "application-api, application-domain, application-infra");
System.setProperty("spring.config.name",
"application-api, application-domain, application-infra, application-support");
SpringApplication.run(ApiApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.dilly.member;

import com.dilly.member.domain.Member;
import com.dilly.member.domain.Status;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberRepository extends JpaRepository<Member, Long> {

Long countByStatus(Status status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dilly.exception.entitynotfound.MemberNotFoundException;
import com.dilly.member.MemberRepository;
import com.dilly.member.domain.Member;
import com.dilly.member.domain.Status;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand All @@ -15,4 +16,8 @@ public class MemberReader {
public Member findById(Long id) {
return memberRepository.findById(id).orElseThrow(MemberNotFoundException::new);
}

public Long countByStatus(Status status) {
return memberRepository.countByStatus(status);
}
}
2 changes: 1 addition & 1 deletion packy-submodule
14 changes: 14 additions & 0 deletions packy-support/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ repositories {
}

dependencies {
// multi module
implementation project(':packy-domain')

implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'com.slack.api:slack-api-client:1.39.0'

// jacoco
jacocoAggregation project(':packy-api')
jacocoAggregation project(':packy-domain')
}
Expand All @@ -20,6 +27,13 @@ test {
finalizedBy 'testCodeCoverageReport'
}

processResources.dependsOn('copySecret')
tasks.register('copySecret', Copy) {
from '../packy-submodule/support'
include '*.yml'
into './src/main/resources'
}

testCodeCoverageReport {
reports {
html.required = true
Expand Down
39 changes: 39 additions & 0 deletions packy-support/src/main/java/com/dilly/logging/LogAspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.dilly.logging;

import com.dilly.member.adaptor.MemberReader;
import com.dilly.member.domain.Status;
import java.util.HashMap;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Aspect
@Profile("prod")
@Component
@RequiredArgsConstructor
public class LogAspect {

private final SlackService slackService;
private final MemberReader memberReader;

@Value("${slack.webhook-url.sign-up}")
private String signUpUrl;

@Pointcut("execution(* com.dilly.auth..AuthController.signUp(..))")
public void signUp() {}

@After("signUp()")
public void countMember() {
String title = "패키에 유저가 들어왔어요!";
HashMap<String, String> data = new HashMap<>();

Long memberCount = memberReader.countByStatus(Status.REGISTERED);
data.put("현재 유저 수: ", memberCount.toString() + "명");

slackService.sendMessage(signUpUrl, title, data);
}
}
47 changes: 47 additions & 0 deletions packy-support/src/main/java/com/dilly/logging/SlackService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.dilly.logging;

import com.slack.api.Slack;
import com.slack.api.model.Attachment;
import com.slack.api.model.Field;
import com.slack.api.webhook.Payload;
import java.awt.Color;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SlackService {

private final Slack slackClient = Slack.getInstance();

public void sendMessage(String url, String title, Map<String, String> data) {
try {
slackClient.send(url, Payload.builder()
.text(title)
.attachments(List.of(
Attachment.builder()
.color(Color.GREEN.toString())
.fields(data.keySet()
.stream()
.map(key -> generateSlackField(key, data.get(key)))
.toList()
)
.build()
))
.build()
);
} catch (Exception e) {
log.error("Slack 메시지 전송에 실패했습니다.", e);
}
}

private Field generateSlackField(String title, String value) {
return Field.builder()
.title(title)
.value(value)
.valueShortEnough(false)
.build();
}
}

0 comments on commit b002894

Please sign in to comment.