Hello Folks, I’m trying to start a ktor server wh...
# ktor
p
Hello Folks, I’m trying to start a ktor server which is dependent upon my application.conf file. I’m using embeddedServer with custom shutdown hook as below:
Copy code
fun main(args: Array<String>): Unit = runBlocking(Dispatchers.Default) {
    embeddedServer(
        Netty,
    ) { module() }.awaitShutdown()
}

fun Application.module(testing: Boolean = false) {
    configureMonitoring()
    configureRouting()
    configureSerialization()
    configureHTTP()
    configureKoin()
    statusPage()
}
But I’m not sure where to pass the args from conf file. I know I can use
fun main(args: Array<String>): Unit = EngineMain.main(args)
instead of embeddedServer but I’m not sure I can add custom shutdown hook to it. Can someone please help me for this?
1
a
how are you reading the config file, and where do you need to pass it to?
p
@August Lilleaas While using
EngineMain.main(args)
implementation, I’m able to read the config via
environment.config.property(key).getString()
. Using the same for embeddedServer is not working and is throwing
io.ktor.server.config.ApplicationConfigurationException: Property ktor.api.customkey not found
a
ah, right. That makes sense, I believe the difference between embeddedServer and EngineMain.main(..) is that with embeddedServer, you configure Ktor programmatically instead of by config file?
you could use a library such as Typesafe Config to read a .conf file instead
p
@August Lilleaas Thanks for the hint. Was able to implement it as:
Copy code
val environment = applicationEngineEnvironment {
      this.parentCoroutineContext = coroutineContext + parentCoroutineContext
      this.log = KtorSimpleLogger("ktor.application")
      this.connectors.addAll(connectors)
      this.config = HoconApplicationConfig(ConfigFactory.load())
    }

    embeddedServer(factory, environment, configure).apply { start() }
K 1