This should not be too hard. Regarding the filename: it depends on what API you want. For the actual chunked sending, I would recommend working with byte buffers if you want good performance.
This should get you going. But careful since it exposes your entire machine via port 8080 https://gist.github.com/rklaehn/3f26c3f80e5870831f52 to test, use e.g. curl http://localhost:8080/Users/rklaehn/tmp/test Curl has some options to simulate low bandwidth connections which allow you to experiment with the chunked encoding. On Wed, Nov 19, 2014 at 6:49 PM, Allan Brighton <[email protected]> wrote: > Hi, > > I'd like to make an akka-http based file server that returns requested large > binary files > (using chunking and ByteStrings), but haven't found any suitable examples > yet. > So something like this: > > case class FileServer(interface: String, port: Int) { > implicit val system = ActorSystem() > import system.dispatcher > implicit val materializer = FlowMaterializer() > implicit val askTimeout: Timeout = 500.millis > > IO(Http) ? Http.Bind(interface, port) foreach { > case Http.ServerBinding(localAddress, connectionStream) ⇒ > Source(connectionStream).foreach({ > case Http.IncomingConnection(remoteAddress, requestProducer, > responseConsumer) ⇒ > > Source(requestProducer).map(requestHandler).to(Sink(responseConsumer)).run() > }) > } > > val requestHandler: HttpRequest ⇒ HttpResponse = { > case HttpRequest(GET, _, _, _, _) ⇒ > val filename = ??? // how to get file name from URI path or use id > attribute? > def output = scala.io.Source.fromFile(filename) > > HttpResponse( > entity = HttpEntity.Chunked(ContentTypes.`application/octet-stream`, > Flow(???) // how to return the file contents in chunks of > ByteStrings? > ) > > case _: HttpRequest ⇒ HttpResponse(404, entity = "Unknown resource!") > } > } > > Any suggestions on how to implement this? Any examples for the client side? > > Thanks, > Allan > > > -- >>>>>>>>>>> Read the docs: http://akka.io/docs/ >>>>>>>>>>> Check the FAQ: >>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html >>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user > --- > You received this message because you are subscribed to the Google Groups > "Akka User List" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/akka-user. > For more options, visit https://groups.google.com/d/optout. -- >>>>>>>>>> Read the docs: http://akka.io/docs/ >>>>>>>>>> Check the FAQ: >>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html >>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user --- You received this message because you are subscribed to the Google Groups "Akka User List" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/akka-user. For more options, visit https://groups.google.com/d/optout.
