2014-06-16 23:09:51 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package docs.http
|
|
|
|
|
|
|
|
|
|
import akka.actor.ActorSystem
|
2014-08-26 09:03:48 +02:00
|
|
|
import akka.http.model._
|
2015-03-26 21:38:27 +02:00
|
|
|
import akka.stream.scaladsl._
|
2014-08-26 09:03:48 +02:00
|
|
|
import akka.stream.testkit.AkkaSpec
|
2015-03-26 21:38:27 +02:00
|
|
|
import scala.concurrent.Future
|
|
|
|
|
import akka.http.Http
|
2014-06-16 23:09:51 +02:00
|
|
|
|
|
|
|
|
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
|
2015-01-27 18:29:20 +01:00
|
|
|
import akka.stream.ActorFlowMaterializer
|
2014-06-16 23:09:51 +02:00
|
|
|
|
|
|
|
|
implicit val system = ActorSystem()
|
2015-01-27 18:29:20 +01:00
|
|
|
implicit val materializer = ActorFlowMaterializer()
|
2014-06-16 23:09:51 +02:00
|
|
|
|
2015-03-26 21:38:27 +02:00
|
|
|
val serverSource: Source[Http.IncomingConnection, Future[Http.ServerBinding]] =
|
|
|
|
|
Http(system).bind(interface = "localhost", port = 8080)
|
|
|
|
|
val bindingFuture: Future[Http.ServerBinding] = serverSource.to(Sink.foreach { connection =>
|
|
|
|
|
// foreach materializes the source
|
2014-11-27 14:00:39 +01:00
|
|
|
println("Accepted new connection from " + connection.remoteAddress)
|
2015-01-28 14:19:50 +01:00
|
|
|
// ... and then actually handle the connection
|
2015-03-26 21:38:27 +02:00
|
|
|
}).run()
|
2014-06-16 23:09:51 +02:00
|
|
|
//#bind-example
|
|
|
|
|
}
|
2014-11-28 10:41:57 +01:00
|
|
|
|
2014-06-16 23:09:51 +02:00
|
|
|
"full-server-example" in {
|
|
|
|
|
import akka.http.Http
|
2015-01-27 18:29:20 +01:00
|
|
|
import akka.stream.ActorFlowMaterializer
|
2014-06-16 23:09:51 +02:00
|
|
|
|
|
|
|
|
implicit val system = ActorSystem()
|
2015-01-27 18:29:20 +01:00
|
|
|
implicit val materializer = ActorFlowMaterializer()
|
2014-06-16 23:09:51 +02:00
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
val serverSource = Http(system).bind(interface = "localhost", port = 8080)
|
2014-06-16 23:09:51 +02:00
|
|
|
|
|
|
|
|
//#full-server-example
|
2014-08-26 09:03:48 +02:00
|
|
|
import akka.http.model.HttpMethods._
|
2015-01-28 14:19:50 +01:00
|
|
|
import akka.stream.scaladsl.{ Flow, Sink }
|
2014-06-16 23:09:51 +02:00
|
|
|
|
2014-12-22 16:18:26 +01:00
|
|
|
val requestHandler: HttpRequest => HttpResponse = {
|
|
|
|
|
case HttpRequest(GET, Uri.Path("/"), _, _, _) =>
|
2014-06-16 23:09:51 +02:00
|
|
|
HttpResponse(
|
|
|
|
|
entity = HttpEntity(MediaTypes.`text/html`,
|
2014-06-25 14:32:29 +02:00
|
|
|
"<html><body>Hello world!</body></html>"))
|
2014-06-16 23:09:51 +02:00
|
|
|
|
2015-03-26 21:38:27 +02:00
|
|
|
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!")
|
2014-06-16 23:09:51 +02:00
|
|
|
}
|
|
|
|
|
|
2015-03-26 21:38:27 +02:00
|
|
|
val bindingFuture: Future[Http.ServerBinding] = serverSource.to(Sink.foreach { connection =>
|
2014-11-27 14:00:39 +01:00
|
|
|
println("Accepted new connection from " + connection.remoteAddress)
|
2014-11-28 10:41:57 +01:00
|
|
|
|
2014-12-18 09:25:33 +01:00
|
|
|
connection handleWithSyncHandler requestHandler
|
|
|
|
|
// this is equivalent to
|
|
|
|
|
// connection handleWith { Flow[HttpRequest] map requestHandler }
|
2015-01-28 14:19:50 +01:00
|
|
|
}).run()
|
2014-06-16 23:09:51 +02:00
|
|
|
//#full-server-example
|
|
|
|
|
}
|
|
|
|
|
}
|