You want to configure the url of your Ktor server based on whether you’re working in dev or deploying to staging or production. But the Ktor official documentation only shows really basic examples like this:
ktor {
deployment {
port = ${PORT}
}
}
But what if you want to do something like this? Is it possible?
ktor {
deployment {
host = "http://my-new-microservice-${ENVIRONMENT}.robduo.com
}
}
So, I decided to test it out.
Testing string interpolation with environment variables
I set up a new Ktor server that would print out the host when I hit the server:
fun Application.module() {
val host = environment.config.property("ktor.deployment.host").getString()
routing {
get("/") {
call.respondText { "Hello World from ${host}" }
}
}
}
Then I had my application.conf
set up to interpolate the environment in the host:
ktor {
deployment {
host = "<http://my-new-microservice-$>{ENVIRONMENT}.robduo.com
port = $PORT
}
}
I have ENVIRONMENT
and PORT
environment variables set up:
And voila – just like that, when I hit the server, I have the staging coming through: