Merge list property

This commit is contained in:
woozu.shin 2021-10-18 14:35:16 +09:00
parent d330bc8df9
commit c3e6e72e55
7 changed files with 85 additions and 13 deletions

View File

@ -1,16 +1,19 @@
package com.kakaoent.cpdd.config.client1.configuration.properties;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Setter
@Getter
@Component
@ConfigurationProperties(prefix = "apis")
public class ApiProperties {
private String gateway; // client config
private String point; // server config
public class ApiProperties implements ConfigApiProperties {
List<ApiProperty> units;
public ApiProperties(List<ConfigApiProperties> apiProperties) {
units = new ArrayList<>();
apiProperties.forEach(e -> units.addAll(e.getUnits()));
}
}

View File

@ -0,0 +1,18 @@
package com.kakaoent.cpdd.config.client1.configuration.properties;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Setter
@Getter
@Component
@ConfigurationProperties(prefix = "api.clientconfig")
public class ClientConfigApiProperties implements ConfigApiProperties {
private List<ApiProperty> units;
}

View File

@ -0,0 +1,26 @@
package com.kakaoent.cpdd.config.client1.configuration.properties;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
public interface ConfigApiProperties {
List<ApiProperty> getUnits();
@Getter
@Setter
class ApiProperty {
private String unitName;
private String url;
}
default ApiProperty find(String unitName) {
return getUnits().stream()
.filter(e -> e.getUnitName().equals(unitName))
.findFirst()
.orElseThrow(IllegalArgumentException::new);
}
}

View File

@ -0,0 +1,18 @@
package com.kakaoent.cpdd.config.client1.configuration.properties;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Setter
@Getter
@Component
@ConfigurationProperties(prefix = "api.serverconfig")
public class ServerConfigApiProperties implements ConfigApiProperties {
private List<ApiProperty> units;
}

View File

@ -18,12 +18,12 @@ public class PropertyAPIController {
@GetMapping("/apis/gateway")
public Mono<String> getGatewayUrl() {
return Mono.just(apiProperties.getGateway());
return Mono.just(apiProperties.find("gateway").getUrl());
}
@GetMapping("/apis/point")
public Mono<String> getPointAPIUrl() {
return Mono.just(apiProperties.getPoint());
return Mono.just(apiProperties.find("point").getUrl());
}
}

View File

@ -3,5 +3,8 @@ spring:
activate:
on-profile: dev
apis:
gateway: http://gw.application-dev.com
api:
clientconfig:
units:
- unit-name: gateway
url: http://gw.application-dev.com

View File

@ -2,5 +2,9 @@ spring:
config:
activate:
on-profile: prod
apis:
gateway: http://gw.application.com
api:
clientconfig:
units:
- unit-name: gateway
url: http://gw.application.com