From a73cd4f579809d92dd66ed4cc12fa1d0b48376d8 Mon Sep 17 00:00:00 2001 From: powercas_gamer Date: Sat, 11 Nov 2023 19:13:15 +0100 Subject: [PATCH] feat: config --- app/build.gradle.kts | 3 + .../imagery/app/config/JavaConfigNotUsed.java | 9 +++ .../main/kotlin/dev/mizule/imagery/app/App.kt | 71 +++++++++++++------ .../dev/mizule/imagery/app/config/Config.kt | 13 ++++ .../mizule/imagery/app/launcher/Launcher.kt | 4 ++ 5 files changed, 79 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/dev/mizule/imagery/app/config/JavaConfigNotUsed.java create mode 100644 app/src/main/kotlin/dev/mizule/imagery/app/config/Config.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 8cafd8c..5e84ce0 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -1,5 +1,6 @@ plugins { id("org.jetbrains.kotlin.jvm") version "1.9.20" + `java-library` id("com.github.johnrengelman.shadow") version "8.1.1" id("net.kyori.indra") version "3.1.3" application @@ -20,6 +21,7 @@ dependencies { implementation("io.javalin:javalin:6.0.0-beta.2") implementation("com.github.ben-manes.caffeine:caffeine:3.1.8") implementation("org.spongepowered:configurate-gson:4.2.0-SNAPSHOT") + implementation("org.spongepowered:configurate-hocon:4.2.0-SNAPSHOT") implementation("org.spongepowered:configurate-extra-kotlin:4.2.0-SNAPSHOT") } @@ -28,6 +30,7 @@ indra { target(17) minimumToolchain(17) } + mitLicense() } application { diff --git a/app/src/main/java/dev/mizule/imagery/app/config/JavaConfigNotUsed.java b/app/src/main/java/dev/mizule/imagery/app/config/JavaConfigNotUsed.java new file mode 100644 index 0000000..54a6ae4 --- /dev/null +++ b/app/src/main/java/dev/mizule/imagery/app/config/JavaConfigNotUsed.java @@ -0,0 +1,9 @@ +package dev.mizule.imagery.app.config; + +import org.spongepowered.configurate.objectmapping.ConfigSerializable; + +@ConfigSerializable +public class JavaConfigNotUsed { + + +} diff --git a/app/src/main/kotlin/dev/mizule/imagery/app/App.kt b/app/src/main/kotlin/dev/mizule/imagery/app/App.kt index 2c4274f..cc602ee 100644 --- a/app/src/main/kotlin/dev/mizule/imagery/app/App.kt +++ b/app/src/main/kotlin/dev/mizule/imagery/app/App.kt @@ -3,6 +3,7 @@ package dev.mizule.imagery.app import com.github.benmanes.caffeine.cache.Caffeine import com.google.gson.Gson import com.google.gson.JsonObject +import dev.mizule.imagery.app.config.Config import dev.mizule.imagery.app.model.UploadedFile import io.javalin.Javalin import io.javalin.http.ContentType @@ -10,8 +11,10 @@ import io.javalin.http.HttpStatus import io.javalin.json.JavalinGson import org.eclipse.jetty.http.MimeTypes import org.spongepowered.configurate.BasicConfigurationNode +import org.spongepowered.configurate.CommentedConfigurationNode import org.spongepowered.configurate.ConfigurateException import org.spongepowered.configurate.gson.GsonConfigurationLoader +import org.spongepowered.configurate.hocon.HoconConfigurationLoader import java.io.FileOutputStream import java.nio.file.Files import java.nio.file.Path @@ -21,14 +24,14 @@ import kotlin.io.path.exists class App { private val gson = Gson() - private lateinit var config: BasicConfigurationNode - private lateinit var loader: GsonConfigurationLoader + private lateinit var config: Config + private lateinit var configNode: CommentedConfigurationNode + private lateinit var configLoader: HoconConfigurationLoader + private lateinit var dataConfig: BasicConfigurationNode + private lateinit var dataLoader: GsonConfigurationLoader private val javalin: Javalin by lazy { Javalin.create { it.jsonMapper(JavalinGson(gson)) - it.contextResolver.ip = { ctx -> - ctx.header("CF-Connecting-IP")?: ctx.req().remoteAddr - } it.router.ignoreTrailingSlashes = true } } @@ -41,16 +44,10 @@ class App { fun load() { initializeStorage() + initializeConfigs() - this.loader = GsonConfigurationLoader.builder() - .path(path.resolve("files.json")) - .build() - - try { - this.config = loader.load() - loader.save(this.config) - } catch (e: ConfigurateException) { - throw RuntimeException(e) + javalin.unsafeConfig().contextResolver.ip = { ctx -> + ctx.header("CF-Connecting-IP") ?: ctx.req().remoteAddr } configureEndpoints() @@ -60,11 +57,43 @@ class App { if (!storage.exists()) { Files.createDirectories(storage) } + + // cache? + } + + private fun initializeConfigs() { + this.dataLoader = GsonConfigurationLoader.builder() + .path(path.resolve("files.json")) + .build() + this.configLoader = HoconConfigurationLoader.builder() + .path(path.resolve("config.conf")) + .defaultOptions { + it.shouldCopyDefaults(true) + } + .build() + + try { + this.dataConfig = dataLoader.load() + this.configNode = configLoader.load() + dataLoader.save(this.dataConfig) + configLoader.save(this.configNode) + this.config = configNode.get(Config::class.java) ?: return + } catch (e: ConfigurateException) { + throw RuntimeException(e) + } } private fun configureEndpoints() { javalin.beforeMatched { ctx -> - println(String.format("Received %s request from: %s:%s for %s", ctx.method(), ctx.ip(), ctx.port(), ctx.fullUrl())); + println( + String.format( + "Received %s request from: %s:%s for %s", + ctx.method(), + ctx.ip(), + ctx.port(), + ctx.fullUrl() + ) + ) } javalin.post("/upload") { ctx -> handleFileUpload(ctx) @@ -77,7 +106,7 @@ class App { private fun handleFileUpload(ctx: io.javalin.http.Context) { ctx.uploadedFiles("file").first().also { file -> - val fileName = getRandomString(8) + file.extension() + val fileName = getRandomString() + file.extension() val filePath = storage.resolve(fileName) Files.createFile(filePath) FileOutputStream(filePath.toFile()).use { @@ -91,9 +120,9 @@ class App { file.extension(), MimeTypes.getDefaultMimeByExtension(file.extension()) ) - config.node(uploadedFile.id).set(uploadedFile) + dataConfig.node(uploadedFile.id).set(uploadedFile) try { - loader.save(this.config) + dataLoader.save(this.dataConfig) } catch (e: ConfigurateException) { throw RuntimeException(e) } @@ -120,14 +149,14 @@ class App { } fun enable() { - javalin.start(5462) + javalin.start(this.config.port) } fun disable() { - // Add any necessary cleanup logic here + println("Shutting down...") } - private fun getRandomString(length: Int): String { + private fun getRandomString(length: Int = 8): String { val allowedChars = ('A'..'Z') + ('a'..'z') + ('0'..'9') return (1..length) .map { allowedChars.random() } diff --git a/app/src/main/kotlin/dev/mizule/imagery/app/config/Config.kt b/app/src/main/kotlin/dev/mizule/imagery/app/config/Config.kt new file mode 100644 index 0000000..5338b30 --- /dev/null +++ b/app/src/main/kotlin/dev/mizule/imagery/app/config/Config.kt @@ -0,0 +1,13 @@ +package dev.mizule.imagery.app.config + +import org.spongepowered.configurate.objectmapping.ConfigSerializable +import org.spongepowered.configurate.objectmapping.meta.Comment + +@ConfigSerializable +data class Config( + @Comment("The port to start the application on") + val port: Int = 8052, + + @Comment("The domain that this will be on") + val domain: String = "https://example.com" +) diff --git a/app/src/main/kotlin/dev/mizule/imagery/app/launcher/Launcher.kt b/app/src/main/kotlin/dev/mizule/imagery/app/launcher/Launcher.kt index dd6c30d..c8cb5ff 100644 --- a/app/src/main/kotlin/dev/mizule/imagery/app/launcher/Launcher.kt +++ b/app/src/main/kotlin/dev/mizule/imagery/app/launcher/Launcher.kt @@ -8,4 +8,8 @@ fun main() { app.load() app.enable() + + Runtime.getRuntime().addShutdownHook(Thread { + app.disable() + }) }