Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

akka-http: complete request with flow

Assume I have set up an arbitrarily complex Flow[HttpRequest, HttpResponse, Unit].

I can already use said flow to handle incoming requests with

Http().bindAndHandle(flow, "0.0.0.0", 8080)

Now I would like to add logging, leveraging some existing directive, like logRequestResult("my-service"){...} Is there a way to combine this directive with my flow? I guess I am looking for another directive, something along the lines of

def completeWithFlow(flow: Flow): Route

Is this possible at all?

N.B.: logRequestResult is an example, my question applies to any Directive one might find useful.

like image 670
Stefano Bonetti Avatar asked Mar 29 '16 20:03

Stefano Bonetti


1 Answers

Turns out one (and maybe the only) way is to wire and materialize a new flow, and feed the extracted request to it. Example below

  val myFlow: Flow[HttpRequest, HttpResponse, NotUsed] = ???

  val route =
    get {
      logRequestResult("my-service") {
        extract(_.request) { req ⇒
          val futureResponse = Source.single(req).via(myFlow).runWith(Sink.head)
          complete(futureResponse)
        }
      }
    }

  Http().bindAndHandle(route, "127.0.0.1", 9000)
like image 101
Stefano Bonetti Avatar answered Oct 05 '22 19:10

Stefano Bonetti