[英]How to aggregate akka-http routes using a trait?
I am trying to aggregate routes using a trait at runtime, so far I have 我正在尝试在运行时使用特征汇总路由,到目前为止
object SMController {
def aggregateRoutes(actorSystem: ActorSystem): List[Route] = {
val runtimeMirror = universe.runtimeMirror(getClass.getClassLoader)
val reflections = new Reflections("com.example.api")
val subclasses = reflections.getSubTypesOf(classOf[Routable])
val routesList = new ListBuffer[Route]()
for (i <- subclasses) {
val module = runtimeMirror.staticModule(i.getName)
val obj = runtimeMirror.reflectModule(module)
val someTrait: Routable = obj.instance.asInstanceOf[Routable]
routesList += someTrait.getAllRoutes(actorSystem)
}
routesList.toList
}
}
obviously above code doesn't work as the List of items cannot be passed to Http().bindAndHandle
. 显然,以上代码无法正常工作,因为不能将项目列表传递给Http().bindAndHandle
。
So my question is, how can I parse the List[Routes]
to a Http().bindAndHandle
accepts or how can I dynamically load routes from subclasses of Routable
? 所以我的问题是,如何解析List[Routes]
到Http().bindAndHandle
接受,或者如何动态地从Http().bindAndHandle
子类加载Routable
?
foldLeft: I have managed to foldLeft the routes and coalesce all the routes, as follows foldLeft:我设法将foldLeft路线合并到所有路线,如下所示
val routes = SMController.aggregateRoutes(system)
val bindingFuture = Http().bindAndHandle(
routes.tail.foldLeft(routes.head)((a, b) => a ~ b), "localhost", 8080)
reduceLeft: reduceLeft:
routes.reduceLeft(_ ~ _)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.