Quarkus - 按需启动 Web 服务器
有一个用 Quarkus 编写的 CLI 应用程序,其工作原理如下:
$ quarkus-app --input Hello
Output: olleH
现在还应该可以通过 REST 调用它。但是,只有在传递了参数“--server”时,才应启动此服务器:
$ quarkus-app --server &
Listening on '127.0.0.1:7070'
$ curl http://127.0.0.1:7070/api/reverse?input=Hello
Output: olleH
现在如何让 WebServer 仅在“--server”上启动?
我的代码目前如下所示:
@QuarkusMain
class CommandLine : QuarkusApplication {
override fun run(vararg args: String?): Int {
val options: Options = Options()
options.addOption("i", "input", false, "Input to reverse")
options.addOption("s", "server", false, "Start server")
val parser: CommandLineParser = DefaultParser()
val cmd: CommandLine = parser.parse(options, args)
if (cmd.hasOption("server")) {
// TODO: Start server
} else if (cmd.hasOption("input")) {
println("Output: ${cmd.getOptionValue("input").toString().reversed()}")
}
return 0
}
}
There is a CLI application written in Quarkus, it works as follows:
$ quarkus-app --input Hello
Output: olleH
Now there should also be the possibility to call this via a REST. However, this server should only be stared if the argument "--server" is passed:
$ quarkus-app --server &
Listening on '127.0.0.1:7070'
$ curl http://127.0.0.1:7070/api/reverse?input=Hello
Output: olleH
Now how do I get the WebServer to start only on "--server"?
My code currently looks like this:
@QuarkusMain
class CommandLine : QuarkusApplication {
override fun run(vararg args: String?): Int {
val options: Options = Options()
options.addOption("i", "input", false, "Input to reverse")
options.addOption("s", "server", false, "Start server")
val parser: CommandLineParser = DefaultParser()
val cmd: CommandLine = parser.parse(options, args)
if (cmd.hasOption("server")) {
// TODO: Start server
} else if (cmd.hasOption("input")) {
println("Output: ${cmd.getOptionValue("input").toString().reversed()}")
}
return 0
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可以通过定义
QuarkusMain
并稍后调用Quarkus.run()
来实现。此外,当然必须安装 io.quarkus:quarkus-resteasy 并定义资源:
This is possible by defining a
QuarkusMain
and later callingQuarkus.run()
.In addition,
io.quarkus:quarkus-resteasy
must of course be installed and a resource must be defined: