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

Replaced configuration classes to use records instead #283

Merged
merged 1 commit into from
Aug 23, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
62 changes: 26 additions & 36 deletions src/main/java/com/github/switcherapi/ac/config/SwitcherConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public ApiController(ConfigProperties configProperties) {
public ResponseEntity<Map<String, Object>> check() {
return ResponseEntity.ok(Map.of(
"status", "All good",
"version", configProperties.getVersion(),
"release_time", configProperties.getReleaseTime()
"version", configProperties.version(),
"release_time", configProperties.releaseTime()
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public SwitcherRelayController(

@GetMapping(value = "/verify")
public ResponseEntity<Object> 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")
Expand Down