/akka-server/src/main/scala/de/nierbeck/floating/data/server/ServiceApp.scala

https://github.com/ANierbeck/BusFloatingData · Scala · 77 lines · 47 code · 15 blank · 15 comment · 0 complexity · 46bcaa3a02d14c86f8fdb59a349cc057 MD5 · raw file

  1. /*
  2. * Copyright 2016 Achim Nierbeck
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package de.nierbeck.floating.data.server
  17. import akka.actor.{ActorRef, ActorSystem, Props}
  18. import akka.event.Logging
  19. import akka.http.scaladsl.Http
  20. import akka.http.scaladsl.model.HttpMethods._
  21. import akka.http.scaladsl.model.ws.UpgradeToWebSocket
  22. import akka.http.scaladsl.model.{HttpRequest, HttpResponse, Uri}
  23. import akka.stream.ActorMaterializer
  24. import de.nierbeck.floating.data.server.actors.websocket.{FLINK, RouterActor, SPARK, TiledVehiclesFromKafkaActor}
  25. import scala.concurrent.Await
  26. import scala.concurrent.duration.Duration
  27. import scala.util.{Failure, Success}
  28. object ServiceApp extends RestService {
  29. import ServiceConfig._
  30. import system.dispatcher
  31. implicit val system = ActorSystem("service-api-http")
  32. implicit val mat = ActorMaterializer()
  33. override val logger = Logging(system, getClass.getName)
  34. override val session = CassandraConnector.connect()
  35. def main(args: Array[String]): Unit = {
  36. val router: ActorRef = system.actorOf(Props[RouterActor], "router")
  37. val sparkKafkaConsumer: ActorRef = system.actorOf(TiledVehiclesFromKafkaActor.props(router, "tiledVehicles", SPARK), "Kafka-Consumer-Spark")
  38. val flinkKafkaConsumer: ActorRef = system.actorOf(TiledVehiclesFromKafkaActor.props(router, "flinkTiledVehicles", FLINK), "Kafka-Consumer-Flink")
  39. val requestHandler: HttpRequest => HttpResponse = {
  40. case req@HttpRequest(GET, Uri.Path("/ws/vehicles"), _, _, _) =>
  41. req.header[UpgradeToWebSocket] match {
  42. case Some(upgrade) => upgrade.handleMessages(Flows.graphFlowWithStats(router))
  43. case None => HttpResponse(400, entity = "Not a valid websocket request!")
  44. }
  45. case _: HttpRequest => HttpResponse(404, entity = "Unknown resource!")
  46. }
  47. Http()
  48. .bindAndHandle(route(), serviceInterface, servicePort)
  49. .onComplete {
  50. case Success(_) => logger.info(s"Successfully bound to $serviceInterface:$servicePort")
  51. case Failure(e) => logger.error(s"Failed !!!! ${e.getMessage}")
  52. }
  53. Http()
  54. .bindAndHandleSync(requestHandler, serviceInterface, 8001)
  55. .onComplete {
  56. case Success(_) => logger.info(s"Successfully started Server to $serviceInterface:8001")
  57. case Failure(e) => logger.error(s"Failed !!!! ${e.getMessage}")
  58. }
  59. Await.ready(system.whenTerminated, Duration.Inf)
  60. CassandraConnector.close(session)
  61. }
  62. }