akka-http: complete request with flow

杀马特。学长 韩版系。学妹 提交于 2019-11-30 07:12:49

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)

http://doc.akka.io/docs/akka/2.4.2/scala/http/routing-dsl/overview.html

Are you looking for route2HandlerFlow or Route.handlerFlow ?

I believe Route.handlerFlow will work based on implicits.

eg /

val serverBinding = Http().bindAndHandle(interface = "0.0.0.0", port = 8080, 
handler = route2HandlerFlow(mainFlow()))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!