akka-http:完整的请求流 [英] akka-http: complete request with flow

查看:37
本文介绍了akka-http:完整的请求流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经设置了一个任意复杂的Flow[HttpRequest, HttpResponse, Unit].

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)

现在我想添加日志,利用一些现有的指令,比如 logRequestResult("my-service"){...}有没有办法将此指令与我的流程结合起来?我想我正在寻找另一个指令,类似于

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

这可能吗?

注意:logRequestResult 是一个例子,我的问题适用于任何可能有用的指令.

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

推荐答案

事实证明,一种(也许是唯一的)方法是连接并具体化一个新流,并将提取的请求提供给它.下面的例子

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)

这篇关于akka-http:完整的请求流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆