Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce spring config #149

Merged
merged 7 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=application.conf", \
"smecalculus.bezmen.construction.App" \
]
13 changes: 8 additions & 5 deletions apps/foo/src/main/java/smecalculus/bezmen/construction/App.java
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 All @@ -36,8 +38,9 @@ public static void main(String[] args) {

@Bean
@ConditionalOnMessageMappingModes(SPRING_MVC)
SepulkaController sepulkaControllerSpringWeb(SepulkaClient client, SepulkaMsgMapper mapper) {
return new SepulkaController(client, mapper);
SepulkaController sepulkaControllerSpringMvc(
EdgeValidator validator, SepulkaClient client, SepulkaMsgMapper mapper) {
return new SepulkaController(validator, client, mapper);
}

@Bean
Expand All @@ -46,8 +49,8 @@ SepulkaMsgMapper sepulkaMsgMapper() {
}

@Bean
SepulkaClient sepulkaClient(EdgeValidator validator, SepulkaService service, SepulkaConverter converter) {
return new SepulkaClientImpl(validator, service, converter);
SepulkaClient sepulkaClient(SepulkaService service, SepulkaConverter converter) {
return new SepulkaClientImpl(service, converter);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@
import smecalculus.bezmen.messaging.client.SepulkaClient;
import smecalculus.bezmen.messaging.client.SepulkaRegReq;
import smecalculus.bezmen.messaging.client.SepulkaRegRes;
import smecalculus.bezmen.validation.EdgeValidator;

public record SepulkaClientImpl(
@NonNull EdgeValidator validator, @NonNull SepulkaService service, @NonNull SepulkaConverter converter)
public record SepulkaClientImpl(@NonNull SepulkaService service, @NonNull SepulkaConverter converter)
implements SepulkaClient {

@Override
public SepulkaRegRes register(SepulkaRegReq request) {
validator.validate(request);
Sepulka sepulka = service.register(request);
return converter.toRegRes(sepulka);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package smecalculus.bezmen.messaging.springwebmvc;
package smecalculus.bezmen.messaging.springmvc;

import lombok.NonNull;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -12,12 +13,16 @@
import smecalculus.bezmen.messaging.client.SepulkaClient;
import smecalculus.bezmen.messaging.client.SepulkaRegReq;
import smecalculus.bezmen.messaging.client.SepulkaRegRes;
import smecalculus.bezmen.validation.EdgeValidator;

@RestController
@RequestMapping("sepulkas")
public record SepulkaController(SepulkaClient client, SepulkaMsgMapper mapper) {
public record SepulkaController(
@NonNull EdgeValidator validator, @NonNull SepulkaClient client, @NonNull SepulkaMsgMapper mapper) {

@PostMapping
ResponseEntity<SepulkaRegResMsg> register(@RequestBody SepulkaRegReqMsg sepulkaRegReqMsg) {
validator.validate(sepulkaRegReqMsg);
SepulkaRegReq sepulkaRegReq = mapper.toDomain(sepulkaRegReqMsg);
SepulkaRegRes sepulkaRegRes = client.register(sepulkaRegReq);
return ResponseEntity.status(HttpStatus.CREATED).body(mapper.toMsg(sepulkaRegRes));
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 All @@ -33,14 +33,15 @@ SepulkaConverter sepulkaConverter() {
}

@Bean
SepulkaClient internalClient(EdgeValidator validator, SepulkaService service, SepulkaConverterImpl converter) {
return new SepulkaClientImpl(validator, service, converter);
SepulkaClient internalClient(SepulkaService service, SepulkaConverterImpl converter) {
return new SepulkaClientImpl(service, converter);
}

@Bean
SepulkaClient externalClient(SepulkaClient internalClient) {
SepulkaClient externalClient(EdgeValidator validator, SepulkaClient internalClient) {
SepulkaMsgMapper mapper = new SepulkaMsgMapperImpl();
WebTestClient client = MockMvcWebTestClient.bindToController(new SepulkaController(internalClient, mapper))
WebTestClient client = MockMvcWebTestClient.bindToController(
new SepulkaController(validator, internalClient, mapper))
.build();
return new SepulkaClientSpringWeb(client, mapper);
}
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));
}
}
}
Loading