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.
This commit is contained in:
1 parent
b33eb076c1
commit
54cf807a9b
2 files changed
+19
No files matched your search
@@ -35,6 +35,7 @@ dependencies {
|
|||||||
implementation("io.ktor:ktor-server-status-pages-jvm:3.1.2")
|
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-call-logging-jvm:3.1.2")
|
||||||
implementation("io.ktor:ktor-server-auto-head-response-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
|
// Database
|
||||||
implementation("org.jetbrains.exposed:exposed-core:0.60.0")
|
implementation("org.jetbrains.exposed:exposed-core:0.60.0")
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package win.taptap.musicmanager
|
package win.taptap.musicmanager
|
||||||
|
|
||||||
import io.ktor.http.ContentType
|
import io.ktor.http.ContentType
|
||||||
|
import io.ktor.http.HttpHeaders
|
||||||
|
import io.ktor.http.HttpMethod
|
||||||
import io.ktor.http.HttpStatusCode
|
import io.ktor.http.HttpStatusCode
|
||||||
import io.ktor.serialization.kotlinx.json.json
|
import io.ktor.serialization.kotlinx.json.json
|
||||||
import io.ktor.server.application.install
|
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.autohead.AutoHeadResponse
|
||||||
import io.ktor.server.plugins.calllogging.CallLogging
|
import io.ktor.server.plugins.calllogging.CallLogging
|
||||||
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
|
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.partialcontent.PartialContent
|
||||||
import io.ktor.server.plugins.statuspages.StatusPages
|
import io.ktor.server.plugins.statuspages.StatusPages
|
||||||
import io.ktor.server.request.path
|
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(PartialContent)
|
||||||
install(AutoHeadResponse)
|
install(AutoHeadResponse)
|
||||||
install(CallLogging)
|
install(CallLogging)
|
||||||
|
|||||||
Reference in new issue
Block a user