From 1c2044d5e98e7f3c14188a24d9e85c83b9f021db Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 14 Mar 2024 15:18:32 +0900 Subject: [PATCH] =?UTF-8?q?watchdog-be:=20CORS=E3=82=92=E3=81=9B=E3=81=A3?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- watch-dog-be/build.gradle.kts | 1 + .../kotlin/dev/usbharu/unos/watchdog/be/Main.kt | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/watch-dog-be/build.gradle.kts b/watch-dog-be/build.gradle.kts index 167907a..d2cff88 100644 --- a/watch-dog-be/build.gradle.kts +++ b/watch-dog-be/build.gradle.kts @@ -36,6 +36,7 @@ dependencies { implementation("io.ktor:ktor-server-content-negotiation-jvm") implementation("io.ktor:ktor-serialization-kotlinx-json-jvm") implementation("io.ktor:ktor-server-netty-jvm") + implementation("io.ktor:ktor-server-cors") implementation("ch.qos.logback:logback-classic:1.5.3") } diff --git a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/Main.kt b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/Main.kt index 5ffbf2c..3fe6191 100644 --- a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/Main.kt +++ b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/Main.kt @@ -6,19 +6,33 @@ import com.mongodb.kotlin.client.coroutine.MongoClient import com.mongodb.kotlin.client.coroutine.MongoDatabase import dev.usbharu.unos.watchdog.be.domain.MongoMetricsRepository import io.grpc.ServerBuilder +import io.ktor.http.* import io.ktor.serialization.kotlinx.json.* import io.ktor.server.application.* import io.ktor.server.plugins.contentnegotiation.* +import io.ktor.server.plugins.cors.routing.* import io.ktor.server.routing.* +import java.net.URI fun main(args: Array) { io.ktor.server.netty.EngineMain.main(args) } fun Application.module() { + val frontEndUrl = URI(environment.config.property("watchdog.front-url").getString()) install(ContentNegotiation) { json() } + install(CORS){ + allowMethod(HttpMethod.Get) + allowMethod(HttpMethod.Post) + allowMethod(HttpMethod.Put) + allowMethod(HttpMethod.Delete) + allowMethod(HttpMethod.Options) + allowHost(frontEndUrl.authority, schemes = listOf(frontEndUrl.scheme)) + allowCredentials = true + allowNonSimpleContentTypes = true + } val url = environment.config.property("watchdog.db.url").getString() val databaseName = environment.config.property("watchdog.db.database").getString()