From a25bd9ea0c32042b8b25441494854b7cec0e219c Mon Sep 17 00:00:00 2001 From: Kama-Pushka Date: Sat, 5 Apr 2025 14:38:17 +0500 Subject: [PATCH] config and profiles --- build.gradle.kts | 4 ++ .../config/MinecraftServerStartConfig.java | 48 +++++++++++++++++++ .../property/MinecraftServerStartProp.java | 13 +++++ src/main/resources/application-dev.yaml | 8 ++++ src/main/resources/application-prod.yaml | 7 +++ src/main/resources/application-test.yaml | 8 ++++ src/main/resources/application.properties | 1 - src/main/resources/application.yaml | 3 ++ 8 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/javaspringcourse/config/MinecraftServerStartConfig.java create mode 100644 src/main/java/org/javaspringcourse/config/property/MinecraftServerStartProp.java create mode 100644 src/main/resources/application-dev.yaml create mode 100644 src/main/resources/application-prod.yaml create mode 100644 src/main/resources/application-test.yaml delete mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/application.yaml diff --git a/build.gradle.kts b/build.gradle.kts index fc5abdc..8f433d4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -19,6 +19,10 @@ repositories { dependencies { implementation("org.springframework.boot:spring-boot-starter") + implementation("org.springframework.boot:spring-boot-starter-thymeleaf") + implementation("org.projectlombok:lombok:1.18.30") + compileOnly("org.projectlombok:lombok") + annotationProcessor("org.projectlombok:lombok") testImplementation("org.springframework.boot:spring-boot-starter-test") testRuntimeOnly("org.junit.platform:junit-platform-launcher") } diff --git a/src/main/java/org/javaspringcourse/config/MinecraftServerStartConfig.java b/src/main/java/org/javaspringcourse/config/MinecraftServerStartConfig.java new file mode 100644 index 0000000..bc5191f --- /dev/null +++ b/src/main/java/org/javaspringcourse/config/MinecraftServerStartConfig.java @@ -0,0 +1,48 @@ +package org.javaspringcourse.config; + +import jakarta.annotation.PostConstruct; +import lombok.RequiredArgsConstructor; +import lombok.extern.log4j.Log4j2; +import org.javaspringcourse.config.property.MinecraftServerStartProp; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +@Log4j2 +@Configuration +@EnableConfigurationProperties(MinecraftServerStartProp.class) +@RequiredArgsConstructor +public class MinecraftServerStartConfig { + private final MinecraftServerStartProp minecraftServerStartProp; + + @Bean + @Profile("test") + public String testServerChecker() { + log.info("Running testServerChecker..."); + return "testServerChecker"; + } + + @Bean + @ConditionalOnBean(name = "testServerChecker") + public String testServerProfiler() { + log.info("Running testServerProfiler..."); + return "testServerChecker"; + } + + @Bean + @ConditionalOnExpression("'${server.env-var}' != 'default'") + public String specialServerMode() { + log.info("Running specialServerMode..."); + log.warn("GEORGE DROID ©NEGROTECH 2077 joined the server."); + return "specialServerMode"; + } + + @PostConstruct + public void init() { + log.info("Server Name: {}", minecraftServerStartProp.getName()); + log.info("Server Params: {}", minecraftServerStartProp.getStartParams()); + } +} diff --git a/src/main/java/org/javaspringcourse/config/property/MinecraftServerStartProp.java b/src/main/java/org/javaspringcourse/config/property/MinecraftServerStartProp.java new file mode 100644 index 0000000..0164bec --- /dev/null +++ b/src/main/java/org/javaspringcourse/config/property/MinecraftServerStartProp.java @@ -0,0 +1,13 @@ +package org.javaspringcourse.config.property; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.List; + +@Data +@ConfigurationProperties(prefix = "server.start") +public class MinecraftServerStartProp { + String name; + List startParams; +} diff --git a/src/main/resources/application-dev.yaml b/src/main/resources/application-dev.yaml new file mode 100644 index 0000000..39c10b7 --- /dev/null +++ b/src/main/resources/application-dev.yaml @@ -0,0 +1,8 @@ +server: + start: + name: svo-craft-dev + startParams: + - debug + - infiniteWar + - nobodyWins + env-var: ${env-var:default} \ No newline at end of file diff --git a/src/main/resources/application-prod.yaml b/src/main/resources/application-prod.yaml new file mode 100644 index 0000000..8c886b0 --- /dev/null +++ b/src/main/resources/application-prod.yaml @@ -0,0 +1,7 @@ +server: + start: + name: svo-craft-dev + startParams: + - dogovornyachokWillBe + - trumpWins + env-var: ${env-var:specialVal} \ No newline at end of file diff --git a/src/main/resources/application-test.yaml b/src/main/resources/application-test.yaml new file mode 100644 index 0000000..5a5952f --- /dev/null +++ b/src/main/resources/application-test.yaml @@ -0,0 +1,8 @@ +server: + start: + name: svo-craft-dev + startParams: + - debug + - checkDogovornyachok + - nobodyWins + env-var: ${env-var:default} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 61bae71..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -spring.application.name=java-spring-course diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml new file mode 100644 index 0000000..5a72d6b --- /dev/null +++ b/src/main/resources/application.yaml @@ -0,0 +1,3 @@ +spring: + application: + name: java-spring-course