forked from playframework/playframework
/
Server.scala
356 lines (325 loc) · 13.5 KB
/
Server.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (C) Lightbend Inc. <https://www.lightbend.com>
*/
package play.core.server
import java.util.function.{ Function => JFunction }
import akka.actor.CoordinatedShutdown
import akka.annotation.ApiMayChange
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import play.api.ApplicationLoader.Context
import play.api._
import play.api.http.DevHttpErrorHandler
import play.api.http.HttpErrorHandler
import play.api.http.Port
import play.api.inject.ApplicationLifecycle
import play.api.inject.DefaultApplicationLifecycle
import play.api.libs.streams.Accumulator
import play.api.mvc._
import play.api.routing.Router
import play.core._
import play.routing.{ Router => JRouter }
import play.{ ApplicationLoader => JApplicationLoader }
import play.{ BuiltInComponents => JBuiltInComponents }
import play.{ BuiltInComponentsFromContext => JBuiltInComponentsFromContext }
import scala.concurrent.Future
import scala.language.postfixOps
import scala.util.Try
trait WebSocketable {
def getHeader(header: String): String
def check: Boolean
}
/**
* Provides generic server behaviour for Play applications.
*/
trait Server extends ReloadableServer {
def mode: Mode
def applicationProvider: ApplicationProvider
def reload(): Unit = applicationProvider.get
def stop(): Unit = {
applicationProvider.get.foreach { app =>
LoggerConfigurator(app.classloader).foreach(_.shutdown())
}
}
/**
* Get the address of the server.
*
* @return The address of the server.
*/
def mainAddress: java.net.InetSocketAddress
/**
* Returns the HTTP port of the server.
*
* This is useful when the port number has been automatically selected (by setting a port number of 0).
*
* @return The HTTP port the server is bound to, if the HTTP connector is enabled.
*/
def httpPort: Option[Int] = serverEndpoints.httpEndpoint.map(_.port)
/**
* Returns the HTTPS port of the server.
*
* This is useful when the port number has been automatically selected (by setting a port number of 0).
*
* @return The HTTPS port the server is bound to, if the HTTPS connector is enabled.
*/
def httpsPort: Option[Int] = serverEndpoints.httpsEndpoint.map(_.port)
/**
* Endpoints information for this server.
*/
@ApiMayChange
def serverEndpoints: ServerEndpoints
}
/**
* Utilities for creating a server that runs around a block of code.
*/
object Server {
/**
* Try to get the handler for a request and return it as a `Right`. If we
* can't get the handler for some reason then return a result immediately
* as a `Left`. Reasons to return a `Left` value:
*
* - If there's a "web command" installed that intercepts the request.
* - If we fail to get the `Application` from the `applicationProvider`,
* i.e. if there's an error loading the application.
* - If an exception is thrown.
*/
private[server] def getHandlerFor(
request: RequestHeader,
tryApp: Try[Application],
fallbackErrorHandler: HttpErrorHandler
): (RequestHeader, Handler) = {
@inline def handleErrors(
errorHandler: HttpErrorHandler,
req: RequestHeader
): PartialFunction[Throwable, (RequestHeader, Handler)] = {
case e: ThreadDeath => throw e
case e: VirtualMachineError => throw e
case e: Throwable =>
val errorResult = errorHandler.onServerError(req, e)
val errorAction = actionForResult(errorResult)
(req, errorAction)
}
try {
// Get the Application from the try.
val application = tryApp.get
// We managed to get an Application, now make a fresh request using the Application's RequestFactory.
// The request created by the request factory needs to be at this scope so that it can be
// used by application error handler. The reason for that is that this request is populated
// with all attributes necessary to translate it to Java.
// TODO: `copyRequestHeader` is a misleading name here since it is also populating the request with attributes
// such as id, session, flash, etc.
val enrichedRequest: RequestHeader = application.requestFactory.copyRequestHeader(request)
try {
// We hen use the Application's logic to handle that request.
val (handlerHeader, handler) = application.requestHandler.handlerForRequest(enrichedRequest)
(handlerHeader, handler)
} catch {
handleErrors(application.errorHandler, enrichedRequest)
}
} catch {
handleErrors(fallbackErrorHandler, request)
}
}
/**
* Create a simple [[Handler]] which sends a [[Result]].
*/
private[server] def actionForResult(errorResult: Future[Result]): Handler = {
EssentialAction(_ => Accumulator.done(errorResult))
}
/**
* Parses the config setting `infinite` as `Long.MaxValue` otherwise uses Config's built-in
* parsing of byte values.
*/
private[server] def getPossiblyInfiniteBytes(
config: Config,
path: String,
deprecatedPath: String = """"""""
): Long = {
Configuration(config).getDeprecated[String](path, deprecatedPath) match {
case "infinite" => Long.MaxValue
case _ => config.getBytes(if (config.hasPath(deprecatedPath)) deprecatedPath else path)
}
}
/**
* Run a block of code with a server for the given application.
*
* The passed in block takes the port that the application is running on. By default, this will be a random ephemeral
* port. This can be changed by passing in an explicit port with the config parameter.
*
* @param application The application for the server to server.
* @param config The configuration for the server. Defaults to test config with the http port bound to a random
* ephemeral port.
* @param block The block of code to run.
* @param provider The server provider.
* @return The result of the block of code.
*/
def withApplication[T](
application: Application,
config: ServerConfig = ServerConfig(port = Some(0), mode = Mode.Test)
)(block: Port => T)(implicit provider: ServerProvider): T = {
Play.start(application)
val server = provider.createServer(config, application)
try {
block(new Port(server.httpPort.orElse(server.httpsPort).get))
} finally {
server.stop()
}
}
/**
* Run a block of code with a server for the given routes.
*
* The passed in block takes the port that the application is running on. By default, this will be a random ephemeral
* port. This can be changed by passing in an explicit port with the config parameter.
*
* @param routes The routes for the server to server.
* @param config The configuration for the server. Defaults to test config with the http port bound to a random
* ephemeral port.
* @param block The block of code to run.
* @param provider The server provider.
* @return The result of the block of code.
*/
def withRouter[T](
config: ServerConfig = ServerConfig(port = Some(0), mode = Mode.Test)
)(routes: PartialFunction[RequestHeader, Handler])(block: Port => T)(implicit provider: ServerProvider): T = {
val context = ApplicationLoader.Context(
environment = Environment.simple(path = config.rootDir, mode = config.mode),
initialConfiguration = Configuration(ConfigFactory.load()),
lifecycle = new DefaultApplicationLifecycle,
devContext = None
)
val application = new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents {
def router = Router.from(routes)
}.application
withApplication(application, config)(block)
}
/**
* Run a block of code with a server for the given routes, obtained from the application components
*
* The passed in block takes the port that the application is running on. By default, this will be a random ephemeral
* port. This can be changed by passing in an explicit port with the config parameter.
*
* @param routes A function that obtains the routes from the server from the application components.
* @param config The configuration for the server. Defaults to test config with the http port bound to a random
* ephemeral port.
* @param block The block of code to run.
* @param provider The server provider.
* @return The result of the block of code.
*/
def withRouterFromComponents[T](config: ServerConfig = ServerConfig(port = Some(0), mode = Mode.Test))(
routes: BuiltInComponents => PartialFunction[RequestHeader, Handler]
)(block: Port => T)(implicit provider: ServerProvider): T = {
val context: Context = ApplicationLoader.Context(
environment = Environment.simple(path = config.rootDir, mode = config.mode),
initialConfiguration = Configuration(ConfigFactory.load()),
lifecycle = new DefaultApplicationLifecycle,
devContext = None
)
val application =
(new BuiltInComponentsFromContext(context) with NoHttpFiltersComponents { self: BuiltInComponents =>
def router = Router.from(routes(self))
}).application
withApplication(application, config)(block)
}
/**
* Run a block of code with a server for the application containing routes.
*
* The passed in block takes the port that the application is running on. By default, this will be a random ephemeral
* port. This can be changed by passing in an explicit port with the config parameter.
*
* An easy way to set up an application with given routes is to use [[play.api.BuiltInComponentsFromContext]] with
* any extra components needed:
*
* {{{
* Server.withApplicationFromContext(ServerConfig(mode = Mode.Prod, port = Some(0))) { context =>
* new BuiltInComponentsFromContext(context) with AssetsComponents with play.filters.HttpFiltersComponents {
* override def router: Router = Router.from {
* case req => assets.versioned("/testassets", req.path)
* }
* }.application
* } { withClient(block)(_) }
* }}}
*
* @param appProducer A function that takes an ApplicationLoader.Context and produces [[play.api.Application]]
* @param config The configuration for the server. Defaults to test config with the http port bound to a random
* ephemeral port.
* @param block The block of code to run.
* @param provider The server provider.
* @return The result of the block of code.
*/
def withApplicationFromContext[T](
config: ServerConfig = ServerConfig(port = Some(0), mode = Mode.Test)
)(appProducer: ApplicationLoader.Context => Application)(block: Port => T)(implicit provider: ServerProvider): T = {
val context: Context = ApplicationLoader.Context(
environment = Environment.simple(path = config.rootDir, mode = config.mode),
initialConfiguration = Configuration(ConfigFactory.load()),
lifecycle = new DefaultApplicationLifecycle,
devContext = None
)
withApplication(appProducer(context), config)(block)
}
case object ServerStoppedReason extends CoordinatedShutdown.Reason
}
/**
* Components to create a Server instance.
*/
trait ServerComponents {
def server: Server
lazy val serverConfig: ServerConfig = ServerConfig()
lazy val environment: Environment = Environment.simple(mode = serverConfig.mode)
lazy val configuration: Configuration = Configuration(ConfigFactory.load())
lazy val applicationLifecycle: ApplicationLifecycle = new DefaultApplicationLifecycle
def serverStopHook: () => Future[Unit] = () => Future.successful(())
}
/**
* Define how to create a Server from a Router.
*/
private[server] trait ServerFromRouter {
protected def createServerFromRouter(serverConfig: ServerConfig = ServerConfig())(
routes: ServerComponents with BuiltInComponents => Router
): Server
/**
* Creates a [[Server]] from the given router.
*
* @param config the server configuration
* @param routes the routes definitions
* @return an AkkaHttpServer instance
*/
@deprecated(
"Use fromRouterWithComponents or use DefaultAkkaHttpServerComponents/DefaultNettyServerComponents",
"2.7.0"
)
def fromRouter(config: ServerConfig = ServerConfig())(routes: PartialFunction[RequestHeader, Handler]): Server = {
createServerFromRouter(config) { _ =>
Router.from(routes)
}
}
/**
* Creates a [[Server]] from the given router, using [[ServerComponents]].
*
* @param config the server configuration
* @param routes the routes definitions
* @return an AkkaHttpServer instance
*/
def fromRouterWithComponents(
config: ServerConfig = ServerConfig()
)(routes: BuiltInComponents => PartialFunction[RequestHeader, Handler]): Server = {
createServerFromRouter(config)(components => Router.from(routes(components)))
}
}
private[play] object JavaServerHelper {
def forRouter(router: JRouter, mode: Mode, httpPort: Option[Integer], sslPort: Option[Integer]): Server = {
forRouter(mode, httpPort, sslPort)(_ => router)
}
def forRouter(mode: Mode, httpPort: Option[Integer], sslPort: Option[Integer])(
block: JFunction[JBuiltInComponents, JRouter]
): Server = {
val context = JApplicationLoader.create(Environment.simple(mode = mode).asJava)
val application = new JBuiltInComponentsFromContext(context) {
override def router: JRouter = block.apply(this)
override def httpFilters(): java.util.List[play.mvc.EssentialFilter] = java.util.Collections.emptyList()
}.application.asScala()
Play.start(application)
val serverConfig = ServerConfig(mode = mode, port = httpPort.map(_.intValue), sslPort = sslPort.map(_.intValue))
implicitly[ServerProvider].createServer(serverConfig, application)
}
}