https://kotlinlang.org logo
#ktor
Title
# ktor
b

Bernhard

02/21/2024, 1:18 PM
how do I pass a parameter from
Copy code
java -jar fat.jar --test="hi"
to KTOR so I can read inside my routes
1
a

Aleksei Tirman [JB]

02/21/2024, 1:21 PM
One way is to pass environment variables and read them with
Java.lang.System.getenv
method:
Copy code
TEST=hi java -jar fat.jar
s

Sam

02/21/2024, 1:21 PM
You could also the
-P
flag to create a custom config property, like mentioned at the bottom of this section: https://ktor.io/docs/configuration-file.html#command-line
👍 1
b

Bernhard

02/21/2024, 1:22 PM
a related question: I've got this generated application.conf file
Copy code
ktor {
  deployment {
    port = 8080
  }
}

media {
    dir = "aadfsdf"
}
when I start the server inside intellij, environment.config.propertyOrNull("media.dir") is null
so from what I see, you could pass
Copy code
-P:media.dir=/data
to override this default
I'm accessing the config like this:
Copy code
fun Application.configureRouting() {
    println(environment.config.propertyOrNull("media.dir"))
}
it's always null though
is the configuration not parsed yet at that point?
a

Aleksei Tirman [JB]

02/21/2024, 1:33 PM
Can you show the code of the
main
function?
b

Bernhard

02/21/2024, 1:33 PM
Copy code
fun main() {
    embeddedServer(Netty, port = 8080, host = "127.0.0.1", module = Application::module)
        .start(wait = true)
}
basically the generated main function from intellij
hah got it
intellij generates an application.conf file in resources that isn't read using the default main function
a

Aleksei Tirman [JB]

02/21/2024, 1:35 PM
The
application.conf
isn't loaded with that configuration. You need to use EngineMain.
Yes
b

Bernhard

02/21/2024, 1:35 PM
thank you, that cleared things up 🙂
2 Views