From 0eb853050b3168cc9a257478fb2518a3cceab31e Mon Sep 17 00:00:00 2001 From: rnentjes Date: Sun, 1 Dec 2024 12:12:33 +0100 Subject: [PATCH] Refactor placeholder constants into dedicated file Moved REPO_NAME and related constants into Placeholders.kt to improve code organization and maintainability. The code now dynamically references these constants where needed, simplifying updates and ensuring consistency across modules. Updated relevant imports and references to accommodate these changes. --- .gitea/template | 2 +- settings.gradle.kts | 2 +- src/jvmMain/kotlin/nl/astraeus/tmpl/Main.kt | 4 +--- src/jvmMain/kotlin/nl/astraeus/tmpl/Placeholders.kt | 7 +++++++ src/jvmMain/kotlin/nl/astraeus/tmpl/web/Index.kt | 8 +++++--- src/jvmMain/kotlin/nl/astraeus/tmpl/web/RequestHandler.kt | 5 ++--- 6 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 src/jvmMain/kotlin/nl/astraeus/tmpl/Placeholders.kt diff --git a/.gitea/template b/.gitea/template index 0ea7e8f..6951fa5 100644 --- a/.gitea/template +++ b/.gitea/template @@ -1,3 +1,3 @@ # Repo name settings.gradle.kts -**.kt +src/jvmMain/kotlin/nl/astraeus/tmpl/Placeholders.kt diff --git a/settings.gradle.kts b/settings.gradle.kts index 449722b..0c521bb 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,5 +1,5 @@ plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0" } -val REPO_NAME = "template" +val REPO_NAME = "dummy so the gitea template compiles, please remove" rootProject.name = "kotlin-server-web-undertow" diff --git a/src/jvmMain/kotlin/nl/astraeus/tmpl/Main.kt b/src/jvmMain/kotlin/nl/astraeus/tmpl/Main.kt index ac04344..272d1f8 100644 --- a/src/jvmMain/kotlin/nl/astraeus/tmpl/Main.kt +++ b/src/jvmMain/kotlin/nl/astraeus/tmpl/Main.kt @@ -13,8 +13,6 @@ import nl.astraeus.tmpl.web.RequestHandler val log = Logger() -val REPO_NAME = "dummy so the gitea template compiles, please remove" - val SERVER_PORT = 7001 val JDBC_PORT = 8001 @@ -37,7 +35,7 @@ fun main() { Class.forName("nl.astraeus.jdbc.Driver") Database.initialize(HikariConfig().apply { driverClassName = "nl.astraeus.jdbc.Driver" - jdbcUrl = "jdbc:stat:webServerPort=$JDBC_PORT:jdbc:sqlite:data/${REPO_NAME}.db" + jdbcUrl = "jdbc:stat:webServerPort=$JDBC_PORT:jdbc:sqlite:data/$repoName.db" username = "sa" password = "" maximumPoolSize = 25 diff --git a/src/jvmMain/kotlin/nl/astraeus/tmpl/Placeholders.kt b/src/jvmMain/kotlin/nl/astraeus/tmpl/Placeholders.kt new file mode 100644 index 0000000..4d8f381 --- /dev/null +++ b/src/jvmMain/kotlin/nl/astraeus/tmpl/Placeholders.kt @@ -0,0 +1,7 @@ +package nl.astraeus.tmpl + +val REPO_NAME = "dummy so the gitea template compiles, please remove" + +val pageTitle = "${REPO_NAME}" +val itemUrl = "${REPO_NAME}" +val repoName = "${REPO_NAME}" diff --git a/src/jvmMain/kotlin/nl/astraeus/tmpl/web/Index.kt b/src/jvmMain/kotlin/nl/astraeus/tmpl/web/Index.kt index 3da7ad0..4b9764a 100644 --- a/src/jvmMain/kotlin/nl/astraeus/tmpl/web/Index.kt +++ b/src/jvmMain/kotlin/nl/astraeus/tmpl/web/Index.kt @@ -8,6 +8,8 @@ import kotlinx.html.script import kotlinx.html.stream.appendHTML import kotlinx.html.title import nl.astraeus.tmpl.REPO_NAME +import nl.astraeus.tmpl.pageTitle +import nl.astraeus.tmpl.repoName fun generateIndex(patch: String?): String { val result = StringBuilder(); @@ -15,17 +17,17 @@ fun generateIndex(patch: String?): String { if (patch == null) { result.appendHTML(true).html { head { - title("${REPO_NAME}") + title(pageTitle) //link("/css/all.min.css", "stylesheet") } body { - script(src = "/${REPO_NAME}.js") {} + script(src = "/$repoName.js") {} } } } else { result.appendHTML(true).html { head { - title("${REPO_NAME}") + title(pageTitle) meta { httpEquiv = "refresh" content = "0; url=/$itemUrl/$patch" diff --git a/src/jvmMain/kotlin/nl/astraeus/tmpl/web/RequestHandler.kt b/src/jvmMain/kotlin/nl/astraeus/tmpl/web/RequestHandler.kt index 42abd85..04f514d 100644 --- a/src/jvmMain/kotlin/nl/astraeus/tmpl/web/RequestHandler.kt +++ b/src/jvmMain/kotlin/nl/astraeus/tmpl/web/RequestHandler.kt @@ -5,11 +5,10 @@ import io.undertow.server.HttpServerExchange import io.undertow.server.handlers.PathHandler import io.undertow.server.handlers.resource.PathResourceManager import io.undertow.server.handlers.resource.ResourceHandler +import nl.astraeus.tmpl.itemUrl import java.nio.file.Paths import kotlin.text.startsWith -val itemUrl = "blaat" - object IndexHandler : HttpHandler { override fun handleRequest(exchange: HttpServerExchange) { if (exchange.requestPath.startsWith("/$itemUrl/")) { @@ -29,7 +28,7 @@ object RequestHandler : HttpHandler { init { pathHandler.addExactPath("/", IndexHandler) pathHandler.addExactPath("/index.html", IndexHandler) - pathHandler.addPrefixPath("/song", IndexHandler) + pathHandler.addPrefixPath("/$itemUrl", IndexHandler) pathHandler.addExactPath("/ws", WebsocketConnectHandler) }