/docs/src/test/scala/docs/http/scaladsl/Http2Spec.scala

https://github.com/akka/akka-http · Scala · 53 lines · 24 code · 14 blank · 15 comment · 0 complexity · 5dc7a3ee232dc7b51e42efc6a5390552 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
  3. */
  4. package docs.http.scaladsl
  5. import akka.http.impl.util.ExampleHttpContexts
  6. import akka.http.scaladsl.model.{ HttpRequest, HttpResponse, StatusCodes }
  7. //#bindAndHandleSecure
  8. import scala.concurrent.Future
  9. import akka.http.scaladsl.HttpsConnectionContext
  10. //#bindAndHandleSecure
  11. //#bindAndHandleSecure
  12. //#bindAndHandlePlain
  13. import akka.http.scaladsl.Http
  14. //#bindAndHandlePlain
  15. //#bindAndHandleSecure
  16. //#bindAndHandlePlain
  17. import akka.http.scaladsl.HttpConnectionContext
  18. //#bindAndHandlePlain
  19. import akka.actor.ActorSystem
  20. import akka.stream.Materializer
  21. object Http2Spec {
  22. implicit val system: ActorSystem = ActorSystem()
  23. {
  24. val asyncHandler: HttpRequest => Future[HttpResponse] = _ => Future.successful(HttpResponse(status = StatusCodes.ImATeapot))
  25. val httpsServerContext: HttpsConnectionContext = ExampleHttpContexts.exampleServerContext
  26. //#bindAndHandleSecure
  27. Http().newServerAt(interface = "localhost", port = 8443).enableHttps(httpsServerContext).bind(asyncHandler)
  28. //#bindAndHandleSecure
  29. }
  30. {
  31. import akka.http.scaladsl.server.Route
  32. import akka.http.scaladsl.server.directives.RouteDirectives.complete
  33. val handler: HttpRequest => Future[HttpResponse] =
  34. Route.toFunction(complete(StatusCodes.ImATeapot))
  35. //#bindAndHandlePlain
  36. Http().newServerAt("localhost", 8080).bind(handler)
  37. //#bindAndHandlePlain
  38. }
  39. }