|
1 |
| -import akka.actor.typed.ActorSystem |
2 |
| -import akka.actor.typed.scaladsl.Behaviors |
3 |
| -import akka.event.Logging |
4 |
| -import akka.http.scaladsl.Http |
5 |
| -import akka.http.scaladsl.model._ |
6 |
| -import akka.http.scaladsl.server.Directives._ |
7 |
| -import scala.concurrent.{Await, Future} |
8 |
| -import scala.concurrent.duration.Duration |
9 |
| - |
10 |
| -object Main { |
11 |
| - implicit val system = ActorSystem(Behaviors.empty, "my-sample-app") |
12 |
| - |
13 |
| - def main(args: Array[String]): Unit = { |
14 |
| - Await.ready(start(), Duration.Inf) |
15 |
| - } |
16 |
| - |
17 |
| - def start(): Future[Http.ServerBinding] = { |
18 |
| - |
19 |
| - // GET /indexでリクエストのURLパラメータとUserAgentを返却する |
20 |
| - val route = |
21 |
| - (get & pathPrefix("index") & extractUri & headerValueByName( |
22 |
| - "User-Agent" |
23 |
| - )) { (uri, ua) => |
24 |
| - logRequestResult("/index", Logging.InfoLevel) { |
25 |
| - complete(s"param: ${uri.query().toMap}, user-agent: ${ua}}") |
26 |
| - } |
27 |
| - } |
28 |
| - |
29 |
| - val host = sys.props.get("http.host") getOrElse "0.0.0.0" |
30 |
| - val port = sys.props.get("http.port").fold(8080) { _.toInt } |
31 |
| - |
32 |
| - println(s"server at [$host:$port]") |
33 |
| - |
34 |
| - Http().newServerAt(host, port).bind(route) |
35 |
| - } |
36 |
| - |
37 |
| -} |
| 1 | +import akka.actor.typed.ActorSystem |
| 2 | +import akka.actor.typed.scaladsl.Behaviors |
| 3 | +import akka.event.Logging |
| 4 | +import akka.http.scaladsl.Http |
| 5 | +import akka.http.scaladsl.model._ |
| 6 | +import akka.http.scaladsl.server.Directives._ |
| 7 | +import scala.concurrent.{Await, Future} |
| 8 | +import scala.concurrent.duration.Duration |
| 9 | + |
| 10 | +object Main { |
| 11 | + implicit val system = ActorSystem(Behaviors.empty, "my-sample-app") |
| 12 | + |
| 13 | + def main(args: Array[String]): Unit = { |
| 14 | + Await.ready(start(), Duration.Inf) |
| 15 | + } |
| 16 | + |
| 17 | + def start(): Future[Http.ServerBinding] = { |
| 18 | + |
| 19 | + // GET /indexでリクエストのURLパラメータとUserAgentを返却する |
| 20 | + val route = |
| 21 | + (get & pathPrefix("index") & extractUri & headerValueByName( |
| 22 | + "User-Agent" |
| 23 | + )) { (uri, ua) => |
| 24 | + logRequestResult("/index", Logging.InfoLevel) { |
| 25 | + complete(s"param: ${uri.query().toMap}, user-agent: ${ua}}") |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + val host = sys.props.get("http.host") getOrElse "0.0.0.0" |
| 30 | + val port = sys.props.get("http.port").fold(8080) { _.toInt } |
| 31 | + |
| 32 | + println(s"server at [$host:$port]") |
| 33 | + |
| 34 | + Http().newServerAt(host, port).bind(route) |
| 35 | + } |
| 36 | + |
| 37 | +} |
0 commit comments