Files
meldestelle/frontend/shells/meldestelle-portal/src/jsMain/kotlin/Config.kt
T
stefan ac3bfcad3c refactor(theme): centralize colors and typography in AppColors and AppTypography
- Replaced hardcoded color values in `LightColorScheme` and `DarkColorScheme` with `AppColors`.
- Updated `AppTypography` references in `AppMaterialTypography`.
- Upgraded Kotlin, Compose Multiplatform, and Ktor to latest versions in `libs.versions.toml`.
- Added `uiToolingPreview` dependency in `meldestelle-portal` build script.
- Refactored config loading to include `RequestInit`.

Signed-off-by: Stefan Mogeritsch <stefan.mo.co@gmail.com>
2026-03-21 16:56:33 +01:00

41 lines
1.5 KiB
Kotlin

import kotlinx.browser.window
import kotlinx.coroutines.await
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import org.w3c.fetch.RequestInit
private val AppJson = Json { ignoreUnknownKeys = true }
@Serializable
data class AppConfig(
val apiBaseUrl: String,
val keycloakUrl: String? = null // Optional, da nicht immer vorhanden
)
suspend fun loadAppConfig(): AppConfig {
return try {
// ?_nocache erzwingt einen SW-bypassing Request (SW sieht andere URL → Cache-Miss → Netzwerk)
val response = window.fetch("/config.json?_nocache=1", RequestInit()).await()
if (!response.ok) {
console.warn("[Config] Failed to load config.json, falling back to globalThis")
return fallbackFromGlobal()
}
val text = response.text().await()
AppJson.decodeFromString(AppConfig.serializer(), text)
} catch (e: dynamic) {
console.error("[Config] Error loading config:", e)
// Fallback: Caddy-injizierte Werte aus index.html (globalThis.API_BASE_URL / KEYCLOAK_URL)
fallbackFromGlobal()
}
}
private fun fallbackFromGlobal(): AppConfig {
val apiBase = (js("globalThis.API_BASE_URL") as? String)
?.takeIf { it.isNotBlank() && !it.startsWith($$"${") }
?: window.location.origin
val kcUrl = (js("globalThis.KEYCLOAK_URL") as? String)
?.takeIf { it.isNotBlank() && !it.startsWith($$"${") }
console.log("[Config] Fallback: apiBaseUrl=$apiBase, keycloakUrl=$kcUrl")
return AppConfig(apiBaseUrl = apiBase, keycloakUrl = kcUrl)
}