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
.
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
?