watchdog-be: CORSをせっていできるように
/ build-tool (pull_request) Has been skipped Details
/ build-angular (pull_request) Has been skipped Details
/ build-watchdog-be (pull_request) Successful in 2m50s Details
/ build-docker-image (pull_request) Has been skipped Details

This commit is contained in:
usbharu 2024-03-14 15:18:32 +09:00
parent a7fe6f988e
commit 1c2044d5e9
Signed by: usbharu
GPG Key ID: 6556747BF94EEBC8
2 changed files with 15 additions and 0 deletions

View File

@ -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")
}

View File

@ -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<String>) {
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()