小编Chr*_*Keo的帖子

在 Ktor 应用程序模块而不是 Main 中获取命令行参数?

我正在尝试从 Ktor 构建一个应用程序,最终应该通过 fatjar 执行。fatjar 允许使用如下参数:

java -jar myApp.jar XXXXX YYYYY

我知道如何XXXXX在主模块中获取args[0],但我在应用程序模块中获取该值时遇到了麻烦。

我想做这样的事情:

fun main(args: Array<String>) {
    val port = System.getenv("PORT")?.toInt() ?: 8080
    val status = args[0]
    embeddedServer(Netty, port = port, module = (Application::mainModule())).start(wait = true)
}

fun Application.mainModule() {
    routing {
        get("/status") {
            call.respondText(
                <status variable from main function>,
                contentType = ContentType.Text.Html
            )
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

kotlin ktor

5
推荐指数
1
解决办法
1909
查看次数

标签 统计

kotlin ×1

ktor ×1