Skip to content

Commit

Permalink
Introduce spring config (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavetok authored Oct 5, 2023
1 parent 882aeda commit b8f247d
Show file tree
Hide file tree
Showing 45 changed files with 268 additions and 177 deletions.
17 changes: 16 additions & 1 deletion .dx/tasks/solution/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
recurse: true

- name: Build spec
vars:
config_files:
turing: application.yaml
church: application.conf
config_mapping_modes:
turing: spring_config
church: lightbend_config
ansible.builtin.command:
cmd: >
docker compose
Expand All @@ -27,10 +34,18 @@
SCHEMA_IMAGE: "{{ schema_images.postgres }}"
FOO_TAG: "{{ hostvars.app.image_cids.foo[:7] }}"
FOO_IMAGE: "{{ app_images.foo }}"
CONFIG_FILE_NAME: "{{ config_files[prefs] }}"
CONFIG_MAPPING_MODE: "{{ config_mapping_modes[prefs] }}"
changed_when: true

- name: Build conf
vars:
config_extensions:
turing: yaml
church: conf
ansible.builtin.command:
cmd: cp basis/{{ prefs }}.conf target/context/bezmen.conf
cmd: >
cp basis/{{ prefs }}.{{ config_extensions[prefs] }}
target/context/application.{{ config_extensions[prefs] }}
chdir: "{{ playbook_dir }}/../solutions"
changed_when: true
5 changes: 0 additions & 5 deletions apps/README.adoc

This file was deleted.

8 changes: 4 additions & 4 deletions apps/foo/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ FROM eclipse-temurin:17-jdk-alpine
RUN addgroup -S bezmen && adduser -S bezmen -G bezmen
USER bezmen:bezmen
WORKDIR /home/bezmen
COPY libs foo/libs
COPY app-foo*.jar foo/app.jar
COPY libs libs
COPY app*.jar app.jar
ENTRYPOINT [ \
"java", "-cp", "foo/app.jar:foo/libs/*", \
"-Dconfig.file=foo/application.conf", \
"java", "-cp", "app.jar:libs/*", \
"-Dconfig.file=${CONFIG_FILE_NAME}", \
"smecalculus.bezmen.construction.App" \
]
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import smecalculus.bezmen.core.SepulkaConverter;
import smecalculus.bezmen.core.SepulkaConverterImpl;
import smecalculus.bezmen.core.SepulkaService;
Expand All @@ -16,7 +17,7 @@
import smecalculus.bezmen.messaging.SepulkaMsgMapper;
import smecalculus.bezmen.messaging.SepulkaMsgMapperImpl;
import smecalculus.bezmen.messaging.client.SepulkaClient;
import smecalculus.bezmen.messaging.springwebmvc.SepulkaController;
import smecalculus.bezmen.messaging.springmvc.SepulkaController;
import smecalculus.bezmen.storage.SepulkaDao;
import smecalculus.bezmen.storage.SepulkaDaoMyBatis;
import smecalculus.bezmen.storage.SepulkaDaoSpringData;
Expand All @@ -27,6 +28,7 @@
import smecalculus.bezmen.validation.EdgeValidator;

@Import({ConfigBeans.class, ValidationBeans.class, MessagingBeans.class, StorageBeans.class})
@PropertySource("classpath:application.properties")
@Configuration(proxyBeanMethods = false)
public class App {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package smecalculus.bezmen.messaging.springwebmvc;
package smecalculus.bezmen.messaging.springmvc;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import smecalculus.bezmen.messaging.SepulkaMsgMapper;
import smecalculus.bezmen.messaging.SepulkaMsgMapperImpl;
import smecalculus.bezmen.messaging.client.SepulkaClient;
import smecalculus.bezmen.messaging.springwebmvc.SepulkaController;
import smecalculus.bezmen.messaging.springmvc.SepulkaController;
import smecalculus.bezmen.validation.EdgeValidator;

@Import(ValidationBeans.class)
Expand Down
3 changes: 0 additions & 3 deletions docs/pipeline.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,4 @@
. Релизная сборка
. Параллелизация модульных тестов
. Построение дерева хешей (взглянуть критически)
. Версионирование библиотек и приложений (взглянуть критически)
. Локальные проверки в удаленном репозитории
. Чистка артефактов в удаленном репозитории
. Поддержка нескольких версий Java
19 changes: 9 additions & 10 deletions libs/essentials/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</dependency>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--VALIDATION-->
<dependency>
<groupId>org.hibernate.validator</groupId>
Expand All @@ -48,6 +38,15 @@
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<optional>true</optional>
</dependency>
<!--LOGGING-->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package smecalculus.bezmen.configuration;

import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.Environment;

public record ConfigKeeperSpringConfig(Environment environment) implements ConfigKeeper {

@Override
public <T> T read(String key, Class<T> type) {
Binder binder = Binder.get(environment);
return binder.bind(key, type).get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package smecalculus.bezmen.configuration;

public enum ConfigMappingMode {
LIGHTBEND_CONFIG,
SPRING_CONFIG
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package smecalculus.bezmen.configuration;

public enum ConfigMode {
public enum ConfigProtocolMode {
FILE_SYSTEM
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package smecalculus.bezmen.construction;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Conditional;
import smecalculus.bezmen.configuration.ConfigMappingMode;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Conditional(ConfigMappingModeCondition.class)
public @interface ConditionalOnConfigMappingMode {
ConfigMappingMode value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package smecalculus.bezmen.construction;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Conditional;
import smecalculus.bezmen.configuration.ConfigProtocolMode;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Conditional(ConfigProtocolModeCondition.class)
public @interface ConditionalOnConfigProtocolMode {
ConfigProtocolMode value();
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
package smecalculus.bezmen.construction;

import static smecalculus.bezmen.configuration.ConfigMappingMode.LIGHTBEND_CONFIG;
import static smecalculus.bezmen.configuration.ConfigMappingMode.SPRING_CONFIG;
import static smecalculus.bezmen.configuration.ConfigProtocolMode.FILE_SYSTEM;

import com.typesafe.config.ConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import smecalculus.bezmen.configuration.ConfigKeeper;
import smecalculus.bezmen.configuration.ConfigKeeperLightbendConfig;
import smecalculus.bezmen.configuration.ConfigMode;
import smecalculus.bezmen.configuration.ConfigKeeperSpringConfig;

@Configuration(proxyBeanMethods = false)
public class ConfigBeans {

@Bean
ConfigKeeper configKeeper() {
String configMode = System.getProperty("bezmen.config.mode", ConfigMode.FILE_SYSTEM.name());
return switch (ConfigMode.valueOf(configMode.toUpperCase())) {
case FILE_SYSTEM -> new ConfigKeeperLightbendConfig(ConfigFactory.load());
};
@ConditionalOnConfigProtocolMode(FILE_SYSTEM)
@ConditionalOnConfigMappingMode(LIGHTBEND_CONFIG)
ConfigKeeper configKeeperLightbendConfig() {
return new ConfigKeeperLightbendConfig(ConfigFactory.load());
}

@Bean
@ConditionalOnConfigProtocolMode(FILE_SYSTEM)
@ConditionalOnConfigMappingMode(SPRING_CONFIG)
ConfigKeeper configKeeperSpringConfig(Environment environment) {
return new ConfigKeeperSpringConfig(environment);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package smecalculus.bezmen.construction;

import static org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase.REGISTER_BEAN;

import java.util.Map;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.type.AnnotatedTypeMetadata;
import smecalculus.bezmen.configuration.ConfigMappingMode;

class ConfigMappingModeCondition implements ConfigurationCondition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes =
metadata.getAnnotationAttributes(ConditionalOnConfigMappingMode.class.getName());
ConfigMappingMode expectedMode = (ConfigMappingMode) attributes.get("value");
String actualMode = context.getEnvironment()
.getProperty("bezmen.config.mapping.mode", ConfigMappingMode.LIGHTBEND_CONFIG.name());
return expectedMode.name().equalsIgnoreCase(actualMode);
}

@Override
public ConfigurationPhase getConfigurationPhase() {
return REGISTER_BEAN;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package smecalculus.bezmen.construction;

import static org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase.REGISTER_BEAN;

import java.util.Map;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.type.AnnotatedTypeMetadata;
import smecalculus.bezmen.configuration.ConfigProtocolMode;

class ConfigProtocolModeCondition implements ConfigurationCondition {

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> attributes =
metadata.getAnnotationAttributes(ConditionalOnConfigProtocolMode.class.getName());
ConfigProtocolMode expectedMode = (ConfigProtocolMode) attributes.get("value");
String actualMode = context.getEnvironment()
.getProperty("bezmen.config.protocol.mode", ConfigProtocolMode.FILE_SYSTEM.name());
return expectedMode.name().equalsIgnoreCase(actualMode);
}

@Override
public ConfigurationPhase getConfigurationPhase() {
return REGISTER_BEAN;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package smecalculus.bezmen.configuration;

import static java.util.Collections.singleton;
import static smecalculus.bezmen.configuration.MessageMappingMode.SPRING_MVC;

import java.util.Set;

public class MessageMappingPropsEg {
public static class Builders {
public static MessageMappingProps.Builder messageMappingProps() {
return MessageMappingProps.builder().mappingModes(singleton(SPRING_MVC));
return MessageMappingProps.builder().mappingModes(Set.of(SPRING_MVC));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public interface MessagingCfgMapper {

@Mapping(source = "protocol", target = "protocolProps")
@Mapping(source = "mapping", target = "mappingProps")
MessagingProps toDomain(MessagingPropsCfg messagingPropsCfg);
MessagingProps toDomain(MessagingPropsCfg propsCfg);

@Mapping(source = "modes", target = "protocolModes")
MessagingProtocolProps toDomain(MessagingProtocolPropsCfg protocolPropsCfg);
MessagingProtocolProps toDomain(MessagingProtocolPropsCfg propsCfg);

@Mapping(source = "modes", target = "mappingModes")
MessageMappingProps toDomain(MessageMappingPropsCfg mappingPropsCfg);
MessageMappingProps toDomain(MessageMappingPropsCfg propsCfg);

default MessagingProtocolMode toProtocolMode(String value) {
return MessagingProtocolMode.valueOf(value.toUpperCase());
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
package smecalculus.bezmen.construction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import smecalculus.bezmen.configuration.ConfigKeeper;
import smecalculus.bezmen.configuration.MessagingCfgMapper;
import smecalculus.bezmen.configuration.MessagingCfgMapperImpl;
import smecalculus.bezmen.configuration.MessagingConfig;
import smecalculus.bezmen.configuration.MessagingConfigImpl;
import smecalculus.bezmen.configuration.MessagingProps;
import smecalculus.bezmen.configuration.MessagingPropsCfg;
import smecalculus.bezmen.validation.EdgeValidator;

@PropertySource("classpath:messaging.properties")
@Configuration(proxyBeanMethods = false)
public class MessagingConfigBeans {

private static final Logger LOG = LoggerFactory.getLogger(MessagingConfigBeans.class);

@Bean
MessagingCfgMapper messagingCfgMapper() {
return new MessagingCfgMapperImpl();
}

@Bean
MessagingConfig messagingConfig(ConfigKeeper keeper, EdgeValidator validator, MessagingCfgMapper mapper) {
return new MessagingConfigImpl(keeper, validator, mapper);
}

@Bean
MessagingProps messagingProps(MessagingConfig config) {
return config.getMessagingProps();
MessagingProps messagingProps(ConfigKeeper keeper, EdgeValidator validator, MessagingCfgMapper mapper) {
MessagingPropsCfg propsCfg = keeper.read("bezmen.messaging", MessagingPropsCfg.class);
validator.validate(propsCfg);
LOG.info("Read {}", propsCfg);
return mapper.toDomain(propsCfg);
}
}
2 changes: 2 additions & 0 deletions libs/messaging/src/main/resources/messaging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bezmen.messaging.protocol.modes[0]=http
bezmen.messaging.mapping.modes[0]=spring_mvc
Loading

0 comments on commit b8f247d

Please sign in to comment.