使用Actor进行Akka-Http响应-如何完成cookie?

时间:2018-07-13 13:53:17

标签: scala httpresponse akka-http akka-actor

我有这个决定:

final class ImperativeRequestContext(ctx: RequestContext, promise: Promise[RouteResult]) {
  private implicit val ec: ExecutionContextExecutor = ctx.executionContext

  def complete(obj: ToResponseMarshallable): Unit = ctx.complete(obj).onComplete(promise.complete)

  def fail(error: Throwable): Unit = ctx.fail(error).onComplete(promise.complete)
}

object RequestHandler {
  def props(ctx: ImperativeRequestContext, msg: AnyRef): Props = Props(new RequestHandler(ctx, msg))

  def actorComplete(inner: ImperativeRequestContext => Unit): Route = { ctx: RequestContext =>
    val p = Promise[RouteResult]
    inner(new ImperativeRequestContext(ctx, p))
    p.future
  }
}

class RequestHandler(ctx: ImperativeRequestContext, msg: AnyRef) extends Actor with ActorLogging with JsonSupport {

  import context._

  import concurrent.duration._

  setReceiveTimeout(10.seconds)

  actorSelection("/user/service") ! msg

  def receive: Receive = {
    case x: SuccessMessage =>
      ctx.complete(x)
      stop(self)

    case x: ErrorMessage =>
      ctx.fail(new Exception(x.msg))
      stop(self)

    case _ => stop(self)
  }
}

可以让这样的每个请求成为演员

  private val route: Route =
...
pathEndOrSingleSlash {
            actorComplete(ctx => context.actorOf(RequestHandler.props(ctx, CheckMessage())))
          }
...

我在理解如何实施Cookie设置时遇到了一些麻烦 在SuccessMessage上获得响应

喜欢

val route =
  setCookie(HttpCookie("userName", value = "paul")) {
    complete("The user was logged in")
  }

我遇到了麻烦,因为我们重写了完整方法。 在收到SuccessMessage消息之前,需要设置的cookie值未知。

请帮助!

0 个答案:

没有答案