+str,htc #16071, #16072 New Stream Tcp and Http API

* StreamTcp and Http extensions now return Flows and Sources that can be materialized later
* Flow can now be completed with another flow to be turned into a runnable flow
This commit is contained in:
Björn Antonsson 2014-11-28 10:41:57 +01:00
parent cac9137aa9
commit 672d4ed091
43 changed files with 1327 additions and 1236 deletions

View file

@ -7,9 +7,6 @@ package docs.http
import akka.actor.ActorSystem
import akka.http.model._
import akka.stream.testkit.AkkaSpec
import akka.util.Timeout
import scala.concurrent.duration._
class HttpServerExampleSpec
extends AkkaSpec("akka.actor.default-mailbox.mailbox-type = akka.dispatch.UnboundedMailbox") {
@ -18,45 +15,33 @@ class HttpServerExampleSpec
"binding example" in {
//#bind-example
import akka.http.Http
import akka.io.IO
import akka.pattern.ask
import akka.stream.FlowMaterializer
import akka.stream.scaladsl.Source
implicit val system = ActorSystem()
import system.dispatcher
implicit val materializer = FlowMaterializer()
implicit val askTimeout: Timeout = 500.millis
val bindingFuture = IO(Http) ? Http.Bind(interface = "localhost", port = 8080)
bindingFuture foreach {
case Http.ServerBinding(localAddress, connectionStream)
Source(connectionStream).foreach({
case Http.IncomingConnection(remoteAddress, requestProducer, responseConsumer)
println("Accepted new connection from " + remoteAddress)
val Http.ServerSource(source, serverBindingKey) = Http(system).bind(interface = "localhost", port = 8080)
source.foreach {
case Http.IncomingConnection(remoteAddress, flow)
println("Accepted new connection from " + remoteAddress)
// handle connection here
})
// handle connection here
}
//#bind-example
}
"full-server-example" in {
import akka.http.Http
import akka.io.IO
import akka.pattern.ask
import akka.stream.FlowMaterializer
import akka.stream.scaladsl.Source
import akka.stream.scaladsl.Sink
implicit val system = ActorSystem()
import system.dispatcher
implicit val materializer = FlowMaterializer()
implicit val askTimeout: Timeout = 500.millis
val bindingFuture = IO(Http) ? Http.Bind(interface = "localhost", port = 8080)
val Http.ServerSource(source, serverBindingKey) = Http(system).bind(interface = "localhost", port = 8080)
//#full-server-example
import akka.http.model.HttpMethods._
import akka.stream.scaladsl.Flow
val requestHandler: HttpRequest HttpResponse = {
case HttpRequest(GET, Uri.Path("/"), _, _, _)
@ -70,14 +55,12 @@ class HttpServerExampleSpec
}
// ...
bindingFuture foreach {
case Http.ServerBinding(localAddress, connectionStream)
Source(connectionStream).foreach({
case Http.IncomingConnection(remoteAddress, requestProducer, responseConsumer)
println("Accepted new connection from " + remoteAddress)
Source(requestProducer).map(requestHandler).to(Sink(responseConsumer)).run()
})
source.foreach {
case Http.IncomingConnection(remoteAddress, flow)
println("Accepted new connection from " + remoteAddress)
flow.join(Flow[HttpRequest].map(requestHandler)).run()
}
//#full-server-example
}