Skip to content

Commit

Permalink
chore: NCP Object Storage 설정 (#100)
Browse files Browse the repository at this point in the history
* move: redis config 패키지 이동

* chore: PropertiesConfig 추가와 configuration-processor 의존성 추가

* chore: AWS 의존성 추가

* chore: Storage Config 생성

* chore: storage.yml 추가

* refactor: RedisProperties 적용

* style: spotless

* refactor: properties record 적용
  • Loading branch information
kdomo authored Jan 4, 2024
1 parent 13aa00e commit fd376ea
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 22 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ dependencies {
runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'

// Validation
implementation 'org.springframework.boot:spring-boot-starter-validation'

// Actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'

// Spring Security
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.security:spring-security-test'
Expand All @@ -52,6 +54,9 @@ dependencies {
// Redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

// AWS
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

testImplementation 'org.springframework.boot:spring-boot-starter-test'

// test lombok
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.depromeet.infra.config.properties;

import com.depromeet.infra.config.redis.RedisProperties;
import com.depromeet.infra.config.storage.StorageProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@EnableConfigurationProperties({StorageProperties.class, RedisProperties.class})
@Configuration
public class PropertiesConfig {}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.depromeet.infra.config;
package com.depromeet.infra.config.redis;

import java.time.Duration;
import org.springframework.beans.factory.annotation.Value;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
Expand All @@ -12,21 +12,16 @@

@EnableRedisRepositories
@Configuration
@RequiredArgsConstructor
public class RedisConfig {
@Value("${spring.data.redis.host}")
private String redisHost;

@Value("${spring.data.redis.port}")
private int redisPort;

@Value("${spring.data.redis.password}")
private String redisPassword;
private final RedisProperties redisProperties;

@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisConfig =
new RedisStandaloneConfiguration(redisHost, redisPort);
if (!redisPassword.isBlank()) redisConfig.setPassword(redisPassword);
new RedisStandaloneConfiguration(redisProperties.host(), redisProperties.port());
if (!redisProperties.password().isBlank())
redisConfig.setPassword(redisProperties.password());
LettuceClientConfiguration clientConfig =
LettuceClientConfiguration.builder()
.commandTimeout(Duration.ofSeconds(1))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.depromeet.infra.config.redis;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "spring.data.redis")
public record RedisProperties(String host, int port, String password) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.depromeet.infra.config.storage;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class StorageConfig {
private final StorageProperties storageProperties;

@Bean
public AmazonS3 amazonS3() {
AWSCredentials credentials =
new BasicAWSCredentials(
storageProperties.accessKey(), storageProperties.secretKey());
return AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(storageProperties.region())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.depromeet.infra.config.storage;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "storage")
public record StorageProperties(String accessKey, String secretKey, String region, String bucket) {}
9 changes: 9 additions & 0 deletions src/main/resources/application-storage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
config:
activate:
on-profile: "storage"
storage:
accessKey: ${STORAGE_ACCESS_KEY:}
secretKey: ${STORAGE_SECRET_KEY:}
region: ${STORAGE_REGION:}
bucket: ${STORAGE_BUCKET:}
8 changes: 4 additions & 4 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
spring:
profiles:
group:
test: "test"
local: "local, datasource, redis"
dev: "dev, datasource, redis, actuator"
prod: "prod, datasource, redis, actuator"
test: "test, redis, storage"
local: "local, datasource, redis, storage"
dev: "dev, datasource, redis, storage, actuator"
prod: "prod, datasource, redis, storage, actuator"

swagger:
version: 0.0.1
Expand Down
6 changes: 0 additions & 6 deletions src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,3 @@ spring:
on-profile: "test"
datasource:
url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MODE=MYSQL

data:
redis:
host: localhost
port: 6379
password:

0 comments on commit fd376ea

Please sign in to comment.