-
Notifications
You must be signed in to change notification settings - Fork 8
/
WebServer.scala
105 lines (88 loc) · 2.85 KB
/
WebServer.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package http
/**
* Created by pabloperezgarcia on 06/02/2017.
*/
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.Http.ServerBinding
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import scala.concurrent.Future
import scala.io.StdIn
import scala.util.parsing.json.JSON
object WebServer extends App {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.dispatcher
val localhost = "localhost"
val port = 8080
var order = Map[String, String]()
initializeService
/**
* Akka http provide the bindAndHandler which allow you to bind using a dsl some endpoints in a specific address/port
*
*/
private def initializeService = {
val serverFuture = Http().bindAndHandle(routes, localhost, port)
serverFuture.onComplete(_ => println(s"Server online at http://$localhost:$port/\nPress RETURN to stop..."))
StdIn.readLine() // let it run until user presses return
shutdownService(serverFuture)
}
/**
* Trigger unbinding from the port and shutdown when done
*
* @param bindingFuture
*/
private def shutdownService(bindingFuture: Future[ServerBinding]) = {
bindingFuture
.flatMap(_.unbind())
.onComplete(_ ⇒ system.terminate())
}
/**
* Akka http provide a dsl to create the endpoints for the rest service
* The ~ character pass the request form one route to the next one in the chain.
*/
private def routes = {
path("version") {
get {
complete(getVersionResponse)
}
} ~
path("order") {
get {
entity(as[String]) { id =>
complete(getOrderResponse(id))
}
} ~
post {
entity(as[String]) { order =>
val jsonOrder = JSON.parseFull(order)
onComplete(saveOrder(getOrderBody(jsonOrder))) { _ =>
complete(getPostResponse)
}
}
}
}
}
private def getOrderBody(jsonOrder: Option[Any]) = {
jsonOrder.get.asInstanceOf[Map[String, String]]
}
def saveOrder(order: Map[String, String]): Future[Map[String, String]] = {
this.order = this.order ++ getBodyMap(order)
Future.apply(order)
}
private def getBodyMap(order: Map[String, String]) = {
Map[String, String](order("id") -> order("product"))
}
private def getVersionResponse = {
HttpEntity(ContentTypes.`text/html(UTF-8)`, "Akka-http version 1.0")
}
private def getOrderResponse(id: String) = {
HttpEntity(ContentTypes.`text/html(UTF-8)`, s"Get order ${order(id)}")
}
private def getPostResponse = {
HttpEntity(ContentTypes.`text/html(UTF-8)`, s"Order stored")
}
}