I have a requirement where a client calls a post REST endpoint created via akka http. As soon as the request is in the post method, I need to pass the post object to the stream (consisting of source, several flows and sink, etc) and get back the response from the sink so that I can return the response back to the client.
I have been going through some articles and have seen the below code but have a concern that I don't want to materialize the stream for every request. I only want to materialize a stream and keep on passing the elements to that stream.
Below is the high level of what I saw:
val route: Route =
path("dummy path") { p =>
get {
(extract(_.request) & extractMaterializer) { (req, mat) ⇒
**Source.single(req).runWith(sink)(mat)**
complete {
s"<h1>Say hello to akka-http. p=$p</h1>"
}
}
}
}
I was thinking of creating an actor and passing the object to that actor. I can create a source from Source.actorRef and connect several flows with this source. But I am not sure, how to get back the response from the sink. Something like:
val actor: ActorRef = some actor
Source.actorRef(actor).via(flows).to(Sink).run() --> materialized stream
val route: akka.http.scaladsl.server.Route =
path("post" / Segment) { p =>
post {
(extract(_.request) & extractMaterializer) { (req, mat) ⇒
response = actor.ask(message) --> get back the response
complete {
response
}
}
}
}
Or, is there anything else that I can incorporate in my use-case.