/akkaStreamsModelServer/src/main/scala/com/lightbend/scala/akkastream/queryablestate/inmemory/RestServiceInMemory.scala

https://github.com/lightbend/kafka-with-akka-streams-kafka-streams-tutorial · Scala · 50 lines · 32 code · 8 blank · 10 comment · 0 complexity · 293598e56827fe768824ad8322e4fea9 MD5 · raw file

  1. package com.lightbend.scala.akkastream.queryablestate.inmemory
  2. import akka.actor.ActorSystem
  3. import akka.http.scaladsl.Http
  4. import akka.http.scaladsl.server.Directives.{complete, get, path}
  5. import akka.http.scaladsl.server.Route
  6. import akka.stream.ActorMaterializer
  7. import scala.concurrent.ExecutionContextExecutor
  8. // import akka.util.timeout // See usage below.
  9. import com.lightbend.scala.akkastream.modelserver.stage.ModelStateStore
  10. import com.lightbend.scala.modelServer.model.ModelToServeStats
  11. import de.heikoseeberger.akkahttpjackson.JacksonSupport
  12. /**
  13. * Implements Queryable State for the Akka Streams-based model scoring application.
  14. * Uses Akka HTTP to implement this capability, with state held in memory.
  15. * Note that a production implementation might need better scalability, if it's used
  16. * heavily and also we ignore security considerations here!
  17. */
  18. object RestServiceInMemory {
  19. // Serve model status: http://localhost:5500/state
  20. def startRest(service: ModelStateStore)(implicit system: ActorSystem, materializer: ActorMaterializer): Unit = {
  21. implicit val executionContext: ExecutionContextExecutor = system.dispatcher
  22. // Use with HTTP methods that accept an implicit timeout argument
  23. // implicit val timeout = Timeout(10.seconds)
  24. val host = "127.0.0.1"
  25. val port = 5500
  26. val routes = QueriesAkkaHttpResource.storeRoutes(service)
  27. Http().bindAndHandle(routes, host, port) map
  28. { binding => println(s"Starting models observer on port ${binding.localAddress}") } recover {
  29. case ex =>
  30. println(s"Models observer could not bind to $host:$port - ${ex.getMessage}")
  31. }
  32. }
  33. }
  34. object QueriesAkkaHttpResource extends JacksonSupport {
  35. def storeRoutes(service: ModelStateStore): Route =
  36. get {
  37. path("state") {
  38. val info: ModelToServeStats = service.getCurrentServingInfo
  39. complete(info)
  40. }
  41. }
  42. }