Fix cors config

This commit is contained in:
2026-02-24 21:40:12 +08:00
parent 1cf6c59e8a
commit 014e0fe225
4 files changed
+21 -13

No files matched your search

+1 -1
View File
@@ -1,2 +1,2 @@
kotlin.code.style=official kotlin.code.style=official
libVersion=1.0.4 libVersion=1.0.5
@@ -14,21 +14,14 @@ import org.w3c.fetch.ResponseInit
* Auto add cors response headers * Auto add cors response headers
*/ */
@Suppress("FunctionName") @Suppress("FunctionName")
internal fun WorkerApplication._addCorsHeaders( internal fun WorkerApplication._addCorsHeaders(init: ResponseInit, ): ResponseInit {
init: ResponseInit,
origin: String = "*",
allowedMethods: Set<HttpMethod> = setOf(
HttpMethod.GET, HttpMethod.POST,
HttpMethod.DELETE, HttpMethod.DELETE,
HttpMethod.OPTIONS
),
): ResponseInit {
val headers = if (this.corsConfig.enabled) { val headers = if (this.corsConfig.enabled) {
val corsHeaders = init.headers ?: js("{}") val corsHeaders = init.headers ?: js("{}")
corsHeaders["Access-Control-Allow-Origin"] = origin corsHeaders["Access-Control-Allow-Origin"] = corsConfig.origin
corsHeaders["Access-Control-Allow-Methods"] = allowedMethods.joinToString(", ") corsHeaders["Access-Control-Allow-Methods"] = corsConfig.allowedMethods.joinToString(", ")
corsHeaders["Access-Control-Allow-Headers"] = "Content-Type, Authorization" corsHeaders["Access-Control-Allow-Headers"] = "Content-Type, Authorization"
corsHeaders["Access-Control-Max-Age"] = "86400" if (corsConfig.allowCredentials) corsHeaders["Access-Control-Allow-Credentials"] = corsConfig.allowCredentials
corsHeaders["Access-Control-Max-Age"] = "${corsConfig.maxAge}"
corsHeaders corsHeaders
} else init.headers } else init.headers
return ResponseInit( return ResponseInit(
@@ -28,4 +28,8 @@ public class CORSConfig {
HttpMethod.DELETE, HttpMethod.DELETE, HttpMethod.DELETE, HttpMethod.DELETE,
HttpMethod.OPTIONS HttpMethod.OPTIONS
) )
public var allowCredentials: Boolean = false
public var maxAge: Int = 86400
} }
+11
View File
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins { plugins {
kotlin("jvm") kotlin("jvm")
id("java-gradle-plugin") id("java-gradle-plugin")
@@ -19,4 +21,13 @@ gradlePlugin {
dependencies { dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:2.2.21") implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:2.2.21")
}
tasks.compileKotlin {
compilerOptions.jvmTarget = JvmTarget.JVM_11
}
tasks.compileJava {
sourceCompatibility = "11"
targetCompatibility = "11"
} }