如何使用特征聚合 akka-http 路由?

How to aggregate akka-http routes using a trait?

我正在尝试在运行时使用特征聚合路由,到目前为止我已经

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
  }
}

显然上面的代码不起作用,因为项目列表无法传递给 Http().bindAndHandle

所以我的问题是,如何将 List[Routes] 解析为 Http().bindAndHandle 接受或如何从 Routable 的子类动态加载路由?

左折: 我已经设法将路线 foldLeft 并合并所有路线,如下

val routes = SMController.aggregateRoutes(system)
val bindingFuture = Http().bindAndHandle(
        routes.tail.foldLeft(routes.head)((a, b) => a ~ b), "localhost", 8080)

reduceLeft:

routes.reduceLeft(_ ~ _)