简单的问题。是否可以创建端点而无需@Endpoint
?
我想通过文件并根据其上下文的内容创建相当动态的端点。
Thanks!
更新一下我的想法。我想创建类似插件系统的东西,以使我的应用程序对于维护和未来功能更具可扩展性。
值得一提的是,我正在使用 Micronaut 和 Kotlin。现在我已经有了固定定义的端点,它与我的命令脚本相匹配。
我的描述文件将位于/src/main/resources
我有以下example描述文件的样子。
ENDPOINT: GET /myapi/customendpoint/version
COMMAND: """
#!/usr/bin/env bash
# This will be executed via SSH and streamed to stdout for further handling
echo "1.0.0"
"""
# This is a template JSON which will generate a JSON as production on the endpoint
OUTPUT: """
{
"version": "Server version: $RESULT"
}
"""
我希望如何使其与应用程序一起工作。
import io.micronaut.docs.context.events.SampleEvent
import io.micronaut.context.event.StartupEvent
import io.micronaut.context.event.ShutdownEvent
import io.micronaut.runtime.event.annotation.EventListener
@Singleton
class SampleEventListener {
/*var invocationCounter = 0
@EventListener
internal fun onSampleEvent(event: SampleEvent) {
invocationCounter++
}*/
@EventListener
internal fun onStartupEvent(event: StartupEvent) {
// 1. I read all my description files
// 2. Parse them (for what I created a parser)
// 3. Now the tricky part, how to add those information to Micronaut Runtime
val do = MyDescription() // After I parsed
// Would be awesome if it is that simple! :)
Micronaut.addEndpoint(
do.getEndpoint(), do.getHttpOption(),
MyCustomRequestHandler(do.getCommand()) // Maybe there is a base class for inheritance?
)
}
@EventListener
internal fun onShutdownEvent(event: ShutdownEvent) {
// shutdown logic here
}
}
您可以创建一个自定义RouteBuilder
这将在运行时注册您的自定义端点:
@Singleton
class CustomRouteBuilder extends DefaultRouteBuilder {
@PostConstruct
fun initRoutes() {
val do = MyDescription();
val method = do.getMethod();
val routeUri = do.getEndpoint();
val routeHandle = MethodExecutionHandle<Object, Object>() {
// implement the 'MethodExecutionHandle' in a suitable manner to invoke the 'do.getCommand()'
};
buildRoute(HttpMethod.parse(method), routeUri, routeHandle);
}
}
请注意,虽然这仍然可行,但最好考虑另一个扩展路径,因为该解决方案会破坏整个解决方案米克罗特作为 AOT 编译框架的哲学。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)