Archived
0

refactoring: Config

YAML -> HOCON
This commit is contained in:
2021-05-11 16:07:46 +03:00
parent 2b9f021419
commit 59b374e623
13 changed files with 152 additions and 151 deletions

35
server-new/build.gradle Normal file
View File

@@ -0,0 +1,35 @@
/*
Запуск
gradle :server:run --args="--config=config.yml --logconfig==logback.xml"
Сборка
gradle :server:shadowJar
*/
//file:noinspection GrUnresolvedAccess
plugins {
id 'com.github.johnrengelman.shadow' version '7.0.0'
}
apply from: rootDir.toPath().resolve('logic.gradle').toFile()
apply plugin: 'application'
application {
mainClassName = 'mc.server.Main'
if (project.hasProperty('jvmArgs')) {
applicationDefaultJvmArgs = List.of((project.jvmArgs as String).split('\\s+'))
}
}
dependencies {
implementation project(':protocol-new')
implementation libs.hocon
}
shadowJar {
archiveBaseName.set(jar.archiveBaseName.get())
archiveVersion.set(project.version as String)
archiveClassifier.set('')
}

View File

@@ -0,0 +1,2 @@
# suppress inspection "UnusedProperty" for whole file
module.name=server

View File

@@ -0,0 +1,13 @@
package mc.server.di;
import com.typesafe.config.Config;
import dagger.Component;
import javax.inject.Singleton;
@Component(modules = ConfigModule.class)
@Singleton
public interface ConfigComponent {
Config getConfig();
}

View File

@@ -0,0 +1,25 @@
package mc.server.di;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import dagger.Module;
import dagger.Provides;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import javax.inject.Singleton;
import java.nio.file.Path;
@Module
@RequiredArgsConstructor
@Slf4j
public class ConfigModule {
private final Path configPath;
@Provides
@Singleton
Config provideConfig() {
return ConfigFactory.parseFile(configPath.toFile()).resolve();
}
}

View File

@@ -0,0 +1,59 @@
package mc.server.di;
import com.typesafe.config.Config;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.*;
class ConfigModuleTest {
@Test
void singleton() {
ConfigComponent component = DaggerConfigComponent.builder()
.configModule(new ConfigModule(pathResource("/config-1.conf"))).build();
Config config1 = component.getConfig();
Config config2 = component.getConfig();
assertEquals(config1, config2);
assertSame(config1, config2);
}
@Test
void loadConfig() {
ConfigComponent component = DaggerConfigComponent.builder()
.configModule(new ConfigModule(pathResource("/config-1.conf"))).build();
Config config = component.getConfig();
assertEquals("value1", config.getString("key1"));
assertEquals("value2", config.getString("key2.subkey1"));
assertEquals("value3", config.getString("key3.subkey1"));
assertEquals("value4", config.getString("\"key4.somename\""));
assertEquals("value5", config.getString("key5"));
}
@Test
void includeTest() {
ConfigComponent component = DaggerConfigComponent.builder()
.configModule(new ConfigModule(pathResource("/config-2.conf"))).build();
Config config = component.getConfig();
assertEquals("value1", config.getString("key1"));
assertEquals("value2", config.getString("key2.subkey1"));
assertEquals("value3", config.getString("key3.subkey1"));
assertEquals("value4", config.getString("\"key4.somename\""));
assertEquals("value5", config.getString("key5"));
}
@SneakyThrows
private static Path pathResource(String resource) {
URL url = ConfigModuleTest.class.getResource(resource);
assertNotNull(url);
return Paths.get(url.toURI());
}
}

View File

@@ -0,0 +1,12 @@
key1: value1
key2.subkey1: value2
key3 {
subkey1: value3
}
"key4.somename": value4
variable: value5
key5: ${variable}

View File

@@ -0,0 +1 @@
include "config-1.conf"