Skip to content

Commit 256f81e

Browse files
committed
[SERVER] Add config for enabling CORS for development
1 parent efbcb77 commit 256f81e

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package me.moallemi.kmpshowcase.server
2+
3+
import io.ktor.application.Application
4+
5+
private val Application.envType get() = environment.config.property("ktor.environment").getString()
6+
val Application.isDev get() = envType == "dev"
7+
val Application.isProd get() = envType == "prod"

server/src/main/kotlin/me/moallemi/kmpshowcase/server/Application.kt renamed to server/src/main/kotlin/me/moallemi/kmpshowcase/server/ServerApp.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package me.moallemi.kmpshowcase.server
22

33
import io.ktor.application.Application
44
import io.ktor.application.install
5+
import io.ktor.features.CORS
56
import io.ktor.features.ContentNegotiation
67
import io.ktor.http.content.resources
78
import io.ktor.http.content.static
@@ -17,6 +18,12 @@ fun Application.module(testing: Boolean = false) {
1718
json()
1819
}
1920

21+
if (isDev) {
22+
install(CORS) {
23+
anyHost()
24+
}
25+
}
26+
2027
routing {
2128
home()
2229
apiV1()

server/src/main/kotlin/me/moallemi/kmpshowcase/server/utils/ApplicationCallExt.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import io.ktor.request.host
66
import io.ktor.request.port
77

88
fun ApplicationCall.baseUrl() =
9-
if (request.host() != "0.0.0.0") {
9+
if (request.host() !in listOf("0.0.0.0", "localhost")) {
1010
"https://${request.host()}"
1111
} else {
1212
"${request.origin.scheme}://${request.host()}:${request.port()}"
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
ktor {
22
deployment {
3-
port = 8080
3+
port = 9090
44
port = ${?PORT}
55
}
6+
7+
environment = dev
8+
environment = ${?KTOR_ENV}
9+
610
application {
7-
modules = [ me.moallemi.kmpshowcase.server.ApplicationKt.module ]
11+
modules = [ me.moallemi.kmpshowcase.server.ServerAppKt.module ]
812
}
913
}

0 commit comments

Comments
 (0)