forked from playframework/playframework
/
ScalaAkkaEmbeddingPlay.scala
131 lines (111 loc) · 3.38 KB
/
ScalaAkkaEmbeddingPlay.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (C) 2009-2016 Lightbend Inc. <https://www.lightbend.com>
*/
package scalaguide.advanced.embedding
import org.specs2.mutable.Specification
import play.api.test.WsTestClient
import scala.concurrent.Await
import scala.concurrent.duration.Duration
class ScalaAkkaEmbeddingPlay extends Specification with WsTestClient {
"Embedding play with akka" should {
"be very simple" in {
//#simple-akka-http
import play.core.server.AkkaHttpServer
import play.api.routing.sird._
import play.api.mvc._
val server = AkkaHttpServer.fromRouter() {
case GET(p"/hello/$to") => Action {
Results.Ok(s"Hello $to")
}
}
try {
testRequest(9000)
} finally {
//#stop-akka-http
server.stop()
//#stop-akka-http
}
}
//#simple-akka-http
"be configurable with akka" in {
//#config-akka-http
import play.core.server._
import play.core.server.AkkaHttpServer
import play.api.routing.sird._
import play.api.mvc._
val server = AkkaHttpServer.fromRouter(ServerConfig(
port = Some(19000),
address = "127.0.0.1"
)) {
case GET(p"/hello/$to") => Action {
Results.Ok(s"Hello $to")
}
}
//#config-akka-http
try {
testRequest(19000)
} finally {
server.stop()
}
}
"allow overriding components" in {
//#components-akka-http
import play.core.server.AkkaServerComponents
import play.api.routing.Router
import play.api.routing.sird._
import play.api.mvc._
import play.api.BuiltInComponents
import play.api.http.DefaultHttpErrorHandler
import scala.concurrent.Future
val components = new AkkaServerComponents with BuiltInComponents {
lazy val router = Router.from {
case GET(p"/hello/$to") => Action {
Results.Ok(s"Hello $to")
}
}
override lazy val httpErrorHandler = new DefaultHttpErrorHandler(environment,
configuration, sourceMapper, Some(router)) {
override protected def onNotFound(request: RequestHeader, message: String) = {
Future.successful(Results.NotFound("Nothing was found!"))
}
}
}
val server = components.server
//#components-akka-http
try {
testRequest(9000)
} finally {
server.stop()
}
}
"allow usage from a running application" in {
//#application-akka-http
import play.api.inject.guice.GuiceApplicationBuilder
import play.core.server.AkkaHttpServer
import play.core.server.ServerConfig
import play.api.routing.sird._
import play.api.routing.SimpleRouterImpl
import play.api.mvc._
val server = AkkaHttpServer.fromApplication(GuiceApplicationBuilder().router(new SimpleRouterImpl({
case GET(p"/hello/$to") => Action {
Results.Ok(s"Hello $to")
}
})).build(), ServerConfig(
port = Some(19000),
address = "127.0.0.1"
))
//#config-akka-http
try {
testRequest(19000)
} finally {
server.stop()
}
//#application-akka-http
}
}
def testRequest(port: Int) = {
withClient { client =>
Await.result(client.url("/hello/world").get(), Duration.Inf).body must_== "Hello world"
}(new play.api.http.Port(port))
}
}