Search code examples
akka-streamakka-http

Complete route with source and get its materialised value


There's a route that goes somewhat like this:

val route = 
  path("data") {
    get {
      val src: Source[ByteString, BoundedSourceQueue[ByteString]] = ???
      complete(HttpEntity(ContentTypes.`application/octet-stream`, src))
    }
  }

How can I access materialised value of this source?


Solution

  • Prematerializing src should do the trick.

    // will need an implicit ActorSystem/ActorMaterializer in scope
    val baseSrc: Source[ByteString, BoundedSourceQueue[ByteString]] = ???
    val (bsq, src) = baseSrc.preMaterialize()
    // do stuff with bsq...
    complete(HttpEntity(ContentTypes.`application/octet-stream`, src))