From 11a2b05f18a54836bbdc90e9035b4ff67047d68c Mon Sep 17 00:00:00 2001 From: Donghoon Lee Date: Sun, 10 Nov 2024 13:29:04 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20refactor:=20Config=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=20=EB=B6=84=EB=A6=AC,=20=EC=8A=A4=ED=83=80=EC=9D=BC?= =?UTF-8?q?=20=EC=A0=81=EC=9A=A9=20(#957)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: use `@Slf4j` instead of `LoggerFactory.getLogger` * refactor: `WebConfig`가 하나의 역할만 하도록 수정 * style: dto 불필요한 개행 제거 * refactor: Config는 상위 패키지, Resolver/Interceptor는 각 도메인에 두기 --- .../main/java/reviewme/config/WebConfig.java | 10 ---------- .../config/{ => cors}/CorsConfig.java | 8 +++----- .../config/{ => cors}/CorsProperties.java | 2 +- .../{ => datasource}/DataSourceType.java | 2 +- .../ReplicationDatasourceConfig.java | 3 +-- .../ReplicationRoutingDataSource.java | 2 +- .../RequestLimitInterceptor.java | 4 +--- .../RequestLimitProperties.java | 9 ++------- .../RequestLimitRedisConfig.java | 2 +- .../requestlimit/RequestLimitWebConfig.java | 20 +++++++++++++++++++ .../TooManyRequestException.java | 3 ++- .../global/GlobalExceptionHandler.java | 2 +- .../global/exception/FieldErrorResponse.java | 6 +----- .../review/service/ReviewRegisterService.java | 4 ---- .../config/{ => cors}/CorsConfigTest.java | 3 ++- .../{ => cors}/ExternalCorsConfigTest.java | 2 +- .../{ => cors}/LocalCorsConfigTest.java | 2 +- .../RequestLimitInterceptorTest.java | 5 +---- 18 files changed, 40 insertions(+), 49 deletions(-) rename backend/src/main/java/reviewme/config/{ => cors}/CorsConfig.java (90%) rename backend/src/main/java/reviewme/config/{ => cors}/CorsProperties.java (91%) rename backend/src/main/java/reviewme/config/{ => datasource}/DataSourceType.java (62%) rename backend/src/main/java/reviewme/config/{ => datasource}/ReplicationDatasourceConfig.java (98%) rename backend/src/main/java/reviewme/config/{ => datasource}/ReplicationRoutingDataSource.java (93%) rename backend/src/main/java/reviewme/{global => config/requestlimit}/RequestLimitInterceptor.java (93%) rename backend/src/main/java/reviewme/config/{ => requestlimit}/RequestLimitProperties.java (50%) rename backend/src/main/java/reviewme/config/{ => requestlimit}/RequestLimitRedisConfig.java (97%) create mode 100644 backend/src/main/java/reviewme/config/requestlimit/RequestLimitWebConfig.java rename backend/src/main/java/reviewme/{global/exception => config/requestlimit}/TooManyRequestException.java (80%) rename backend/src/test/java/reviewme/config/{ => cors}/CorsConfigTest.java (93%) rename backend/src/test/java/reviewme/config/{ => cors}/ExternalCorsConfigTest.java (98%) rename backend/src/test/java/reviewme/config/{ => cors}/LocalCorsConfigTest.java (97%) rename backend/src/test/java/reviewme/{global => config/requestlimit}/RequestLimitInterceptorTest.java (93%) diff --git a/backend/src/main/java/reviewme/config/WebConfig.java b/backend/src/main/java/reviewme/config/WebConfig.java index 916ea5a41..d855040f0 100644 --- a/backend/src/main/java/reviewme/config/WebConfig.java +++ b/backend/src/main/java/reviewme/config/WebConfig.java @@ -3,11 +3,8 @@ import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.method.support.HandlerMethodArgumentResolver; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import reviewme.global.RequestLimitInterceptor; import reviewme.reviewgroup.controller.ReviewGroupSessionResolver; import reviewme.reviewgroup.service.ReviewGroupService; @@ -16,16 +13,9 @@ public class WebConfig implements WebMvcConfigurer { private final ReviewGroupService reviewGroupService; - private final RedisTemplate redisTemplate; - private final RequestLimitProperties requestLimitProperties; @Override public void addArgumentResolvers(List resolvers) { resolvers.add(new ReviewGroupSessionResolver(reviewGroupService)); } - - @Override - public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(new RequestLimitInterceptor(redisTemplate, requestLimitProperties)); - } } diff --git a/backend/src/main/java/reviewme/config/CorsConfig.java b/backend/src/main/java/reviewme/config/cors/CorsConfig.java similarity index 90% rename from backend/src/main/java/reviewme/config/CorsConfig.java rename to backend/src/main/java/reviewme/config/cors/CorsConfig.java index 2f51720ea..ff1a483a3 100644 --- a/backend/src/main/java/reviewme/config/CorsConfig.java +++ b/backend/src/main/java/reviewme/config/cors/CorsConfig.java @@ -1,18 +1,16 @@ -package reviewme.config; +package reviewme.config.cors; import lombok.RequiredArgsConstructor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +@Slf4j public class CorsConfig { - private static final Logger log = LoggerFactory.getLogger(CorsConfig.class); - private CorsConfig() { } diff --git a/backend/src/main/java/reviewme/config/CorsProperties.java b/backend/src/main/java/reviewme/config/cors/CorsProperties.java similarity index 91% rename from backend/src/main/java/reviewme/config/CorsProperties.java rename to backend/src/main/java/reviewme/config/cors/CorsProperties.java index 69a7d1c4d..e11a93667 100644 --- a/backend/src/main/java/reviewme/config/CorsProperties.java +++ b/backend/src/main/java/reviewme/config/cors/CorsProperties.java @@ -1,4 +1,4 @@ -package reviewme.config; +package reviewme.config.cors; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; diff --git a/backend/src/main/java/reviewme/config/DataSourceType.java b/backend/src/main/java/reviewme/config/datasource/DataSourceType.java similarity index 62% rename from backend/src/main/java/reviewme/config/DataSourceType.java rename to backend/src/main/java/reviewme/config/datasource/DataSourceType.java index c48080ab4..b40750df2 100644 --- a/backend/src/main/java/reviewme/config/DataSourceType.java +++ b/backend/src/main/java/reviewme/config/datasource/DataSourceType.java @@ -1,4 +1,4 @@ -package reviewme.config; +package reviewme.config.datasource; public enum DataSourceType { READ, diff --git a/backend/src/main/java/reviewme/config/ReplicationDatasourceConfig.java b/backend/src/main/java/reviewme/config/datasource/ReplicationDatasourceConfig.java similarity index 98% rename from backend/src/main/java/reviewme/config/ReplicationDatasourceConfig.java rename to backend/src/main/java/reviewme/config/datasource/ReplicationDatasourceConfig.java index 6a33a9e08..fb59b2498 100644 --- a/backend/src/main/java/reviewme/config/ReplicationDatasourceConfig.java +++ b/backend/src/main/java/reviewme/config/datasource/ReplicationDatasourceConfig.java @@ -1,4 +1,4 @@ -package reviewme.config; +package reviewme.config.datasource; import java.util.HashMap; import java.util.Map; @@ -54,4 +54,3 @@ public DataSource dataSource(@Qualifier(ROUTING_DATA_SOURCE_NAME) DataSource rou return new LazyConnectionDataSourceProxy(routingDataSource); } } - diff --git a/backend/src/main/java/reviewme/config/ReplicationRoutingDataSource.java b/backend/src/main/java/reviewme/config/datasource/ReplicationRoutingDataSource.java similarity index 93% rename from backend/src/main/java/reviewme/config/ReplicationRoutingDataSource.java rename to backend/src/main/java/reviewme/config/datasource/ReplicationRoutingDataSource.java index 49b7aa22b..f8a802467 100644 --- a/backend/src/main/java/reviewme/config/ReplicationRoutingDataSource.java +++ b/backend/src/main/java/reviewme/config/datasource/ReplicationRoutingDataSource.java @@ -1,4 +1,4 @@ -package reviewme.config; +package reviewme.config.datasource; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; import org.springframework.transaction.support.TransactionSynchronizationManager; diff --git a/backend/src/main/java/reviewme/global/RequestLimitInterceptor.java b/backend/src/main/java/reviewme/config/requestlimit/RequestLimitInterceptor.java similarity index 93% rename from backend/src/main/java/reviewme/global/RequestLimitInterceptor.java rename to backend/src/main/java/reviewme/config/requestlimit/RequestLimitInterceptor.java index b5747dfd1..ef25b711e 100644 --- a/backend/src/main/java/reviewme/global/RequestLimitInterceptor.java +++ b/backend/src/main/java/reviewme/config/requestlimit/RequestLimitInterceptor.java @@ -1,4 +1,4 @@ -package reviewme.global; +package reviewme.config.requestlimit; import static org.springframework.http.HttpHeaders.USER_AGENT; @@ -11,8 +11,6 @@ import org.springframework.http.HttpMethod; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; -import reviewme.config.RequestLimitProperties; -import reviewme.global.exception.TooManyRequestException; @Component @EnableConfigurationProperties(RequestLimitProperties.class) diff --git a/backend/src/main/java/reviewme/config/RequestLimitProperties.java b/backend/src/main/java/reviewme/config/requestlimit/RequestLimitProperties.java similarity index 50% rename from backend/src/main/java/reviewme/config/RequestLimitProperties.java rename to backend/src/main/java/reviewme/config/requestlimit/RequestLimitProperties.java index efea3b4f8..558378094 100644 --- a/backend/src/main/java/reviewme/config/RequestLimitProperties.java +++ b/backend/src/main/java/reviewme/config/requestlimit/RequestLimitProperties.java @@ -1,13 +1,8 @@ -package reviewme.config; +package reviewme.config.requestlimit; import java.time.Duration; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "request-limit") -public record RequestLimitProperties( - long threshold, - Duration duration, - String host, - int port -) { +public record RequestLimitProperties(long threshold, Duration duration, String host, int port) { } diff --git a/backend/src/main/java/reviewme/config/RequestLimitRedisConfig.java b/backend/src/main/java/reviewme/config/requestlimit/RequestLimitRedisConfig.java similarity index 97% rename from backend/src/main/java/reviewme/config/RequestLimitRedisConfig.java rename to backend/src/main/java/reviewme/config/requestlimit/RequestLimitRedisConfig.java index a8307db5f..d8bb458a9 100644 --- a/backend/src/main/java/reviewme/config/RequestLimitRedisConfig.java +++ b/backend/src/main/java/reviewme/config/requestlimit/RequestLimitRedisConfig.java @@ -1,4 +1,4 @@ -package reviewme.config; +package reviewme.config.requestlimit; import lombok.RequiredArgsConstructor; import org.springframework.boot.context.properties.EnableConfigurationProperties; diff --git a/backend/src/main/java/reviewme/config/requestlimit/RequestLimitWebConfig.java b/backend/src/main/java/reviewme/config/requestlimit/RequestLimitWebConfig.java new file mode 100644 index 000000000..19f3b2fe4 --- /dev/null +++ b/backend/src/main/java/reviewme/config/requestlimit/RequestLimitWebConfig.java @@ -0,0 +1,20 @@ +package reviewme.config.requestlimit; + +import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@RequiredArgsConstructor +public class RequestLimitWebConfig implements WebMvcConfigurer { + + private final RedisTemplate redisTemplate; + private final RequestLimitProperties requestLimitProperties; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new RequestLimitInterceptor(redisTemplate, requestLimitProperties)); + } +} diff --git a/backend/src/main/java/reviewme/global/exception/TooManyRequestException.java b/backend/src/main/java/reviewme/config/requestlimit/TooManyRequestException.java similarity index 80% rename from backend/src/main/java/reviewme/global/exception/TooManyRequestException.java rename to backend/src/main/java/reviewme/config/requestlimit/TooManyRequestException.java index 4f26fee3e..544fb5885 100644 --- a/backend/src/main/java/reviewme/global/exception/TooManyRequestException.java +++ b/backend/src/main/java/reviewme/config/requestlimit/TooManyRequestException.java @@ -1,6 +1,7 @@ -package reviewme.global.exception; +package reviewme.config.requestlimit; import lombok.extern.slf4j.Slf4j; +import reviewme.global.exception.ReviewMeException; @Slf4j public class TooManyRequestException extends ReviewMeException { diff --git a/backend/src/main/java/reviewme/global/GlobalExceptionHandler.java b/backend/src/main/java/reviewme/global/GlobalExceptionHandler.java index 9d4511618..161e43172 100644 --- a/backend/src/main/java/reviewme/global/GlobalExceptionHandler.java +++ b/backend/src/main/java/reviewme/global/GlobalExceptionHandler.java @@ -22,7 +22,7 @@ import org.springframework.web.servlet.resource.NoResourceFoundException; import reviewme.global.exception.BadRequestException; import reviewme.global.exception.DataInconsistencyException; -import reviewme.global.exception.TooManyRequestException; +import reviewme.config.requestlimit.TooManyRequestException; import reviewme.global.exception.FieldErrorResponse; import reviewme.global.exception.NotFoundException; import reviewme.global.exception.UnauthorizedException; diff --git a/backend/src/main/java/reviewme/global/exception/FieldErrorResponse.java b/backend/src/main/java/reviewme/global/exception/FieldErrorResponse.java index e44edf619..ae0c678a4 100644 --- a/backend/src/main/java/reviewme/global/exception/FieldErrorResponse.java +++ b/backend/src/main/java/reviewme/global/exception/FieldErrorResponse.java @@ -1,8 +1,4 @@ package reviewme.global.exception; -public record FieldErrorResponse( - String field, - Object value, - String message -) { +public record FieldErrorResponse(String field, Object value, String message) { } diff --git a/backend/src/main/java/reviewme/review/service/ReviewRegisterService.java b/backend/src/main/java/reviewme/review/service/ReviewRegisterService.java index 966eaa602..87e5f8538 100644 --- a/backend/src/main/java/reviewme/review/service/ReviewRegisterService.java +++ b/backend/src/main/java/reviewme/review/service/ReviewRegisterService.java @@ -1,8 +1,6 @@ package reviewme.review.service; import lombok.RequiredArgsConstructor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import reviewme.review.domain.Review; @@ -15,8 +13,6 @@ @RequiredArgsConstructor public class ReviewRegisterService { - private static final Logger log = LoggerFactory.getLogger(ReviewRegisterService.class); - private final ReviewMapper reviewMapper; private final ReviewValidator reviewValidator; private final ReviewRepository reviewRepository; diff --git a/backend/src/test/java/reviewme/config/CorsConfigTest.java b/backend/src/test/java/reviewme/config/cors/CorsConfigTest.java similarity index 93% rename from backend/src/test/java/reviewme/config/CorsConfigTest.java rename to backend/src/test/java/reviewme/config/cors/CorsConfigTest.java index 90af4a342..d7f20bfd2 100644 --- a/backend/src/test/java/reviewme/config/CorsConfigTest.java +++ b/backend/src/test/java/reviewme/config/cors/CorsConfigTest.java @@ -1,4 +1,4 @@ -package reviewme.config; +package reviewme.config.cors; import org.junit.jupiter.api.BeforeEach; import org.springframework.beans.factory.annotation.Autowired; @@ -32,6 +32,7 @@ void setUp() { static class TestController { @RequestMapping("/test") public void test() { + // Testing controller calls, no-op } } } diff --git a/backend/src/test/java/reviewme/config/ExternalCorsConfigTest.java b/backend/src/test/java/reviewme/config/cors/ExternalCorsConfigTest.java similarity index 98% rename from backend/src/test/java/reviewme/config/ExternalCorsConfigTest.java rename to backend/src/test/java/reviewme/config/cors/ExternalCorsConfigTest.java index 095bb1bc7..39445b70f 100644 --- a/backend/src/test/java/reviewme/config/ExternalCorsConfigTest.java +++ b/backend/src/test/java/reviewme/config/cors/ExternalCorsConfigTest.java @@ -1,4 +1,4 @@ -package reviewme.config; +package reviewme.config.cors; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; diff --git a/backend/src/test/java/reviewme/config/LocalCorsConfigTest.java b/backend/src/test/java/reviewme/config/cors/LocalCorsConfigTest.java similarity index 97% rename from backend/src/test/java/reviewme/config/LocalCorsConfigTest.java rename to backend/src/test/java/reviewme/config/cors/LocalCorsConfigTest.java index cd050b988..214de0857 100644 --- a/backend/src/test/java/reviewme/config/LocalCorsConfigTest.java +++ b/backend/src/test/java/reviewme/config/cors/LocalCorsConfigTest.java @@ -1,4 +1,4 @@ -package reviewme.config; +package reviewme.config.cors; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; diff --git a/backend/src/test/java/reviewme/global/RequestLimitInterceptorTest.java b/backend/src/test/java/reviewme/config/requestlimit/RequestLimitInterceptorTest.java similarity index 93% rename from backend/src/test/java/reviewme/global/RequestLimitInterceptorTest.java rename to backend/src/test/java/reviewme/config/requestlimit/RequestLimitInterceptorTest.java index 998639691..969040683 100644 --- a/backend/src/test/java/reviewme/global/RequestLimitInterceptorTest.java +++ b/backend/src/test/java/reviewme/config/requestlimit/RequestLimitInterceptorTest.java @@ -1,4 +1,4 @@ -package reviewme.global; +package reviewme.config.requestlimit; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; @@ -14,8 +14,6 @@ import org.junit.jupiter.api.Test; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; -import reviewme.config.RequestLimitProperties; -import reviewme.global.exception.TooManyRequestException; class RequestLimitInterceptorTest { @@ -41,7 +39,6 @@ void setUp() { @Test void POST_요청이_아니면_통과한다() { // given - HttpServletRequest request = mock(HttpServletRequest.class); given(request.getMethod()).willReturn("GET"); // when