akka spray -> akka http migration

188 views Asked by At

this is fragment of my router in spray based service:

path(baseUrl / version / "apps" / Segment / "users" / Segment) { (app, user) =>
  respondWithMediaType(MediaTypes.`application/json`) { ctx =>
    createProxy(ctx, management, GetUser(appId = UUID.fromString(app), userId = UUID.fromString(user)))
  }
}

...

def createProxy(ctx: RequestContext, service: ActorRef, message: AnyRef): Unit = {
  val ref = actorRefFactory.actorOf(ResponseProxy.props(ctx))
  service.tell(message, ref)
}

...

object ResponseProxy {
  def props(ctx: RequestContext): Props = Props(new ResponseProxy(ctx))
}

class ResponseProxy(ctx: RequestContext) extends Actor {

  import concurrent.duration._

  context setReceiveTimeout 30.second

  def receive = {
    case ReceiveTimeout =>
      ctx.complete(mapping.mapper.get.writeValueAsString(ResponseMessage("timeout")))
      context stop self

    case x: HttpResponse => ctx.complete(x)

    case x: AnyRef =>
      ctx.complete(mapping.mapper.get.writeValueAsString(x))
      context stop self
  }
}

i try to migrate on akka http, this is part of route:

get {
    pathPrefix(baseUrl / version / "apps" / JavaUUID / "users" / JavaUUID ) { (app : UUID , user : UUID) =>
      pathEndOrSingleSlash { ctx =>
        createProxy(ctx, management, GetUser(appId = app, userId = user))
    }
}

how i can return JSON?

there is no solution for actor based request processing? only on Future?

0

There are 0 answers