0
# Server Management
1
2
Core server instantiation, configuration, and lifecycle management capabilities for the Play Netty Server.
3
4
## Capabilities
5
6
### NettyServer Factory Methods
7
8
Create NettyServer instances from Play applications or route definitions.
9
10
```scala { .api }
11
/**
12
* Create a Netty server from a Play Application
13
* @param application The Play application to serve
14
* @param config Server configuration (defaults to ServerConfig())
15
* @return Started NettyServer instance
16
*/
17
def fromApplication(
18
application: Application,
19
config: ServerConfig = ServerConfig()
20
): NettyServer
21
22
/**
23
* Create a Netty server from route definitions
24
* @param config Server configuration
25
* @param routes Partial function mapping requests to handlers
26
* @return Started NettyServer instance
27
*/
28
def fromRouter(config: ServerConfig = ServerConfig())(
29
routes: PartialFunction[RequestHeader, Handler]
30
): NettyServer
31
```
32
33
**Usage Examples:**
34
35
```scala
36
import play.api.Application
37
import play.core.server.{NettyServer, ServerConfig}
38
import play.api.mvc._
39
import play.api.routing.Router
40
41
// Create server from existing Play application
42
val app: Application = // ... your Play application
43
val server = NettyServer.fromApplication(app, ServerConfig(port = Some(9000)))
44
45
// Create server with custom routes
46
val server = NettyServer.fromRouter(ServerConfig(port = Some(8080))) {
47
case GET -> Root / "api" / "status" =>
48
Action { Ok("Server is running") }
49
case POST -> Root / "api" / "data" =>
50
Action(parse.json) { request =>
51
Ok(s"Received: ${request.body}")
52
}
53
}
54
55
// Server starts automatically when created
56
println(s"Server running on port ${server.httpPort.getOrElse("unknown")}")
57
```
58
59
### NettyServer Class
60
61
Main server implementation providing lifecycle management and connection information.
62
63
```scala { .api }
64
/**
65
* Netty-based HTTP server implementation
66
* @param config Server configuration including ports and SSL settings
67
* @param applicationProvider Provides access to the Play application
68
* @param stopHook Callback executed when server stops
69
*/
70
class NettyServer(
71
config: ServerConfig,
72
applicationProvider: ApplicationProvider,
73
stopHook: () => Future[Unit]
74
) extends Server {
75
76
/** Current server mode (Dev, Test, Prod) */
77
def mode: Mode
78
79
/** Stop the server and release all resources */
80
def stop(): Unit
81
82
/** HTTP port if configured */
83
def httpPort: Option[Int]
84
85
/** HTTPS port if configured */
86
def httpsPort: Option[Int]
87
88
/** Primary server address */
89
def mainAddress: InetSocketAddress
90
}
91
```
92
93
**Usage Examples:**
94
95
```scala
96
import play.core.server.{NettyServer, ServerConfig}
97
import play.api.Mode
98
99
// Check server configuration
100
println(s"Server mode: ${server.mode}")
101
println(s"HTTP port: ${server.httpPort}")
102
println(s"HTTPS port: ${server.httpsPort}")
103
println(s"Main address: ${server.mainAddress}")
104
105
// Graceful shutdown
106
server.stop()
107
```
108
109
### Server Configuration
110
111
Configure server ports, SSL settings, and other network parameters.
112
113
```scala { .api }
114
/**
115
* Server configuration for NettyServer
116
* @param port Optional HTTP port (None disables HTTP)
117
* @param sslPort Optional HTTPS port (None disables HTTPS)
118
* @param address Bind address (default: "0.0.0.0")
119
* @param mode Server mode (Dev/Test/Prod)
120
* @param configuration Play configuration for additional settings
121
*/
122
case class ServerConfig(
123
port: Option[Int] = None,
124
sslPort: Option[Int] = None,
125
address: String = "0.0.0.0",
126
mode: Mode = Mode.Prod,
127
configuration: Configuration = Configuration.empty
128
)
129
```
130
131
**Usage Examples:**
132
133
```scala
134
import play.core.server.ServerConfig
135
import play.api.{Mode, Configuration}
136
137
// Basic HTTP-only server
138
val httpConfig = ServerConfig(port = Some(8080))
139
140
// HTTPS-only server
141
val httpsConfig = ServerConfig(sslPort = Some(8443))
142
143
// Both HTTP and HTTPS
144
val dualConfig = ServerConfig(
145
port = Some(8080),
146
sslPort = Some(8443),
147
address = "127.0.0.1"
148
)
149
150
// Development mode with custom configuration
151
val devConfig = ServerConfig(
152
port = Some(9000),
153
mode = Mode.Dev,
154
configuration = Configuration(
155
"play.server.netty.maxInitialLineLength" -> 8192,
156
"play.server.netty.maxHeaderSize" -> 8192
157
)
158
)
159
```
160
161
### Application Provider
162
163
Provides lazy access to Play applications with error handling.
164
165
```scala { .api }
166
/**
167
* Provides access to Play applications
168
*/
169
trait ApplicationProvider {
170
/** Get the application or error */
171
def get: Either[Throwable, Application]
172
173
/** Get current application if available */
174
def current: Option[Application]
175
}
176
177
/**
178
* Create application provider from instance
179
*/
180
object ApplicationProvider {
181
def apply(app: Application): ApplicationProvider
182
}
183
```
184
185
**Usage Examples:**
186
187
```scala
188
import play.core.ApplicationProvider
189
190
// Create provider from application
191
val provider = ApplicationProvider(myApplication)
192
193
// Check application availability
194
provider.get match {
195
case Right(app) => println(s"Application available: ${app.configuration}")
196
case Left(error) => println(s"Application error: ${error.getMessage}")
197
}
198
199
// Get current application
200
provider.current.foreach { app =>
201
println(s"Current app mode: ${app.mode}")
202
}
203
```
204
205
### Netty Configuration Options
206
207
Configure Netty-specific server behavior through Play configuration.
208
209
```scala { .api }
210
// Available configuration keys under play.server.netty:
211
val nettyConfigKeys = Map(
212
"maxInitialLineLength" -> "Maximum URL length (default: 4096)",
213
"maxHeaderSize" -> "Maximum HTTP header size (default: 8192)",
214
"maxChunkSize" -> "Maximum chunk buffer size (default: 8192)",
215
"log.wire" -> "Enable wire-level logging (default: false)",
216
"option.child.keepAlive" -> "TCP keep-alive for connections",
217
"option.child.tcpNoDelay" -> "TCP no-delay flag",
218
"option.backlog" -> "TCP connection backlog size"
219
)
220
```
221
222
**Usage Examples:**
223
224
```scala
225
// application.conf
226
play.server.netty {
227
maxInitialLineLength = 8192
228
maxHeaderSize = 16384
229
maxChunkSize = 16384
230
log.wire = true
231
232
option {
233
child.keepAlive = true
234
child.tcpNoDelay = true
235
backlog = 200
236
}
237
}
238
239
// Programmatic configuration
240
val config = Configuration(
241
"play.server.netty.maxInitialLineLength" -> 8192,
242
"play.server.netty.maxHeaderSize" -> 16384,
243
"play.server.netty.option.child.keepAlive" -> true
244
)
245
246
val serverConfig = ServerConfig(
247
port = Some(8080),
248
configuration = config
249
)
250
```