/
EngineMain.kt
64 lines (59 loc) · 2.49 KB
/
EngineMain.kt
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
/*
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.server.netty
import io.ktor.server.config.*
import io.ktor.server.engine.*
/**
* Netty engine
*/
public object EngineMain {
/**
* Main function for starting EngineMain with Netty
* Creates an embedded Netty application with an environment built from command line arguments.
*/
@JvmStatic
public fun main(args: Array<String>) {
val applicationEnvironment = commandLineEnvironment(args)
val engine = NettyApplicationEngine(applicationEnvironment) { loadConfiguration(applicationEnvironment.config) }
val gracePeriod =
engine.environment.config.propertyOrNull("ktor.deployment.shutdownGracePeriod")?.getString()?.toLong() ?: 3000
val timeout =
engine.environment.config.propertyOrNull("ktor.deployment.shutdownTimeout")?.getString()?.toLong() ?: 5000
engine.addShutdownHook {
engine.stop(gracePeriod, timeout)
}
engine.start(true)
}
internal fun NettyApplicationEngine.Configuration.loadConfiguration(config: ApplicationConfig) {
val deploymentConfig = config.config("ktor.deployment")
loadCommonConfiguration(deploymentConfig)
deploymentConfig.propertyOrNull("requestQueueLimit")?.getString()?.toInt()?.let {
requestQueueLimit = it
}
deploymentConfig.propertyOrNull("runningLimit")?.getString()?.toInt()?.let {
runningLimit = it
}
deploymentConfig.propertyOrNull("shareWorkGroup")?.getString()?.toBoolean()?.let {
shareWorkGroup = it
}
deploymentConfig.propertyOrNull("responseWriteTimeoutSeconds")?.getString()?.toInt()?.let {
responseWriteTimeoutSeconds = it
}
deploymentConfig.propertyOrNull("requestReadTimeoutSeconds")?.getString()?.toInt()?.let {
requestReadTimeoutSeconds = it
}
deploymentConfig.propertyOrNull("tcpKeepAlive")?.getString()?.toBoolean()?.let {
tcpKeepAlive = it
}
deploymentConfig.propertyOrNull("maxInitialLineLength")?.getString()?.toInt()?.let {
maxInitialLineLength = it
}
deploymentConfig.propertyOrNull("maxHeaderSize")?.getString()?.toInt()?.let {
maxHeaderSize = it
}
deploymentConfig.propertyOrNull("maxChunkSize")?.getString()?.toInt()?.let {
maxChunkSize = it
}
}
}