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()