From 9e0d63f4876e35f52f7f5ae72bd7a0a9ae3b6294 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Fri, 23 Aug 2024 14:37:12 -0700 Subject: [PATCH] Replaced configuration classes to use records instead --- .../switcherapi/ac/SwitcherAcApplication.java | 2 + .../ac/config/ConfigProperties.java | 45 +++++--------- .../ac/config/OpenAPIConfiguration.java | 16 ++--- .../switcherapi/ac/config/SwitcherConfig.java | 62 ++++++++----------- .../ac/controller/ApiController.java | 4 +- .../controller/SwitcherRelayController.java | 2 +- 6 files changed, 53 insertions(+), 78 deletions(-) diff --git a/src/main/java/com/github/switcherapi/ac/SwitcherAcApplication.java b/src/main/java/com/github/switcherapi/ac/SwitcherAcApplication.java index 683e682..f205a83 100644 --- a/src/main/java/com/github/switcherapi/ac/SwitcherAcApplication.java +++ b/src/main/java/com/github/switcherapi/ac/SwitcherAcApplication.java @@ -6,11 +6,13 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; import org.springframework.context.annotation.ComponentScan; import static com.github.switcherapi.ac.config.SwitcherFeatures.checkSwitchers; @SpringBootApplication +@ConfigurationPropertiesScan @ComponentScan(basePackages = { "com.github.switcherapi.ac" }) @Slf4j public class SwitcherAcApplication implements CommandLineRunner { diff --git a/src/main/java/com/github/switcherapi/ac/config/ConfigProperties.java b/src/main/java/com/github/switcherapi/ac/config/ConfigProperties.java index cedc7d9..d5724ed 100644 --- a/src/main/java/com/github/switcherapi/ac/config/ConfigProperties.java +++ b/src/main/java/com/github/switcherapi/ac/config/ConfigProperties.java @@ -1,39 +1,22 @@ package com.github.switcherapi.ac.config; -import lombok.Generated; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; -import lombok.Getter; -import lombok.Setter; - -@Configuration @ConfigurationProperties(prefix = "service.docs") -@Getter -@Setter -@Generated -public class ConfigProperties { - - private String title; - private String description; - private String version; - private String releaseTime; - private String url; - private License license; - private Contact contact; - - @Getter - @Setter - public static class License { - private String type; - private String url; - } +public record ConfigProperties( + String title, + String description, + String version, + String releaseTime, + String url, + License license, + Contact contact) { - @Getter - @Setter - public static class Contact { - private String author; - private String email; - } + public record License( + String type, + String url) { } + public record Contact( + String author, + String email) { } } diff --git a/src/main/java/com/github/switcherapi/ac/config/OpenAPIConfiguration.java b/src/main/java/com/github/switcherapi/ac/config/OpenAPIConfiguration.java index 268e5f6..caec2ac 100644 --- a/src/main/java/com/github/switcherapi/ac/config/OpenAPIConfiguration.java +++ b/src/main/java/com/github/switcherapi/ac/config/OpenAPIConfiguration.java @@ -27,7 +27,7 @@ public OpenAPIConfiguration(ConfigProperties configProperties) { @Bean public OpenAPI customOpenAPI() { var openApi = new OpenAPI() - .addServersItem(new Server().url(configProperties.getUrl())) + .addServersItem(new Server().url(configProperties.url())) .info(getInfo()); addSecurity(openApi); @@ -36,23 +36,23 @@ public OpenAPI customOpenAPI() { private Info getInfo() { return new Info() - .title(configProperties.getTitle()) - .description(configProperties.getDescription()) - .version(configProperties.getVersion()) + .title(configProperties.title()) + .description(configProperties.description()) + .version(configProperties.version()) .contact(getContact()) .license(getLicense()); } private License getLicense() { return new License() - .name(configProperties.getLicense().getType()) - .url(configProperties.getLicense().getUrl()); + .name(configProperties.license().type()) + .url(configProperties.license().url()); } private Contact getContact() { return new Contact() - .name(configProperties.getContact().getAuthor()) - .email(configProperties.getContact().getEmail()); + .name(configProperties.contact().author()) + .email(configProperties.contact().email()); } private void addSecurity(OpenAPI openApi) { diff --git a/src/main/java/com/github/switcherapi/ac/config/SwitcherConfig.java b/src/main/java/com/github/switcherapi/ac/config/SwitcherConfig.java index 55e6971..f94577a 100644 --- a/src/main/java/com/github/switcherapi/ac/config/SwitcherConfig.java +++ b/src/main/java/com/github/switcherapi/ac/config/SwitcherConfig.java @@ -4,46 +4,37 @@ import com.github.switcherapi.client.ContextBuilder; import com.github.switcherapi.client.SnapshotCallback; import jakarta.annotation.PostConstruct; -import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; import static com.github.switcherapi.ac.config.SwitcherFeatures.configure; import static com.github.switcherapi.ac.config.SwitcherFeatures.initializeClient; -import static com.github.switcherapi.client.SwitcherContextBase.scheduleSnapshotAutoUpdate; +import static com.github.switcherapi.ac.config.SwitcherFeatures.scheduleSnapshotAutoUpdate; @Slf4j -@Configuration @ConfigurationProperties(prefix = "switcher") -@Data -public class SwitcherConfig implements SnapshotCallback { +public record SwitcherConfig( + String url, + String apikey, + String domain, + String component, + String environment, + boolean local, + String silent, + SnapshotConfig snapshot, + String relayCode, + TruststoreConfig truststore) implements SnapshotCallback { + + record SnapshotConfig( + String autoUpdateInterval, + String location, + boolean auto) { } + + record TruststoreConfig( + String path, + String password) { } - private String url; - private String apikey; - private String domain; - private String component; - private String environment; - private boolean local; - private String silent; - private SnapshotConfig snapshot; - private String relayCode; - private TruststoreConfig truststore; - - @Data - static class SnapshotConfig { - private String autoUpdateInterval; - private String location; - private boolean auto; - } - - @Data - static class TruststoreConfig { - private String path; - private String password; - } - @PostConstruct private void configureSwitcher() { configure(ContextBuilder.builder() @@ -55,14 +46,13 @@ private void configureSwitcher() { .component(component) .local(local) .silentMode(silent) - .snapshotLocation(StringUtils.isNotBlank(snapshot.getLocation()) ? snapshot.getLocation() : null) - .snapshotAutoLoad(snapshot.isAuto()) - .truststorePath(StringUtils.isNotBlank(truststore.getPath()) ? FileUtil.getFilePathFromResource(truststore.getPath()) : null) - .truststorePassword(StringUtils.isNotBlank(truststore.getPassword()) ? truststore.getPassword() : null) - ); + .snapshotLocation(StringUtils.isNotBlank(snapshot.location()) ? snapshot.location() : null) + .snapshotAutoLoad(snapshot.auto()) + .truststorePath(StringUtils.isNotBlank(truststore.path()) ? FileUtil.getFilePathFromResource(truststore.path()) : null) + .truststorePassword(StringUtils.isNotBlank(truststore.password()) ? truststore.password() : null)); initializeClient(); - scheduleSnapshotAutoUpdate(snapshot.getAutoUpdateInterval(), this); + scheduleSnapshotAutoUpdate(snapshot.autoUpdateInterval(), this); } @Override diff --git a/src/main/java/com/github/switcherapi/ac/controller/ApiController.java b/src/main/java/com/github/switcherapi/ac/controller/ApiController.java index 86c97aa..d447f71 100644 --- a/src/main/java/com/github/switcherapi/ac/controller/ApiController.java +++ b/src/main/java/com/github/switcherapi/ac/controller/ApiController.java @@ -26,8 +26,8 @@ public ApiController(ConfigProperties configProperties) { public ResponseEntity> check() { return ResponseEntity.ok(Map.of( "status", "All good", - "version", configProperties.getVersion(), - "release_time", configProperties.getReleaseTime() + "version", configProperties.version(), + "release_time", configProperties.releaseTime() )); } diff --git a/src/main/java/com/github/switcherapi/ac/controller/SwitcherRelayController.java b/src/main/java/com/github/switcherapi/ac/controller/SwitcherRelayController.java index 569c492..a9819be 100644 --- a/src/main/java/com/github/switcherapi/ac/controller/SwitcherRelayController.java +++ b/src/main/java/com/github/switcherapi/ac/controller/SwitcherRelayController.java @@ -44,7 +44,7 @@ public SwitcherRelayController( @GetMapping(value = "/verify") public ResponseEntity verify() { - return ResponseEntity.ok(Map.of("code", switcherConfig.getRelayCode())); + return ResponseEntity.ok(Map.of("code", switcherConfig.relayCode())); } @Operation(summary = "Load new account to Switcher AC")