fixing web-app

This commit is contained in:
stefan
2025-09-24 14:21:57 +02:00
parent cd2b0796a6
commit 1c4184809a
156 changed files with 440 additions and 1708 deletions
+46
View File
@@ -0,0 +1,46 @@
plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.kotlinSerialization)
}
group = "at.mocode"
version = "1.0.0"
kotlin {
jvm()
js {
browser()
}
// Keep WASM for dev since sources already present
@OptIn(org.jetbrains.kotlin.gradle.ExperimentalWasmDsl::class)
wasmJs {
browser()
}
jvmToolchain(21)
sourceSets {
val commonMain by getting {
dependencies {
implementation(projects.services.ping.pingApi)
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.contentNegotiation)
implementation(libs.ktor.client.serialization.kotlinx.json)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.serialization.json)
}
}
val commonTest by getting {
dependencies {
implementation(libs.kotlin.test)
}
}
val jvmMain by getting {
dependencies {
implementation(libs.ktor.client.cio)
}
}
}
}
@@ -0,0 +1,3 @@
package at.mocode
const val SERVER_PORT = 8081
@@ -0,0 +1,9 @@
package at.mocode
class Greeting {
private val platform = getPlatform()
fun greet(): String {
return "Hello, ${platform.name}!"
}
}
@@ -0,0 +1,7 @@
package at.mocode
interface Platform {
val name: String
}
expect fun getPlatform(): Platform
@@ -0,0 +1,10 @@
package at.mocode.model
// Deprecated local DTOs are replaced by typealiases to the shared API contract.
// This preserves binary/source compatibility for existing imports while enforcing SSoT.
typealias PingResponse = at.mocode.ping.api.PingResponse
typealias EnhancedPingResponse = at.mocode.ping.api.EnhancedPingResponse
typealias HealthResponse = at.mocode.ping.api.HealthResponse
@@ -0,0 +1,25 @@
package at.mocode.ping.client
import at.mocode.ping.api.EnhancedPingResponse
import at.mocode.ping.api.HealthResponse
import at.mocode.ping.api.PingApi
import at.mocode.ping.api.PingResponse
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import at.mocode.service.getBaseUrl
class PingApiClient(
private val client: HttpClient,
baseUrl: String = getBaseUrl()
) : PingApi {
private val base = "$baseUrl/api/ping"
override suspend fun simplePing(): PingResponse = client.get("$base/simple").body()
override suspend fun enhancedPing(simulate: Boolean): EnhancedPingResponse =
client.get("$base/enhanced") { parameter("simulate", simulate) }.body()
override suspend fun healthCheck(): HealthResponse = client.get("$base/health").body()
}
@@ -0,0 +1,43 @@
package at.mocode.service
import at.mocode.model.EnhancedPingResponse
import at.mocode.model.HealthResponse
import at.mocode.model.PingResponse
import at.mocode.ping.client.PingApiClient
import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
@Deprecated("Use PingApiClient directly for new code")
class PingService(
private val client: HttpClient = HttpClient {
install(ContentNegotiation) {
json(Json {
ignoreUnknownKeys = true
isLenient = true
})
}
install(HttpTimeout) {
requestTimeoutMillis = 10000
connectTimeoutMillis = 5000
}
}
) {
private val api = PingApiClient(client)
suspend fun ping(): Result<PingResponse> = runCatching { api.simplePing() }
suspend fun enhancedPing(simulate: Boolean = false): Result<EnhancedPingResponse> =
runCatching { api.enhancedPing(simulate) }
suspend fun health(): Result<HealthResponse> = runCatching { api.healthCheck() }
suspend fun testFailure(): Result<EnhancedPingResponse> = runCatching {
throw RuntimeException("Simulated failure for testing")
}
}
// Platform-specific base URL required by PingApiClient via getBaseUrl()
expect fun getBaseUrl(): String
@@ -0,0 +1,12 @@
package at.mocode
import kotlin.test.Test
import kotlin.test.assertEquals
class SharedCommonTest {
@Test
fun example() {
assertEquals(3, 1 + 2)
}
}
@@ -0,0 +1,7 @@
package at.mocode
class JsPlatform: Platform {
override val name: String = "Web with Kotlin/JS"
}
actual fun getPlatform(): Platform = JsPlatform()
@@ -0,0 +1,4 @@
package at.mocode.service
// Use direct ping-service for JS Development - based on central.toml
actual fun getBaseUrl(): String = "http://localhost:8082"
@@ -0,0 +1,7 @@
package at.mocode
class JVMPlatform: Platform {
override val name: String = "Java ${System.getProperty("java.version")}"
}
actual fun getPlatform(): Platform = JVMPlatform()
@@ -0,0 +1,4 @@
package at.mocode.service
// Use direct ping-service for JVM (Desktop) - based on central.toml
actual fun getBaseUrl(): String = "http://localhost:8082"
@@ -0,0 +1,7 @@
package at.mocode
class WasmPlatform: Platform {
override val name: String = "Web with Kotlin/Wasm"
}
actual fun getPlatform(): Platform = WasmPlatform()
@@ -0,0 +1,4 @@
package at.mocode.service
// Use direct ping-service for WASM Development - based on central.toml
actual fun getBaseUrl(): String = "http://localhost:8082"