How to host static resources in finagle

1.2k views Asked by At

I am trying to host static resources, like javascript and css files, in finagle.

I've managed to get it to work, but I have to specifically configure every route to a resource folder in my routing service. For example:

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
    }

and

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
    }

Now I can get any resource in public and public/bootstrap/css, but I can't get public/bootstrap/js without more configuration.

1

There are 1 answers

1
Steve Gury On BEST ANSWER

TLDR: Finagle is not exactly the right library to do what you want. You could use something like Finatra which is built on top of finagle.

Long version: Finagle is designed to build distributed systems, it's not a web framework like ruby on rails (even if finagle-http provides very basic feature for doing that). It makes easy to build services that interact with each other (and take care of load-balancing, timeout, disconnection, back-pressure, distributed tracing, ...) We have, at Twitter, a web framework library built on top of finagle, but it's not yet open-sourced, in the meantime you can use Finatra.