Made imports in docs snippets more complete and a few minor improvements
This commit is contained in:
parent
87a9196088
commit
bf76d6c389
15 changed files with 197 additions and 161 deletions
|
|
@ -5,10 +5,6 @@
|
|||
package docs.http.scaladsl
|
||||
|
||||
import akka.event.LoggingAdapter
|
||||
import akka.http.scaladsl.Http.ServerBinding
|
||||
import akka.http.scaladsl.model._
|
||||
import akka.stream.ActorMaterializer
|
||||
import akka.stream.scaladsl.{ Flow, Sink }
|
||||
import akka.testkit.TestActors
|
||||
import docs.CompileOnlySpec
|
||||
import org.scalatest.{ Matchers, WordSpec }
|
||||
|
|
@ -44,37 +40,50 @@ class HttpServerExampleSpec extends WordSpec with Matchers
|
|||
"binding-failure-high-level-example" in compileOnlySpec {
|
||||
import akka.actor.ActorSystem
|
||||
import akka.http.scaladsl.Http
|
||||
import akka.http.scaladsl.Http.ServerBinding
|
||||
import akka.http.scaladsl.server.Directives._
|
||||
import akka.stream.ActorMaterializer
|
||||
|
||||
implicit val system = ActorSystem()
|
||||
implicit val materializer = ActorMaterializer()
|
||||
// needed for the future onFailure in the end
|
||||
implicit val executionContext = system.dispatcher
|
||||
import scala.concurrent.Future
|
||||
|
||||
val handler = get {
|
||||
complete("Hello world!")
|
||||
object WebServer {
|
||||
def main(args: Array[String]) {
|
||||
implicit val system = ActorSystem()
|
||||
implicit val materializer = ActorMaterializer()
|
||||
// needed for the future onFailure in the end
|
||||
implicit val executionContext = system.dispatcher
|
||||
|
||||
val handler = get {
|
||||
complete("Hello world!")
|
||||
}
|
||||
|
||||
// let's say the OS won't allow us to bind to 80.
|
||||
val (host, port) = ("localhost", 80)
|
||||
val bindingFuture: Future[ServerBinding] =
|
||||
Http().bindAndHandle(handler, host, port)
|
||||
|
||||
bindingFuture.onFailure {
|
||||
case ex: Exception =>
|
||||
log.error(ex, "Failed to bind to {}:{}!", host, port)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// let's say the OS won't allow us to bind to 80.
|
||||
val (host, port) = ("localhost", 80)
|
||||
val bindingFuture: Future[ServerBinding] =
|
||||
Http().bindAndHandle(handler, host, port)
|
||||
|
||||
bindingFuture.onFailure {
|
||||
case ex: Exception =>
|
||||
log.error(ex, "Failed to bind to {}:{}!", host, port)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// mock values:
|
||||
import akka.http.scaladsl.Http
|
||||
import akka.actor.ActorSystem
|
||||
val handleConnections: Sink[Http.IncomingConnection, Future[Http.ServerBinding]] =
|
||||
val handleConnections = {
|
||||
import akka.stream.scaladsl.Sink
|
||||
Sink.ignore.mapMaterializedValue(_ => Future.failed(new Exception("")))
|
||||
}
|
||||
|
||||
"binding-failure-handling" in compileOnlySpec {
|
||||
import akka.actor.ActorSystem
|
||||
import akka.http.scaladsl.Http
|
||||
import akka.http.scaladsl.Http.ServerBinding
|
||||
import akka.stream.ActorMaterializer
|
||||
|
||||
import scala.concurrent.Future
|
||||
|
||||
implicit val system = ActorSystem()
|
||||
implicit val materializer = ActorMaterializer()
|
||||
// needed for the future onFailure in the end
|
||||
|
|
@ -102,11 +111,8 @@ class HttpServerExampleSpec extends WordSpec with Matchers
|
|||
import akka.actor.ActorSystem
|
||||
import akka.actor.ActorRef
|
||||
import akka.http.scaladsl.Http
|
||||
import akka.http.scaladsl.model.HttpEntity
|
||||
import akka.http.scaladsl.model.ContentTypes
|
||||
import akka.http.scaladsl.server.Directives._
|
||||
import akka.stream.ActorMaterializer
|
||||
import scala.io.StdIn
|
||||
import akka.stream.scaladsl.Flow
|
||||
|
||||
implicit val system = ActorSystem()
|
||||
implicit val materializer = ActorMaterializer()
|
||||
|
|
@ -132,8 +138,9 @@ class HttpServerExampleSpec extends WordSpec with Matchers
|
|||
"connection-stream-failure-handling" in compileOnlySpec {
|
||||
import akka.actor.ActorSystem
|
||||
import akka.http.scaladsl.Http
|
||||
import akka.http.scaladsl.model.{ ContentTypes, HttpEntity }
|
||||
import akka.http.scaladsl.model._
|
||||
import akka.stream.ActorMaterializer
|
||||
import akka.stream.scaladsl.Flow
|
||||
|
||||
implicit val system = ActorSystem()
|
||||
implicit val materializer = ActorMaterializer()
|
||||
|
|
@ -203,6 +210,7 @@ class HttpServerExampleSpec extends WordSpec with Matchers
|
|||
}
|
||||
|
||||
"low-level-server-example" in compileOnlySpec {
|
||||
import akka.actor.ActorSystem
|
||||
import akka.http.scaladsl.Http
|
||||
import akka.http.scaladsl.model.HttpMethods._
|
||||
import akka.http.scaladsl.model._
|
||||
|
|
@ -286,7 +294,9 @@ class HttpServerExampleSpec extends WordSpec with Matchers
|
|||
}
|
||||
|
||||
"minimal-routing-example" in compileOnlySpec {
|
||||
import akka.actor.ActorSystem
|
||||
import akka.http.scaladsl.Http
|
||||
import akka.http.scaladsl.model._
|
||||
import akka.http.scaladsl.server.Directives._
|
||||
import akka.stream.ActorMaterializer
|
||||
import scala.io.StdIn
|
||||
|
|
@ -319,13 +329,14 @@ class HttpServerExampleSpec extends WordSpec with Matchers
|
|||
|
||||
"long-routing-example" in compileOnlySpec {
|
||||
//#long-routing-example
|
||||
import akka.actor.ActorRef
|
||||
import akka.actor.{ActorRef, ActorSystem}
|
||||
import akka.http.scaladsl.coding.Deflate
|
||||
import akka.http.scaladsl.marshalling.ToResponseMarshaller
|
||||
import akka.http.scaladsl.model.StatusCodes.MovedPermanently
|
||||
import akka.http.scaladsl.server.Directives._
|
||||
import akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller
|
||||
import akka.pattern.ask
|
||||
import akka.stream.ActorMaterializer
|
||||
import akka.util.Timeout
|
||||
|
||||
// types used by the API routes
|
||||
|
|
@ -427,6 +438,7 @@ class HttpServerExampleSpec extends WordSpec with Matchers
|
|||
|
||||
"stream random numbers" in compileOnlySpec {
|
||||
//#stream-random-numbers
|
||||
import akka.actor.ActorSystem
|
||||
import akka.stream.scaladsl._
|
||||
import akka.util.ByteString
|
||||
import akka.http.scaladsl.Http
|
||||
|
|
@ -483,15 +495,15 @@ class HttpServerExampleSpec extends WordSpec with Matchers
|
|||
"interact with an actor" in compileOnlySpec {
|
||||
//#actor-interaction
|
||||
import akka.actor.ActorSystem
|
||||
import akka.actor.Props
|
||||
import scala.concurrent.duration._
|
||||
import akka.util.Timeout
|
||||
import akka.pattern.ask
|
||||
import akka.stream.ActorMaterializer
|
||||
import akka.http.scaladsl.Http
|
||||
import akka.http.scaladsl.model.StatusCodes
|
||||
import akka.http.scaladsl.server.Directives._
|
||||
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
|
||||
import akka.pattern.ask
|
||||
import akka.stream.ActorMaterializer
|
||||
import akka.util.Timeout
|
||||
import spray.json.DefaultJsonProtocol._
|
||||
import scala.concurrent.duration._
|
||||
import scala.io.StdIn
|
||||
|
||||
object WebServer {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue