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

查看:105
本文介绍了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

这可能吗?

NB: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天全站免登陆