pekko/akka-docs-dev/rst/scala/code/docs/http/HttpServerExampleSpec.scala
Björn Antonsson 672d4ed091 +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
2014-11-28 10:41:57 +01:00

67 lines
2 KiB
Scala

/*
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package docs.http
import akka.actor.ActorSystem
import akka.http.model._
import akka.stream.testkit.AkkaSpec
class HttpServerExampleSpec
extends AkkaSpec("akka.actor.default-mailbox.mailbox-type = akka.dispatch.UnboundedMailbox") {
def ActorSystem(): ActorSystem = system
"binding example" in {
//#bind-example
import akka.http.Http
import akka.stream.FlowMaterializer
implicit val system = ActorSystem()
implicit val materializer = FlowMaterializer()
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
}
//#bind-example
}
"full-server-example" in {
import akka.http.Http
import akka.stream.FlowMaterializer
implicit val system = ActorSystem()
implicit val materializer = FlowMaterializer()
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("/"), _, _, _)
HttpResponse(
entity = HttpEntity(MediaTypes.`text/html`,
"<html><body>Hello world!</body></html>"))
case HttpRequest(GET, Uri.Path("/ping"), _, _, _) HttpResponse(entity = "PONG!")
case HttpRequest(GET, Uri.Path("/crash"), _, _, _) sys.error("BOOM!")
case _: HttpRequest HttpResponse(404, entity = "Unknown resource!")
}
// ...
source.foreach {
case Http.IncomingConnection(remoteAddress, flow)
println("Accepted new connection from " + remoteAddress)
flow.join(Flow[HttpRequest].map(requestHandler)).run()
}
//#full-server-example
}
}