sscarduzio
sscarduzio

Reputation: 6188

Applying a filter after a service in Finagle

I am able to apply all kind of pre-processing of incoming HTTP requests by adding filters before a HTTP service with filter1 andThen httpService i.e.

val addRequestHeaderFilter = new SimpleFilter[Request, Response] {
    def apply(req: Request, service: Service[Request, Response]) = {
      req.headerMap.add("header1","header_value")
      service(req)
    }      
 }
 Http.serve(":5000", addRequestHeaderFilter andThen theActualHTTPService) 

The problem is, I want to add headers to the Response. Is there a post-service filter? You can't really do service andThen service.

Upvotes: 2

Views: 1104

Answers (1)

sscarduzio
sscarduzio

Reputation: 6188

My fault: I was reasoning about filters like they were streams. So this works:

val addRequestHeaderFilter = new SimpleFilter[Request, Response] {
    def apply(req: Request, service: Service[Request, Response]) = {
      val respFuture = service(req)
      respFuture.map(_.headerMap.add("header1","header_value"))
      respFuture
    }      
 }
 Http.serve(":5000", addRequestHeaderFilter andThen theActualHTTPService) 

In the fitler, I'm applying the service and manipulating its response before returning it. And this behaves like a pre-service filter, that is, no need to invert the andThen functional composition.

Finagle is really cool <3

Upvotes: 4

Related Questions