class ResponseActor extends Actor {
def receive = {
case _ => sender ! HttpResponse(entity = HttpEntity(MediaTypes.`text/html`, "<html><body>Hello!</body></html>"))
}
}
object ReactiveInventoryService extends App {
implicit val system = ActorSystem()
implicit val executor = system.dispatcher
implicit val materializer = ActorFlowMaterializer()
implicit val timeout = Timeout(5 seconds)
val config = ConfigFactory.load()
val responseHandler = system.actorOf(Props[ResponseActor])
val requestHandler: HttpRequest => Future[HttpResponse] = {
httpRequest: HttpRequest => ask(responseHandler, httpRequest).mapTo[HttpResponse]
}
val serverBinding: Source[IncomingConnection, Future[ServerBinding]] = Http().bind(config.getString("http.interface"), config.getInt("http.port"))
serverBinding.runForeach { connection: IncomingConnection =>
println("Accepted new connection from " + connection.remoteAddress)
connection handleWithAsyncHandler requestHandler
}
}
httpRequest: HttpRequest => ask(responseHandler, httpRequest).mapTo[HttpResponse]
What do I need to do to give the IncomingConnection to an actor and have it send back the response without having to use ask and return the HttpResponse back to the request handler?