/chapter09/sample/src/main/scala/code/Main.scala

https://github.com/PacktPublishing/Mastering-Akka · Scala · 84 lines · 67 code · 14 blank · 3 comment · 0 complexity · bb50ff7e288b2ecfe6fe89c43fc0b0b1 MD5 · raw file

  1. package code
  2. import akka.actor.ActorSystem
  3. import akka.event.{ Logging, LoggingAdapter }
  4. import akka.http.scaladsl._
  5. import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
  6. import akka.http.scaladsl.server.{ Directives, Route }
  7. import akka.stream.{ ActorMaterializer, Materializer }
  8. import com.typesafe.conductr.bundlelib.akka.{ Env, LocationService, StatusService }
  9. import com.typesafe.conductr.bundlelib.scala.{ LocationCache, URI }
  10. import com.typesafe.conductr.lib.akka.ConnectionContext
  11. import com.typesafe.config.ConfigFactory
  12. import spray.json.DefaultJsonProtocol
  13. import scala.concurrent.{ ExecutionContext, Future }
  14. object Main extends App with SprayJsonSupport with DefaultJsonProtocol with Directives {
  15. // getting bundle configuration from Conductr
  16. val config = Env.asConfig
  17. val systemName = sys.env.getOrElse("BUNDLE_SYSTEM", "StandaloneSystem")
  18. val systemVersion = sys.env.getOrElse("BUNDLE_SYSTEM_VERSION", "1")
  19. // configuring the ActorSystem
  20. implicit val system = ActorSystem(s"$systemName-$systemVersion", config.withFallback(ConfigFactory.load()))
  21. // setting up some machinery
  22. implicit val mat: Materializer = ActorMaterializer()
  23. implicit val ec: ExecutionContext = system.dispatcher
  24. implicit val log: LoggingAdapter = Logging(system, this.getClass)
  25. implicit val cc = ConnectionContext()
  26. implicit val locationCache = LocationCache()
  27. val httpServerCfg = system.settings.config.getConfig("helloworld")
  28. val configuredIpAddress = httpServerCfg.getString("ip")
  29. val configuredPort = httpServerCfg.getInt("port")
  30. log.debug(" ==> Launching HelloWorld sample application on ip: '{}', port: '{}'", configuredIpAddress, configuredPort)
  31. final case class HelloWorldResponse(msg: String)
  32. final case class Person(name: String, age: Int)
  33. implicit val helloWorldJsonFormat = jsonFormat1(HelloWorldResponse)
  34. implicit val personJsonFormat = jsonFormat2(Person)
  35. def completeWithHello = extractMethod(method => complete(HelloWorldResponse(s"${method.value} Hello World!")))
  36. def queryServiceLocator(serviceName: String = "web") =
  37. LocationService.lookup(serviceName, URI("http://localhost:8080/"), locationCache)
  38. def route: Route =
  39. logRequestResult("chapter9-sample-helloworld") {
  40. path("helloworld") {
  41. (get & pathEnd)(completeWithHello) ~
  42. (put & pathEnd)(completeWithHello) ~
  43. (patch & pathEnd)(completeWithHello) ~
  44. (delete & pathEnd)(completeWithHello) ~
  45. (options & pathEnd)(completeWithHello)
  46. } ~
  47. path("person") {
  48. (post & pathEnd & entity(as[Person])) { person =>
  49. complete(person)
  50. } ~
  51. (get & pathEnd) {
  52. complete(Person("John Doe", 40))
  53. }
  54. } ~
  55. (get & path("service" / Segment)) { serviceName =>
  56. complete(queryServiceLocator(serviceName).map {
  57. case Some(uri) => s"Service '$serviceName' found, its available at: $uri"
  58. case _ => s"No service found for service name: '$serviceName'"
  59. })
  60. }
  61. }
  62. (for {
  63. _ <- Http().bindAndHandle(route, interface = configuredIpAddress, port = configuredPort)
  64. _ <- StatusService.signalStartedOrExit()
  65. } yield ()).recover {
  66. case cause: Throwable =>
  67. log.error(cause, "Failure while launching HelloWorld")
  68. StatusService.signalStartedOrExit()
  69. }
  70. }