我正在尝试在 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
and中获取任何资源,但如果没有更多配置public/bootstrap/css
,我将无法获取。public/bootstrap/js