From 53d7ba0beac0eaecfc902e5f0c0ec16ad7889386 Mon Sep 17 00:00:00 2001 From: woozu-shin Date: Mon, 13 May 2024 00:13:49 +0900 Subject: [PATCH] Update feign client --- .../datasource/H2ConsoleConfiguration.java | 1 - .../{ => feign}/FeignDefaultConfig.java | 3 +-- .../FmkoreaClientFeignConfiguration.java | 15 +++++++++++++++ .../feign/UserAgentInterceptor.java | 15 +++++++++++++++ .../controller/TestAPIController.java | 19 ++++++++++++------- .../infra/client/slack/SlackAPIClient.java | 2 +- .../shopping/scheduler/ParseEventEmitter.java | 2 +- 7 files changed, 45 insertions(+), 12 deletions(-) rename shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/{ => feign}/FeignDefaultConfig.java (88%) create mode 100644 shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/feign/FmkoreaClientFeignConfiguration.java create mode 100644 shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/feign/UserAgentInterceptor.java diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/datasource/H2ConsoleConfiguration.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/datasource/H2ConsoleConfiguration.java index 30991f9..3645d3e 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/datasource/H2ConsoleConfiguration.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/datasource/H2ConsoleConfiguration.java @@ -12,7 +12,6 @@ import org.springframework.context.event.EventListener; import java.sql.SQLException; @Slf4j -@Profile({"datasource-local", "datasource-development"}) @Configuration public class H2ConsoleConfiguration { diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/FeignDefaultConfig.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/feign/FeignDefaultConfig.java similarity index 88% rename from shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/FeignDefaultConfig.java rename to shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/feign/FeignDefaultConfig.java index f01a4d4..865912f 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/FeignDefaultConfig.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/feign/FeignDefaultConfig.java @@ -1,8 +1,7 @@ -package com.myoa.engineering.crawl.shopping.configuration; +package com.myoa.engineering.crawl.shopping.configuration.feign; import feign.Logger; import feign.RequestInterceptor; -import feign.codec.ErrorDecoder; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/feign/FmkoreaClientFeignConfiguration.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/feign/FmkoreaClientFeignConfiguration.java new file mode 100644 index 0000000..869a448 --- /dev/null +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/feign/FmkoreaClientFeignConfiguration.java @@ -0,0 +1,15 @@ +package com.myoa.engineering.crawl.shopping.configuration.feign; + +import feign.RequestInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class FmkoreaClientFeignConfiguration { + + @Bean + public RequestInterceptor requestInterceptor() { + return requestTemplate -> new UserAgentInterceptor().apply(requestTemplate); + } +} + diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/feign/UserAgentInterceptor.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/feign/UserAgentInterceptor.java new file mode 100644 index 0000000..210c9ce --- /dev/null +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/feign/UserAgentInterceptor.java @@ -0,0 +1,15 @@ +package com.myoa.engineering.crawl.shopping.configuration.feign; + +import feign.RequestInterceptor; +import feign.RequestTemplate; + +public class UserAgentInterceptor implements RequestInterceptor { + + private static final String USER_AGENT_HEADER = "User-Agent"; + private static final String USER_AGENT_VALUE = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"; + + @Override + public void apply(RequestTemplate template) { + template.header(USER_AGENT_HEADER, USER_AGENT_VALUE); + } +} diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/controller/TestAPIController.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/controller/TestAPIController.java index 02a666c..e1c4c0e 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/controller/TestAPIController.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/controller/TestAPIController.java @@ -1,30 +1,35 @@ package com.myoa.engineering.crawl.shopping.controller; -import com.myoa.engineering.crawl.shopping.crawlhandler.PpomppuCrawlDomesticHandler; +import com.myoa.engineering.crawl.shopping.crawlhandler.CrawlHandler; +import com.myoa.engineering.crawl.shopping.support.dto.constant.CrawlTarget; import com.slack.api.methods.MethodsClient; import com.slack.api.methods.SlackApiException; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; +import java.util.List; @RestController @RequestMapping("/api/v1/exploit") public class TestAPIController { - private final PpomppuCrawlDomesticHandler ppomppuCrawlDomesticHandler; private final MethodsClient methodsClient; + private final List crawlHandlers; - public TestAPIController(PpomppuCrawlDomesticHandler ppomppuCrawlDomesticHandler, - MethodsClient methodsClient) { - this.ppomppuCrawlDomesticHandler = ppomppuCrawlDomesticHandler; + + public TestAPIController(MethodsClient methodsClient, List crawlHandlers) { this.methodsClient = methodsClient; + this.crawlHandlers = crawlHandlers; } @GetMapping("/triggers") - public void triggerExploit() { - ppomppuCrawlDomesticHandler.handle(); + public void triggerExploit(@RequestParam("crawlTarget") CrawlTarget crawlTarget) { + crawlHandlers + .stream().filter(e -> e.getCrawlTarget().equals(crawlTarget)) + .forEach(CrawlHandler::handle); } @GetMapping("/test-message") diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/infra/client/slack/SlackAPIClient.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/infra/client/slack/SlackAPIClient.java index 21a87e3..3b6e00c 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/infra/client/slack/SlackAPIClient.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/infra/client/slack/SlackAPIClient.java @@ -1,6 +1,6 @@ package com.myoa.engineering.crawl.shopping.infra.client.slack; -import com.myoa.engineering.crawl.shopping.configuration.FeignDefaultConfig; +import com.myoa.engineering.crawl.shopping.configuration.feign.FeignDefaultConfig; import com.myoa.engineering.crawl.shopping.dto.slack.v1.SlackMessageDTO; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/scheduler/ParseEventEmitter.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/scheduler/ParseEventEmitter.java index 6f99680..18d985d 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/scheduler/ParseEventEmitter.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/scheduler/ParseEventEmitter.java @@ -21,7 +21,7 @@ public class ParseEventEmitter { @Scheduled(cron = "0 0/5 * * * ?") public void emit() { - log.info("[emitDomesticBoard] trigger fired!"); + log.info("[emit] trigger fired!"); crawlHandlers.forEach(CrawlHandler::handle); }