首页 > 解决方案 > Akka如何根据参数值有条件地转发请求

问题描述

我有一个类似的现有路线:

pathPrefix("foo") {
  pathPrefix(IntNumber) { id =>
    post {
      entity(as[FooReqeust]) { request =>
        val f = doSomething(id, request)
        onComplete(f) {
          case Success(res) => complete(res)
          case Failure(e) => complete(HttpResponse(InternalServerError, entity = "someError"))
        }
      }
    }
  }
}

我们有一个新服务(在这个服务之外),我们希望在运行一些测试后开始路由到它。新的服务标头/参数/帖子正文等与现有的相同

id如果回答某些条件,我想将来自现有端点的请求转发到新端点。

我不清楚如何实现这一点。(并希望避免序列化/反序列化检查主体等)

例如:

pathPrefix("foo") {
  pathPrefix(IntNumber) { id =>
    post {
      if (id == 1234) complete(usingNewService(...)) <<<< blindly complete with the new service
      else  // use the current implementation
      entity(as[FooReqeust]) { request =>
        .... same code as before ...
      }
    }
  }
}

标签: scalaakkaakka-http

解决方案


推荐阅读