Skip to content

Instantly share code, notes, and snippets.

@ane
Created October 11, 2016 07:43
Show Gist options
  • Save ane/6afa45e2ab1eb2ab97222b1537bc2bb5 to your computer and use it in GitHub Desktop.
Save ane/6afa45e2ab1eb2ab97222b1537bc2bb5 to your computer and use it in GitHub Desktop.
val end: Sink[Foo, NotUsed] = ???
val route = Flow.fromGraph(GraphDSL.create(end) { implicit builder => dest =>
import GraphDSL.Implicits._
// todo: errors, request validation, etc.
val entityFlow = builder.add(Flow[HttpRequest].mapAsync(1)(r => Unmarshal(r.entity).to[Foo]))
val responseFlow = builder.add(Flow[Foo].map(_ => HttpResponse(StatusCodes.Created)))
val bcast = builder.add(Broadcast[Foo](2))
entityFlow.out ~> bcast.in
bcast.out(0) ~> dest.in
bcast.out(1) ~> responseFlow.in
FlowShape(entityFlow.in, responseFlow.out)
})
Http().bindAndHandle(route, "localhost", 8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment