diff --git a/.github/workflows/develop-deploy.yml b/.github/workflows/develop-deploy.yml index 90c0b48..0bc1edd 100644 --- a/.github/workflows/develop-deploy.yml +++ b/.github/workflows/develop-deploy.yml @@ -19,18 +19,25 @@ jobs: distribution: 'temurin' # 환경 변수 파일 생성 - - name: Set environment values for main application + - name: Set environment values for common run: | cd ./src/main/resources - touch ./env.properties - echo "${{ secrets.ENV }}" > ./env.properties + touch ./env-common.properties + echo "${{ secrets.ENV_COMMON }}" > ./env-common.properties + shell: bash + + - name: Set environment values for prod + run: | + cd ./src/main/resources + touch ./env-prod.properties + echo "${{ secrets.ENV_PROD }}" > ./env-prod.properties shell: bash - name: Set environment values for test run: | - cd ./src/test/resources - touch ./env.properties - echo "${{ secrets.TEST_ENV }}" > ./env.properties + cd ./src/main/resources + touch ./env-test.properties + echo "${{ secrets.TEST_ENV }}" > ./env-test.properties shell: bash # Gradle에 권한 부여 @@ -49,7 +56,8 @@ jobs: - name: Make zip file run: | mkdir develop-deploy - cp ./src/main/resources/env.properties ./develop-deploy/.env + cp ./src/main/resources/env-common.properties ./develop-deploy/.env.common + cp ./src/main/resources/env-prod.properties ./develop-deploy/.env.prod cp ./docker-compose.blue.yml ./develop-deploy/ cp ./docker-compose.green.yml ./develop-deploy/ cp ./appspec.yml ./develop-deploy/ diff --git a/build.gradle b/build.gradle index 583ea2b..8579c2e 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,10 @@ java { } configurations { + all { + exclude group: "commons-logging", module: "commons-logging" + exclude group: "org.slf4j", module: "slf4j-simple" + } compileOnly { extendsFrom annotationProcessor } @@ -53,6 +57,7 @@ dependencies { // Test testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' + implementation 'it.ozimov:embedded-redis:0.7.3' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' // Spotify diff --git a/docker-compose.blue.yml b/docker-compose.blue.yml index b742e7a..f86c3bb 100644 --- a/docker-compose.blue.yml +++ b/docker-compose.blue.yml @@ -6,6 +6,11 @@ services: ports: - "8081:8080" container_name: spring-blue + environment: + - SPRING_PROFILES_ACTIVE=prod + env_file: + - .env.common + - .env.prod networks: - munecting-network diff --git a/docker-compose.green.yml b/docker-compose.green.yml index e95614b..1c51189 100644 --- a/docker-compose.green.yml +++ b/docker-compose.green.yml @@ -6,6 +6,11 @@ services: ports: - "8082:8080" container_name: spring-green + environment: + - SPRING_PROFILES_ACTIVE=prod + env_file: + - .env.common + - .env.prod networks: - munecting-network diff --git a/src/main/java/com/munecting/api/global/config/DevConfig.java b/src/main/java/com/munecting/api/global/config/DevConfig.java new file mode 100644 index 0000000..d97f011 --- /dev/null +++ b/src/main/java/com/munecting/api/global/config/DevConfig.java @@ -0,0 +1,11 @@ +package com.munecting.api.global.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.context.annotation.PropertySource; + +@Configuration +@Profile("dev") +@PropertySource(value = {"classpath:env-common.properties", "classpath:env-dev.properties"}) +public class DevConfig { +} diff --git a/src/main/java/com/munecting/api/global/config/EmbeddedRedisConfig.java b/src/main/java/com/munecting/api/global/config/EmbeddedRedisConfig.java new file mode 100644 index 0000000..36995a2 --- /dev/null +++ b/src/main/java/com/munecting/api/global/config/EmbeddedRedisConfig.java @@ -0,0 +1,47 @@ +package com.munecting.api.global.config; + +import jakarta.annotation.PostConstruct; +import jakarta.annotation.PreDestroy; +import org.redisson.Redisson; +import org.redisson.api.RedissonClient; +import org.redisson.config.Config; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import redis.embedded.RedisServer; + +@Configuration +@Profile("test") +public class EmbeddedRedisConfig { + + @Value("${spring.data.redis.host}") + private String host; + + @Value("${spring.data.redis.port}") + private int port; + + private RedisServer redisServer; + + @PostConstruct + public void startEmbeddedRedis() { + redisServer = new RedisServer(port); + redisServer.start(); + } + + @PreDestroy + public void stopEmbeddedRedis() { + if (redisServer != null) { + redisServer.stop(); + } + } + + @Bean + public RedissonClient redissonClient() { + Config config = new Config(); + config.useSingleServer() + .setAddress("redis://" + host + ":" + port); + return Redisson.create(config); + } + +} diff --git a/src/main/java/com/munecting/api/global/config/PropertyConfig.java b/src/main/java/com/munecting/api/global/config/ProdConfig.java similarity index 56% rename from src/main/java/com/munecting/api/global/config/PropertyConfig.java rename to src/main/java/com/munecting/api/global/config/ProdConfig.java index 3bee10b..582443b 100644 --- a/src/main/java/com/munecting/api/global/config/PropertyConfig.java +++ b/src/main/java/com/munecting/api/global/config/ProdConfig.java @@ -1,12 +1,12 @@ package com.munecting.api.global.config; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySources; @Configuration -@PropertySources({ - @PropertySource("classpath:env.properties") -}) -public class PropertyConfig { +@Profile("prod") +@PropertySource(value = {"classpath:env-common.properties","classpath:env-prod.properties"}) +public class ProdConfig { } diff --git a/src/main/java/com/munecting/api/global/config/RedisConfig.java b/src/main/java/com/munecting/api/global/config/RedisConfig.java index 730be95..592dbbb 100644 --- a/src/main/java/com/munecting/api/global/config/RedisConfig.java +++ b/src/main/java/com/munecting/api/global/config/RedisConfig.java @@ -3,6 +3,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; @@ -10,6 +11,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration +@Profile({"prod","dev"}) public class RedisConfig { @Value("${spring.data.redis.host}") diff --git a/src/main/java/com/munecting/api/global/config/RedissonConfig.java b/src/main/java/com/munecting/api/global/config/RedissonConfig.java index 770ef61..544ce44 100644 --- a/src/main/java/com/munecting/api/global/config/RedissonConfig.java +++ b/src/main/java/com/munecting/api/global/config/RedissonConfig.java @@ -6,8 +6,10 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; @Configuration +@Profile({"prod","dev"}) public class RedissonConfig { @Value("${spring.data.redis.host}") @@ -29,4 +31,5 @@ private Config createRedissonConfig() { config.useSingleServer().setAddress(REDISSON_HOST_PREFIX + redisHost + ":" + redisPort); return config; } + } diff --git a/src/main/java/com/munecting/api/global/config/TestConfig.java b/src/main/java/com/munecting/api/global/config/TestConfig.java new file mode 100644 index 0000000..7645724 --- /dev/null +++ b/src/main/java/com/munecting/api/global/config/TestConfig.java @@ -0,0 +1,12 @@ +package com.munecting.api.global.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.PropertySources; + +@Configuration +@Profile("test") +@PropertySource(value = {"classpath:env-common.properties", "classpath:env-test.properties"}) +public class TestConfig { +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index bc20a06..ec0e92f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,6 +4,8 @@ server: spring : application: name: munectingV4 + profiles: + active: dev mvc: path match: matching-strategy: ant_path_matcher @@ -17,32 +19,16 @@ spring : format_sql: true jdbc: timezone: Asia/Seoul - - datasource: - url: jdbc:${DATASOURCE}://${DB_HOST}:${DB_PORT}/${DB_NAME} - username: ${DB_USERNAME} - password: ${DB_PASSWORD} - driver-class-name: org.postgresql.Driver - hikari: - initialization-fail-timeout: 3600 - - data: - redis: - host: ${REDIS_HOST} - port: 6379 -# password: 1234 - - security: cors: - allowed-origins: ${CORS_ALLOWED_ORIGINS:*} - allowed-methods: ${CORS_ALLOWED_METHODS:GET,POST,PUT,PATCH,DELETE} - allowed-headers: ${CORS_ALLOWED_HEADERS:*} + allowed-origins: "*" + allowed-methods: GET,POST,PUT,PATCH,DELETE + allowed-headers: "*" path-pattern: "/**" auth: - header: ${AUTH_HEADER:Authorization} - prefix: ${AUTH_PREFIX:Bearer } + header: Authorization + prefix: Bearer oauth: kakao: @@ -62,14 +48,12 @@ spring : maxRequestSize: 10MB # 한 번에 최대 업로드 가능 용량 resolve-lazily: true - cloud: aws: s3: bucket: munecting path: profile-image: profileImage - region: static: ap-northeast-2 stack: @@ -91,3 +75,83 @@ jwt: kakao-map: api-key: ${KAKAO_MAP_API_KEY} + +--- +spring: + config: + activate: + on-profile: prod + + datasource: + url: jdbc:${DATASOURCE}://${DB_HOST}:${DB_PORT}/${DB_NAME} + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + driver-class-name: org.postgresql.Driver + hikari: + initialization-fail-timeout: 3600 + jpa: + hibernate: + ddl-auto: validate + data: + redis: + host: ${REDIS_HOST} + port: 6379 + +--- +spring: + config: + activate: + on-profile: dev + datasource: + driver-class-name: org.postgresql.Driver + url: jdbc:postgresql://localhost:5432/munecting + username: ${DB_USERNAME} + password: ${DB_PASSWORD} + hikari: + initialization-fail-timeout: 3600 + jpa: + hibernate: + ddl-auto: update + data: + redis: + host: 127.0.0.1 + port: 6379 +cloud: + aws: + s3: + bucket: munecting-dev + path: + profile-image: profileImage +--- +spring: + config: + activate: + on-profile: test + datasource: + driver-class-name: org.h2.Driver + url: jdbc:h2:mem:~/munecting + username: sa + password: + jpa: + database-platform: org.hibernate.dialect.H2Dialect + hibernate: + ddl-auto: create + properties: + hibernate: + dialect: org.hibernate.dialect.H2Dialect + show_sql: true + format_sql: true + h2: + console: + enabled: true + path: /h2-console + data: + redis: + host: localhost + port: 6378 +cloud: + aws: + s3: + bucket: munecting-test + path: + profile-image: profileImage \ No newline at end of file diff --git a/src/test/java/com/munecting/api/MunectingServerApplicationTests.java b/src/test/java/com/munecting/api/MunectingServerApplicationTests.java index e110f35..2557a00 100644 --- a/src/test/java/com/munecting/api/MunectingServerApplicationTests.java +++ b/src/test/java/com/munecting/api/MunectingServerApplicationTests.java @@ -1,4 +1,4 @@ -/* + package com.munecting.api; import org.junit.jupiter.api.Test; @@ -14,4 +14,4 @@ void contextLoads() { } } -*/ + diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml deleted file mode 100644 index 9958a91..0000000 --- a/src/test/resources/application-test.yml +++ /dev/null @@ -1,82 +0,0 @@ -server: - port: ${SERVER_PORT} - -spring : - config: - activate: - on-profile: "test" - - application: - name: munectingV4 - mvc: - path match: - matching-strategy: ant_path_matcher - jpa: - database-platform: org.hibernate.dialect.H2Dialect - - hibernate: - ddl-auto: create - - properties: - hibernate: - dialect: org.hibernate.dialect.H2Dialect - show_sql: true - format_sql: true - - # H2 Database 설정 - datasource: - driver-class-name: org.h2.Driver - url: jdbc:h2:mem:test;MODE=PostgreSQL # H2 DB 연결 주소 (In-Memory Mode) - #url: 'jdbc:h2:~/test' # H2 DB 연결 주소 (Embedded Mode) - username: ${DATASOURCE_USERNAME:sa} - password: ${DATASOURCE_PASSWORD} - - # H2 Console 설정 - h2: - console: # H2 DB를 웹에서 관리할 수 있는 기능 - enabled: true # H2 Console 사용 여부 - path: /h2-console # H2 Console 접속 주소 - - data: - redis: - host: ${REDIS_HOST} - port: 6379 - # password: 1234 - - security: - cors: - allowed-origins: ${CORS_ALLOWED_ORIGINS:*} - allowed-methods: ${CORS_ALLOWED_METHODS:GET,POST,PUT,PATCH,DELETE} - allowed-headers: ${CORS_ALLOWED_HEADERS:*} - allow-credentials: ${CORS_ALLOW_CREDENTIALS:true} - path-pattern: "/**" - - auth: - header: ${AUTH_HEADER:Authorization} - prefix: ${AUTH_PREFIX:Bearer } - - oauth: - kakao: - public-key-info: https://kauth.kakao.com/.well-known/jwks.json - issuer: https://kauth.kakao.com - audience: ${KAKAO_APP_ID}} - apple: - public-key-url: https://appleid.apple.com/auth/keys - issuer: https://appleid.apple.com - audience: ${APPLE_APP_ID} - google: - client-id: ${GOOGLE_CLIENT_ID} - -spotify: - client-id: ${CLIENT_ID} - client-secret: ${CLIENT_SECRET} - -jwt: - secret: ${JWT_SECRET_KEY} - access: - expiration: ${JWT_ACCESS_TOKEN_EXPIRATION} - refresh: - expiration: ${JWT_REFRESH_TOKEN_EXPIRATION} - -kakao-map: - api-key: ${KAKAO_MAP_API_KEY}