Skip to content

Commit

Permalink
[BE] refactor: Config 패키지 분리, 스타일 적용 (#957)
Browse files Browse the repository at this point in the history
* chore: use `@Slf4j` instead of `LoggerFactory.getLogger`

* refactor: `WebConfig`가 하나의 역할만 하도록 수정

* style: dto 불필요한 개행 제거

* refactor: Config는 상위 패키지, Resolver/Interceptor는 각 도메인에 두기
  • Loading branch information
donghoony authored Nov 10, 2024
1 parent 7cd031c commit 11a2b05
Show file tree
Hide file tree
Showing 18 changed files with 40 additions and 49 deletions.
10 changes: 0 additions & 10 deletions backend/src/main/java/reviewme/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,16 +13,9 @@
public class WebConfig implements WebMvcConfigurer {

private final ReviewGroupService reviewGroupService;
private final RedisTemplate<String, Long> redisTemplate;
private final RequestLimitProperties requestLimitProperties;

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new ReviewGroupSessionResolver(reviewGroupService));
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new RequestLimitInterceptor(redisTemplate, requestLimitProperties));
}
}
Original file line number Diff line number Diff line change
@@ -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() {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reviewme.config;
package reviewme.config.cors;

import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reviewme.config;
package reviewme.config.datasource;

public enum DataSourceType {
READ,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reviewme.config;
package reviewme.config.datasource;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -54,4 +54,3 @@ public DataSource dataSource(@Qualifier(ROUTING_DATA_SOURCE_NAME) DataSource rou
return new LazyConnectionDataSourceProxy(routingDataSource);
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reviewme.config;
package reviewme.config.datasource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.transaction.support.TransactionSynchronizationManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reviewme.global;
package reviewme.config.requestlimit;

import static org.springframework.http.HttpHeaders.USER_AGENT;

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reviewme.config;
package reviewme.config.requestlimit;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Long> redisTemplate;
private final RequestLimitProperties requestLimitProperties;

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new RequestLimitInterceptor(redisTemplate, requestLimitProperties));
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reviewme.config;
package reviewme.config.cors;

import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -32,6 +32,7 @@ void setUp() {
static class TestController {
@RequestMapping("/test")
public void test() {
// Testing controller calls, no-op
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -41,7 +39,6 @@ void setUp() {
@Test
void POST_요청이_아니면_통과한다() {
// given
HttpServletRequest request = mock(HttpServletRequest.class);
given(request.getMethod()).willReturn("GET");

// when
Expand Down

0 comments on commit 11a2b05

Please sign in to comment.