/docs/serving/samples/hello-world/helloworld-scala/src/main/scala/klang/HelloWorldScala.scala
https://github.com/knative/docs · Scala · 51 lines · 36 code · 6 blank · 9 comment · 0 complexity · bb22ab7cb97e6f19925d5df98e83f4f9 MD5 · raw file
- package klang
- import akka.actor.ActorSystem
- import akka.event.LoggingAdapter
- import akka.http.scaladsl.Http
- import akka.http.scaladsl.model._
- import ContentTypes._
- import akka.http.scaladsl.server.Directives._
- import akka.stream.{ActorMaterializer, Materializer}
- import scala.concurrent.ExecutionContext
- import scala.util.{Failure, Success}
- object HelloWorldScala {
- def main(args: Array[String]): Unit = {
- // Creates and initializes an Akka Actor System
- implicit val system: ActorSystem = ActorSystem("HelloWorldScala")
- // Creates and initializes a Materializer to be used for the Akka HTTP Server
- implicit val mat: Materializer = ActorMaterializer()
- // Specifies where any Futures in this code will execute
- implicit val ec: ExecutionContext = system.dispatcher
- // Obtains a logger to be used for the sample
- val log: LoggingAdapter = system.log
- // Obtains a reference to the configuration for this application
- val config = system.settings.config
- // These are read from the application.conf file under `resources`
- val message = config.getString("helloworld.message")
- val host = config.getString("helloworld.host")
- val port = config.getInt("helloworld.port")
- // Here we define the endpoints exposed by this application
- val serviceRoute =
- path("") {
- get {
- log.info("Received request to HelloWorldScala")
- complete(HttpEntity(`text/html(UTF-8)`, message))
- }
- }
- // Here we create the Http server, and bind it to the host and the port,
- // so we can serve requests using the route(s) we defined previously.
- val binding = Http().bindAndHandle(serviceRoute, host, port) andThen {
- case Success(sb) =>
- log.info("Bound: {}", sb)
- case Failure(t) =>
- log.error(t, "Failed to bind to {}:{}—shutting down", host, port)
- system.terminate()
- }
- }
- }