如何在嵌入式服务器中启用开发模式以在KTOR中使用自动重新加载?
我正在使用KTOR作为后端,我计划使用自动重新加载,当我使用Engine Main时,我在application.conf文件中配置了开发模式。如何在嵌入式服务器中进行相同的操作?
fun main() {
embeddedServer(Netty, port = 8080 ,watchPaths = listOf("classes","resources")) {
module()
}.start(wait = true)
}
I'm using Ktor for my back end, I am planning to use auto reload and when I used engine main I've configured development mode in application.conf file. How to do the same in embedded server?
fun main() {
embeddedServer(Netty, port = 8080 ,watchPaths = listOf("classes","resources")) {
module()
}.start(wait = true)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想自动填充后端类/资源,则可以从IDE启动KTOR:
然后从命令行中运行
./ gradlew -t:<< backend-module>:classe 。
但是,如果要在完整的堆栈应用程序中自动填充前端,则您的主要方法可以是:
然后在frontend模块build.gradle.kts中添加以下
内容:然后在
<模块>/webpack.config.d
文件夹,例如称为watch.js:然后在运行上述前端自动加载主函数后,从iDE中,您可以从命令行中运行以下内容:
。 /gradlew -t:sample-ktor-react-js:build -x test -pjswatch
以下是一个工作示例: https://github.com/alexoooo/sample-ktor-reakct
If you want to auto-reload the backend classes/resources, you can launch Ktor from your IDE like this:
Then from the command line you can run
./gradlew -t :<backend-module>:classes
.However, if you want to auto-reload the frontend in a full stack application, your main methods can be:
Then add the following in the frontend module build.gradle.kts:
Then add a file inside the
<frontend-module>/webpack.config.d
folder, for example called watch.js:Then after running the above frontend auto-reload main function from the IDE, you can run the following from the command line:
./gradlew -t :sample-ktor-react-js:build -x test -PjsWatch
Here's a working example: https://github.com/alexoooo/sample-ktor-react
我尝试和工作的方法是创建自己的功能来创建嵌入式服务器。
基本上,我复制了EmbeddedServer.kt逻辑和裸露的开发模式。
如果使用Intellij,则需要按“构建”按钮进行工作,或启用自动构建。
但是,我认为不是一个好主意,因为您需要一种为其他环境更新此值的方法。因此,应该通过application.conf或gradle中的applicationDefaultjvmargs完成。
编辑:
回去尝试了KTOR的最新版本。您可以使用这样的东西。另外,请确保您拥有启用自动构建,否则服务器运行时每次都必须构建。
What I tried and worked is created my own functions to created an embededServer.
basically I copied EmbeddedServer.kt logic and exposed developmentMode.
If you are using Intellij you need to press the Build button to work, or enable auto build.
However i dont think is a good idea because you need a way to update this value for other environments. Thus it should be done by application.conf or from applicationDefaultJvmArgs in gradle.
Edit:
Went back and tried latest version for ktor. You can use something like this. Also make sure you have enable auto build or you will have to build each time while server is running.
在Intellijidea中,转到运行配置,将其添加到VM Options
-dio.ktor.ktor.development = false
In intellijIdea, go to Run COnfiguration, add this to VM Options
-Dio.ktor.development=false
对我来说,它在
application.conf
中使用此代码,这就是我检查它的方式
For me it worked with this code in
application.conf
and this is how i checked it