我正试图在finagle中托管静态资源,比如javascript和css文件。
我设法让它工作,但我必须在路由服务中专门配置每个路由到资源文件夹。例如:
def build():RoutingService[Request with Request] = {
val routingService = RoutingService.byPathObject {
case Root => ControllerRegistry.rootController.root()
case Root / "public" / resource => ControllerRegistry.publicController.findPublic()
case Root / "public" / "bootstrap"/ "css" / resource => ControllerRegistry.publicController.findPublic()
}
routingService
}
和
def findPublic(): Service[Request, Response] = {
val findPublic = new Service[Request, Response] {
def apply(request: Request) = {
Future {
val resource = Path(request.path) match {
case Root / "public" / resource => getResourceText(s"/public/$resource")
case Root / "public" / "bootstrap" / "css" / resource => getResourceText(s"/public/bootstrap/css/$resource")
case _ => throw new IllegalStateException
}
val response = Response()
response.setContent(copiedBuffer(resource, UTF_8))
response
}
}
}
findPublic
}
现在我可以在public
和public/bootstrap/css
中获取任何资源,但如果没有更多配置,我就无法获得public/bootstrap/js
。