From 54cf807a9b7be5dc5319c52298190b2c4620985b Mon Sep 17 00:00:00 2001 From: TapTap Date: Sat, 14 Nov 2026 18:24:13 +0000 Subject: [PATCH] feat: allow cross-origin API requests for the web client The Kotlin/Wasm client can be hosted separately from the API. Bearer auth does not use cookies, so a permissive CORS policy is safe and keeps the static bundle deployable anywhere. --- build.gradle.kts | 1 + .../win/taptap/musicmanager/Application.kt | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/build.gradle.kts b/build.gradle.kts index f4d09c6..e6a0f00 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -35,6 +35,7 @@ dependencies { implementation("io.ktor:ktor-server-status-pages-jvm:3.1.2") implementation("io.ktor:ktor-server-call-logging-jvm:3.1.2") implementation("io.ktor:ktor-server-auto-head-response-jvm:3.1.2") + implementation("io.ktor:ktor-server-cors-jvm:3.1.2") // Database implementation("org.jetbrains.exposed:exposed-core:0.60.0") diff --git a/src/main/kotlin/win/taptap/musicmanager/Application.kt b/src/main/kotlin/win/taptap/musicmanager/Application.kt index 0d1e2d8..e857482 100644 --- a/src/main/kotlin/win/taptap/musicmanager/Application.kt +++ b/src/main/kotlin/win/taptap/musicmanager/Application.kt @@ -1,6 +1,8 @@ package win.taptap.musicmanager import io.ktor.http.ContentType +import io.ktor.http.HttpHeaders +import io.ktor.http.HttpMethod import io.ktor.http.HttpStatusCode import io.ktor.serialization.kotlinx.json.json import io.ktor.server.application.install @@ -9,6 +11,7 @@ import io.ktor.server.netty.Netty import io.ktor.server.plugins.autohead.AutoHeadResponse import io.ktor.server.plugins.calllogging.CallLogging import io.ktor.server.plugins.contentnegotiation.ContentNegotiation +import io.ktor.server.plugins.cors.routing.CORS import io.ktor.server.plugins.partialcontent.PartialContent import io.ktor.server.plugins.statuspages.StatusPages import io.ktor.server.request.path @@ -81,6 +84,21 @@ fun main() { }, ) } + // The Wasm client can be hosted separately; bearer auth needs no cookies. + install(CORS) { + anyHost() + allowMethod(HttpMethod.Get) + allowMethod(HttpMethod.Post) + allowMethod(HttpMethod.Patch) + allowMethod(HttpMethod.Delete) + allowMethod(HttpMethod.Head) + allowMethod(HttpMethod.Options) + allowHeader(HttpHeaders.Authorization) + allowHeader(HttpHeaders.ContentType) + allowHeader(HttpHeaders.Range) + allowNonSimpleContentTypes = true + maxAgeInSeconds = 3600 + } install(PartialContent) install(AutoHeadResponse) install(CallLogging)