如何启用嵌入式服务器中的开发模式以在 Ktor 中使用自动重新加载?

una*_*adi 8 webserver kotlin ktor

我在后端使用 Ktor,我计划使用自动重新加载,当我使用 engine main 时,我在 application.conf 文件中配置了开发模式。如何在嵌入式服务器中做同样的事情?

fun main() {
    embeddedServer(Netty, port = 8080 ,watchPaths = listOf("classes","resources")) {
        module()
    }.start(wait = true)
}
Run Code Online (Sandbox Code Playgroud)

小智 0

fun main() {
    embeddedServer(
        Netty, port = 80, host = "0.0.0.0", developmentMode = true, watchPaths = listOf("main")
    ) {
        configureRouting()
        configureSerialization()
    }.start(wait = true)
}

@OptIn(DelicateCoroutinesApi::class)
fun <TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Configuration> embeddedServer(
    factory: ApplicationEngineFactory<TEngine, TConfiguration>,
    port: Int = 80,
    host: String = "0.0.0.0",
    developmentMode: Boolean = false,
    watchPaths: List<String> = listOf(File(".").canonicalPath),
    configure: TConfiguration.() -> Unit = {},
    module: Application.() -> Unit
): TEngine = GlobalScope.embeddedServer(
    factory, port, host, developmentMode, watchPaths, EmptyCoroutineContext, configure, module
)


fun <TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Configuration> CoroutineScope.embeddedServer(
    factory: ApplicationEngineFactory<TEngine, TConfiguration>,
    port: Int = 80,
    host: String = "0.0.0.0",
    developmentMode: Boolean = false,
    watchPaths: List<String> = listOf(File(".").canonicalPath),
    parentCoroutineContext: CoroutineContext = EmptyCoroutineContext,
    configure: TConfiguration.() -> Unit = {},
    module: Application.() -> Unit
): TEngine {

    val connectors: Array<EngineConnectorConfig> = arrayOf(EngineConnectorBuilder().apply {
        this.port = port
        this.host = host
    })
    val environment = applicationEngineEnvironment {
        this.parentCoroutineContext = coroutineContext + parentCoroutineContext
        this.log = KtorSimpleLogger("ktor.application")
        this.watchPaths = watchPaths
        this.module(module)
        this.connectors.addAll(connectors)
        this.developmentMode = developmentMode
    }

    return embeddedServer(factory, environment, configure)
}
Run Code Online (Sandbox Code Playgroud)

我尝试和工作的是创建我自己的函数来创建一个嵌入服务器。基本上我复制了 EmbeddedServer.kt 逻辑并公开了developmentMode。如果您使用 Intellij,则需要按“构建”按钮才能工作,或启用自动构建。但是我认为这不是一个好主意,因为您需要一种方法来为其他环境更新此值。因此,它应该由 application.conf 或 gradle 中的 applicationDefaultJvmArgs 来完成。

编辑:回去尝试 ktor 的最新版本。你可以使用这样的东西。还要确保您已启用自动构建,否则每次服务器运行时都必须进行构建。

fun main() {

    embeddedServer(
        Netty,
        environment = applicationEngineEnvironment {
            log = LoggerFactory.getLogger("ktor.application")
            config = HoconApplicationConfig(ConfigFactory.load())
            developmentMode = true
            watchPaths = listOf("class")

            connector {
                port = 8080
                host = "localhost"
            }

            module {
                defaultModule()
            }
        }

    ).start(wait = true)
}
Run Code Online (Sandbox Code Playgroud)

  • 尝试过此操作,但得到“找不到具有此名称的参数:developmentMode” (2认同)