Last active
February 22, 2022 10:18
-
-
Save robinraju/b79817bf961092c16206a889043f88c2 to your computer and use it in GitHub Desktop.
Starting an Akka HTTP server (akka-http version 10.2.8)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.concurrent.ExecutionContext | |
import scala.concurrent.duration.DurationInt | |
import scala.util.{ Failure, Success } | |
import akka.Done | |
import akka.actor.{ ActorSystem, CoordinatedShutdown } | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.server.Route | |
object AkkaHttpServer { | |
def start(name: String, routes: Route, port: Int)(implicit system: ActorSystem): Unit = { | |
implicit val ec: ExecutionContext = system.dispatcher | |
val shutdown = CoordinatedShutdown(system) | |
Http().newServerAt("0.0.0.0", port).bind(routes).onComplete { | |
case Success(binding) => | |
val address = binding.localAddress | |
val uri = s"http://${address.getHostString}:${address.getPort}" | |
system.log.info("{} online at {}", name, uri) | |
shutdown.addTask(CoordinatedShutdown.PhaseServiceRequestsDone, "http-graceful-terminate") { () => | |
binding.terminate(10.seconds).map { _ => | |
system.log.info("{} at {} graceful shutdown completed", name, uri) | |
Done | |
} | |
} | |
case Failure(ex) => | |
system.log.error("Failed to bind HTTP endpoint, terminating system", ex) | |
system.terminate() | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.http.scaladsl.model.{ ContentTypes, HttpEntity } | |
import akka.http.scaladsl.server.Directives._ | |
import akka.http.scaladsl.server.Route | |
object HttpRoutes { | |
lazy val all: Route = concat(helloRoute, greetRoute) | |
private lazy val helloRoute = | |
path("hello") { | |
get { | |
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>")) | |
} | |
} | |
private lazy val greetRoute = | |
path("hello" / Segment) { name => | |
get { | |
complete(s"Hello, $name") | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.ActorSystem | |
object Main { | |
def main(args: Array[String]): Unit = { | |
implicit val system: ActorSystem = ActorSystem("main-system") | |
// Start http server on port 8080 | |
AkkaHttpServer.start("api-server", HttpRoutes.all, 8080) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment