0
# Standalone Server Execution
1
2
Main entry point for running standalone Ktor Netty servers from command line or application main functions.
3
4
## Capabilities
5
6
### EngineMain Object
7
8
Main object for running standalone Netty servers with command-line argument support and configuration loading.
9
10
```kotlin { .api }
11
/**
12
* Netty engine main entry point for standalone applications
13
*/
14
object EngineMain {
15
/**
16
* Main function for starting EngineMain with Netty
17
* Creates an embedded Netty application with an environment built from command line arguments
18
* @param args Command line arguments for server configuration
19
*/
20
@JvmStatic
21
fun main(args: Array<String>)
22
23
/**
24
* Creates an instance of the embedded Netty server without starting it
25
* @param args Command line arguments for configuring the server
26
* @return An instance of EmbeddedServer with the specified configuration
27
*/
28
fun createServer(
29
args: Array<String>
30
): EmbeddedServer<NettyApplicationEngine, NettyApplicationEngine.Configuration>
31
}
32
```
33
34
**Usage Examples:**
35
36
```kotlin
37
import io.ktor.server.netty.*
38
39
// Basic standalone server
40
fun main(args: Array<String>) {
41
EngineMain.main(args)
42
}
43
44
// Create server without starting (for testing or custom startup logic)
45
fun main(args: Array<String>) {
46
val server = EngineMain.createServer(args)
47
48
// Perform additional setup
49
// ...
50
51
// Start when ready
52
server.start(wait = true)
53
}
54
```
55
56
### Command Line Arguments
57
58
The EngineMain accepts standard command line arguments for server configuration:
59
60
**Common Arguments:**
61
- `-port=8080` - Server port
62
- `-host=0.0.0.0` - Bind address
63
- `-config=application.conf` - Configuration file path
64
- `-P:ktor.deployment.port=8080` - Set configuration property directly
65
66
**Example Usage:**
67
68
```bash
69
# Run with specific port
70
java -jar myapp.jar -port=8080
71
72
# Run with custom configuration
73
java -jar myapp.jar -config=production.conf
74
75
# Run with multiple properties
76
java -jar myapp.jar -port=8080 -P:ktor.deployment.watch=io.ktor.samples
77
```
78
79
### Configuration File Support
80
81
EngineMain supports loading configuration from HOCON files (application.conf):
82
83
```hocon
84
ktor {
85
deployment {
86
port = 8080
87
host = "0.0.0.0"
88
89
# Netty-specific configuration
90
runningLimit = 64
91
shareWorkGroup = true
92
responseWriteTimeoutSeconds = 30
93
requestReadTimeoutSeconds = 0
94
tcpKeepAlive = true
95
maxInitialLineLength = 4096
96
maxHeaderSize = 8192
97
maxChunkSize = 8192
98
}
99
100
application {
101
modules = [ com.example.ApplicationKt.main ]
102
}
103
}
104
```
105
106
### Application Module Loading
107
108
EngineMain automatically loads and initializes application modules specified in configuration:
109
110
```kotlin
111
// Application.kt
112
package com.example
113
114
import io.ktor.server.application.*
115
import io.ktor.server.response.*
116
import io.ktor.server.routing.*
117
118
fun Application.main() {
119
routing {
120
get("/") {
121
call.respondText("Hello from standalone Ktor Netty server!")
122
}
123
124
get("/health") {
125
call.respondText("OK")
126
}
127
}
128
}
129
```
130
131
### Environment Variables
132
133
EngineMain supports environment variable substitution in configuration files:
134
135
```hocon
136
ktor {
137
deployment {
138
port = ${?PORT} # Uses PORT env var if available
139
host = ${?HOST} # Uses HOST env var if available
140
141
# Netty configuration from environment
142
runningLimit = ${?KTOR_RUNNING_LIMIT}
143
tcpKeepAlive = ${?KTOR_TCP_KEEP_ALIVE}
144
}
145
}
146
```
147
148
### Development vs Production
149
150
EngineMain automatically detects development mode and adjusts behavior:
151
152
**Development Mode Features:**
153
- Auto-reload on code changes (when watch paths are configured)
154
- Detailed error pages
155
- Additional logging
156
157
**Production Mode Features:**
158
- Optimized performance settings
159
- Minimal error information exposure
160
- Production-ready defaults
161
162
```hocon
163
ktor {
164
development = true # Enable development mode
165
166
deployment {
167
watch = [ "classes", "resources" ] # Auto-reload paths
168
}
169
}
170
```