/webapp/jvm/src/main/scala/com.scalakata/Server.scala

https://github.com/MasseGuillaume/ScalaKata2 · Scala · 44 lines · 34 code · 9 blank · 1 comment · 0 complexity · 6ad4361ce469e37eef2cf1a107339b1e MD5 · raw file

  1. package com.scalakata
  2. import akka.actor.ActorSystem
  3. import akka.http.scaladsl.Http
  4. import akka.stream.ActorMaterializer
  5. import com.typesafe.config.{ConfigFactory, Config}
  6. import scala.concurrent.duration._
  7. import scala.concurrent.Await
  8. import java.nio.file.Path
  9. object Server {
  10. def start(timeout: FiniteDuration, security: Boolean, artifacts: Seq[Path],
  11. scalacOptions: Seq[String], host: String, port: Int, readyPort: Option[Int], prod: Boolean): Unit = {
  12. println((timeout, security, artifacts, scalacOptions, host, port, readyPort, prod))
  13. val config: Config = ConfigFactory.parseString(s"""
  14. akka.http.server {
  15. idle-timeout = ${timeout.toSeconds + 5}s
  16. }
  17. """)
  18. implicit val system = ActorSystem("scalakata-playground", config)
  19. import system.dispatcher
  20. implicit val materializer = ActorMaterializer()
  21. val api = new ApiImpl(artifacts, scalacOptions, security, timeout)
  22. val route = (new Route(api, prod)).route
  23. val setup =
  24. Http().bindAndHandle(route, host, port).map{ _
  25. // notify sbt plugin to open browser
  26. readyPort.map{ p
  27. val ready = new java.net.Socket(host, p)
  28. ready.sendUrgentData(0)
  29. ready.close()
  30. }
  31. ()
  32. }
  33. Await.result(setup, 20.seconds)
  34. }
  35. }