Skip to content

Commit

Permalink
chore: 공통 상수 Enum으로 관리 (#71)
Browse files Browse the repository at this point in the history
* chore: 공통 상수 enum 생성

* refactor: 공통 상수 값 사용하도록 변경

* refactor: 공통 상수 관심사에 맞도록 분리
  • Loading branch information
kdomo authored Dec 22, 2023
1 parent ffa0b20 commit 938a63b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.depromeet.global.common.constants;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum EnvironmentConstants {
PROD("prod"),
DEV("dev"),
LOCAL("local"),
;

private String value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.depromeet.global.common.constants;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum UrlConstants {
PROD_SERVER_URL("https://api.10mm.today"),
DEV_SERVER_URL("https://dev-api.10mm.today"),
LOCAL_SERVER_URL("http://localhost:8080"),

PROD_DOMAIN_URL("https://10mm.today"),
DEV_DOMAIN_URL("https://dev.10mm.today"),
LOCAL_DOMAIN_URL("http://localhost:3000"),
;

private String value;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.depromeet.global.config.security;

import com.depromeet.global.common.constants.UrlConstants;
import com.depromeet.global.util.SpringEnvironmentUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -39,10 +40,10 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();

configuration.addAllowedOriginPattern("https://10mm.today");
configuration.addAllowedOriginPattern(UrlConstants.PROD_DOMAIN_URL.getValue());

if (!springEnvironmentUtil.isProdProfile()) {
configuration.addAllowedOriginPattern("http://localhost:3000");
configuration.addAllowedOriginPattern(UrlConstants.LOCAL_DOMAIN_URL.getValue());
}

configuration.addAllowedHeader("*");
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/depromeet/global/util/SpringEnvironmentUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.depromeet.global.util;

import com.depromeet.global.common.constants.EnvironmentConstants;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
Expand All @@ -12,25 +13,25 @@
public class SpringEnvironmentUtil {
private final Environment environment;

public static final String PROD = "prod";
public static final String DEV = "dev";
public static final String LOCAL = "local";

private final List<String> PROD_AND_DEV = List.of(PROD, DEV);
private final List<String> PROD_AND_DEV =
List.of(EnvironmentConstants.PROD.getValue(), EnvironmentConstants.DEV.getValue());

public String getCurrentProfile() {
return getActiveProfiles()
.filter(profile -> profile.equals(PROD) || profile.equals(DEV))
.filter(
profile ->
profile.equals(EnvironmentConstants.PROD.getValue())
|| profile.equals(EnvironmentConstants.DEV.getValue()))
.findFirst()
.orElse(LOCAL);
.orElse(EnvironmentConstants.LOCAL.getValue());
}

public Boolean isProdProfile() {
return getActiveProfiles().anyMatch(PROD::equals);
return getActiveProfiles().anyMatch(EnvironmentConstants.PROD.getValue()::equals);
}

public Boolean isDevProfile() {
return getActiveProfiles().anyMatch(DEV::equals);
return getActiveProfiles().anyMatch(EnvironmentConstants.DEV.getValue()::equals);
}

public Boolean isProdAndDevProfile() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.depromeet.global.util;

import static com.depromeet.global.util.SpringEnvironmentUtil.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import com.depromeet.global.common.constants.EnvironmentConstants;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -17,9 +17,9 @@ class SpringEnvironmentUtilTest {

@InjectMocks private SpringEnvironmentUtil springEnvironmentUtil;

private final String[] PROD_ARRAY = new String[] {PROD};
private final String[] DEV_ARRAY = new String[] {DEV};
private final String[] LOCAL_ARRAY = new String[] {LOCAL};
private final String[] PROD_ARRAY = new String[] {EnvironmentConstants.PROD.getValue()};
private final String[] DEV_ARRAY = new String[] {EnvironmentConstants.DEV.getValue()};
private final String[] LOCAL_ARRAY = new String[] {EnvironmentConstants.LOCAL.getValue()};

@Test
void 상용_환경이라면_isProdProfile은_true를_반환한다() {
Expand Down Expand Up @@ -88,7 +88,8 @@ class SpringEnvironmentUtilTest {

// when
// then
assertEquals(springEnvironmentUtil.getCurrentProfile(), PROD);
assertEquals(
springEnvironmentUtil.getCurrentProfile(), EnvironmentConstants.PROD.getValue());
}

@Test
Expand All @@ -98,7 +99,8 @@ class SpringEnvironmentUtilTest {

// when
// then
assertEquals(springEnvironmentUtil.getCurrentProfile(), DEV);
assertEquals(
springEnvironmentUtil.getCurrentProfile(), EnvironmentConstants.DEV.getValue());
}

@Test
Expand All @@ -108,6 +110,7 @@ class SpringEnvironmentUtilTest {

// when
// then
assertEquals(springEnvironmentUtil.getCurrentProfile(), LOCAL);
assertEquals(
springEnvironmentUtil.getCurrentProfile(), EnvironmentConstants.LOCAL.getValue());
}
}

0 comments on commit 938a63b

Please sign in to comment.