Skip to content

Commit 6d0cf52

Browse files
committed
シンプルなWebアプリを実装
0 parents  commit 6d0cf52

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
target/
2+
.vscode
3+
.metals
4+
.bloop
5+
.bsp
6+
metals.sbt

.scalafmt.conf

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version = "2.7.4"

build.sbt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
val AkkaVersion = "2.6.8"
2+
val AkkaHttpVersion = "10.2.4"
3+
libraryDependencies ++= Seq(
4+
"com.typesafe.akka" %% "akka-actor-typed" % AkkaVersion,
5+
"com.typesafe.akka" %% "akka-stream" % AkkaVersion,
6+
"com.typesafe.akka" %% "akka-http" % AkkaHttpVersion,
7+
"ch.qos.logback" % "logback-classic" % "1.2.3"
8+
)

project/build.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.4.7

src/main/scala/sample.scala

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

0 commit comments

Comments
 (0)