From d0df60cc1cf030e275bc38a6c267143fb78abb14 Mon Sep 17 00:00:00 2001 From: Graham Date: Wed, 24 Feb 2021 18:08:27 +0000 Subject: [PATCH] Add archiving service config file Signed-off-by: Graham --- all/build.gradle.kts | 4 +++ archive/build.gradle.kts | 1 + .../org/openrs2/archive/ArchiveConfig.kt | 3 +++ .../openrs2/archive/ArchiveConfigProvider.kt | 27 +++++++++++++++++++ .../org/openrs2/archive/ArchiveModule.kt | 6 +++++ .../org/openrs2/archive/DataSourceProvider.kt | 8 +++--- etc/.gitignore | 1 + etc/archive.example.yaml | 2 ++ 8 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 archive/src/main/kotlin/org/openrs2/archive/ArchiveConfig.kt create mode 100644 archive/src/main/kotlin/org/openrs2/archive/ArchiveConfigProvider.kt create mode 100644 etc/archive.example.yaml diff --git a/all/build.gradle.kts b/all/build.gradle.kts index 1d1b9eb7..f2dbc758 100644 --- a/all/build.gradle.kts +++ b/all/build.gradle.kts @@ -79,6 +79,10 @@ distributions { from("$rootDir/DCO") from("$rootDir/LICENSE") from("$rootDir/README.md") + from("$rootDir/etc/archive.example.yaml") { + rename { "archive.yaml" } + into("etc") + } from("$rootDir/etc/config.example.yaml") { rename { "config.yaml" } into("etc") diff --git a/archive/build.gradle.kts b/archive/build.gradle.kts index 9f8f255c..f560b467 100644 --- a/archive/build.gradle.kts +++ b/archive/build.gradle.kts @@ -21,6 +21,7 @@ dependencies { implementation(project(":net")) implementation(project(":protocol")) implementation(project(":util")) + implementation(project(":yaml")) implementation("com.google.guava:guava:${Versions.guava}") implementation("com.zaxxer:HikariCP:${Versions.hikaricp}") implementation("io.ktor:ktor-jackson:${Versions.ktor}") diff --git a/archive/src/main/kotlin/org/openrs2/archive/ArchiveConfig.kt b/archive/src/main/kotlin/org/openrs2/archive/ArchiveConfig.kt new file mode 100644 index 00000000..b25a6f12 --- /dev/null +++ b/archive/src/main/kotlin/org/openrs2/archive/ArchiveConfig.kt @@ -0,0 +1,3 @@ +package org.openrs2.archive + +public data class ArchiveConfig(val databaseUrl: String) diff --git a/archive/src/main/kotlin/org/openrs2/archive/ArchiveConfigProvider.kt b/archive/src/main/kotlin/org/openrs2/archive/ArchiveConfigProvider.kt new file mode 100644 index 00000000..9d05d710 --- /dev/null +++ b/archive/src/main/kotlin/org/openrs2/archive/ArchiveConfigProvider.kt @@ -0,0 +1,27 @@ +package org.openrs2.archive + +import com.fasterxml.jackson.databind.ObjectMapper +import org.openrs2.yaml.Yaml +import java.nio.file.Files +import java.nio.file.Path +import javax.inject.Inject +import javax.inject.Provider + +public class ArchiveConfigProvider @Inject constructor( + @Yaml private val mapper: ObjectMapper +) : Provider { + override fun get(): ArchiveConfig { + if (Files.notExists(CONFIG_PATH)) { + Files.copy(EXAMPLE_CONFIG_PATH, CONFIG_PATH) + } + + return Files.newBufferedReader(CONFIG_PATH).use { reader -> + mapper.readValue(reader, ArchiveConfig::class.java) + } + } + + private companion object { + private val CONFIG_PATH = Path.of("etc/archive.yaml") + private val EXAMPLE_CONFIG_PATH = Path.of("etc/archive.example.yaml") + } +} diff --git a/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt b/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt index 484f339a..f4f3da96 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/ArchiveModule.kt @@ -15,6 +15,7 @@ import org.openrs2.db.Database import org.openrs2.http.HttpModule import org.openrs2.json.JsonModule import org.openrs2.net.NetworkModule +import org.openrs2.yaml.YamlModule import javax.sql.DataSource public object ArchiveModule : AbstractModule() { @@ -24,6 +25,11 @@ public object ArchiveModule : AbstractModule() { install(HttpModule) install(JsonModule) install(NetworkModule) + install(YamlModule) + + bind(ArchiveConfig::class.java) + .toProvider(ArchiveConfigProvider::class.java) + .`in`(Scopes.SINGLETON) bind(DataSource::class.java) .toProvider(DataSourceProvider::class.java) diff --git a/archive/src/main/kotlin/org/openrs2/archive/DataSourceProvider.kt b/archive/src/main/kotlin/org/openrs2/archive/DataSourceProvider.kt index d67192a3..dbd54c75 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/DataSourceProvider.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/DataSourceProvider.kt @@ -4,14 +4,16 @@ import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource import org.flywaydb.core.Flyway import org.postgresql.ds.PGSimpleDataSource +import javax.inject.Inject import javax.inject.Provider import javax.sql.DataSource -public class DataSourceProvider : Provider { +public class DataSourceProvider @Inject constructor( + private val config: ArchiveConfig +) : Provider { override fun get(): DataSource { val dataSource = PGSimpleDataSource() - // TODO(gpe): make the URL configurable - dataSource.setUrl("jdbc:postgresql://localhost/runearchive?user=gpe&password=gpe") + dataSource.setUrl(config.databaseUrl) Flyway.configure() .dataSource(dataSource) diff --git a/etc/.gitignore b/etc/.gitignore index 7acc4fb9..0f8c48e6 100644 --- a/etc/.gitignore +++ b/etc/.gitignore @@ -1,3 +1,4 @@ +/archive.yaml /config.yaml /game.key /loader.p12 diff --git a/etc/archive.example.yaml b/etc/archive.example.yaml new file mode 100644 index 00000000..f68cbbc6 --- /dev/null +++ b/etc/archive.example.yaml @@ -0,0 +1,2 @@ +--- +database_url: jdbc:postgresql://localhost/openrs2_archive?user=openrs2&password=openrs2