-
Notifications
You must be signed in to change notification settings - Fork 4
/
HttpServer.scala
39 lines (34 loc) · 1.18 KB
/
HttpServer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package reactive5
import akka.actor.typed.ActorSystem
import akka.actor.typed.scaladsl.Behaviors
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import scala.io.StdIn
import com.typesafe.config.ConfigFactory
// see: https://doc.akka.io/docs/akka-http/current/routing-dsl/index.html#minimal-example
object HttpServer extends App {
implicit val system =
ActorSystem(Behaviors.empty[Any], "reactive5", ConfigFactory.empty())
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.executionContext
val route =
path("hello") {
get {
complete(
HttpEntity(
ContentTypes.`text/html(UTF-8)`,
"<h1>Say hello to akka-http</h1>"
)
)
}
}
val bindingFuture = Http().newServerAt("localhost", 8080).bind(route)
println(
s"Server now online. Please navigate to http://localhost:8080/hello\nPress RETURN to stop..."
)
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}