/
CommandLine.kt
140 lines (113 loc) · 5.14 KB
/
CommandLine.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
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
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.server.engine
import io.ktor.server.config.*
import io.ktor.server.config.ConfigLoader.Companion.load
import io.ktor.util.*
import io.ktor.util.logging.*
internal object ConfigKeys {
val applicationIdPath = "ktor.application.id"
val hostConfigPath = "ktor.deployment.host"
val hostPortPath = "ktor.deployment.port"
val hostWatchPaths = "ktor.deployment.watch"
val rootPathPath = "ktor.deployment.rootPath"
val hostSslPortPath = "ktor.deployment.sslPort"
val hostSslKeyStore = "ktor.security.ssl.keyStore"
val hostSslKeyAlias = "ktor.security.ssl.keyAlias"
val hostSslKeyStorePassword = "ktor.security.ssl.keyStorePassword"
val hostSslPrivateKeyPassword = "ktor.security.ssl.privateKeyPassword"
val developmentModeKey = "ktor.development"
}
/**
* Creates an [ApplicationEngineEnvironment] instance from command line arguments
*/
public fun commandLineEnvironment(args: Array<String>): ApplicationEngineEnvironment {
val argumentsPairs = args.mapNotNull { it.splitPair('=') }.toMap()
val configuration = buildApplicationConfig(args)
val applicationId = configuration.tryGetString(ConfigKeys.applicationIdPath) ?: "Application"
val logger = KtorSimpleLogger(applicationId)
val rootPath = argumentsPairs["-path"] ?: configuration.tryGetString(ConfigKeys.rootPathPath) ?: ""
val environment = applicationEngineEnvironment {
log = logger
configurePlatformProperties(args)
config = configuration
this.rootPath = rootPath
val host = argumentsPairs["-host"] ?: configuration.tryGetString(ConfigKeys.hostConfigPath) ?: "0.0.0.0"
val port = argumentsPairs["-port"] ?: configuration.tryGetString(ConfigKeys.hostPortPath)
val sslPort = argumentsPairs["-sslPort"] ?: configuration.tryGetString(ConfigKeys.hostSslPortPath)
val sslKeyStorePath = argumentsPairs["-sslKeyStore"] ?: configuration.tryGetString(ConfigKeys.hostSslKeyStore)
val sslKeyStorePassword = configuration.tryGetString(ConfigKeys.hostSslKeyStorePassword)?.trim()
val sslPrivateKeyPassword = configuration.tryGetString(ConfigKeys.hostSslPrivateKeyPassword)?.trim()
val sslKeyAlias = configuration.tryGetString(ConfigKeys.hostSslKeyAlias) ?: "mykey"
developmentMode = configuration.tryGetString(ConfigKeys.developmentModeKey)
?.let { it.toBoolean() } ?: PlatformUtils.IS_DEVELOPMENT_MODE
if (port != null) {
connector {
this.host = host
this.port = port.toInt()
}
}
if (sslPort != null) {
configureSSLConnectors(
host,
sslPort,
sslKeyStorePath,
sslKeyStorePassword,
sslPrivateKeyPassword,
sslKeyAlias
)
}
if (port == null && sslPort == null) {
throw IllegalArgumentException(
"Neither port nor sslPort specified. Use command line options -port/-sslPort " +
"or configure connectors in application.conf"
)
}
(argumentsPairs["-watch"]?.split(",") ?: configuration.tryGetStringList(ConfigKeys.hostWatchPaths))?.let {
watchPaths = it
}
}
return environment
}
internal fun buildApplicationConfig(args: Array<String>): ApplicationConfig {
val argumentsPairs = args.mapNotNull { it.splitPair('=') }
val commandLineProperties = argumentsPairs
.filter { it.first.startsWith("-P:") }
.map { it.first.removePrefix("-P:") to it.second }
val configPath = argumentsPairs.firstOrNull { it.first == "-config" }?.second
val commandLineConfig = MapApplicationConfig(commandLineProperties)
val environmentConfig = getConfigFromEnvironment()
val fileConfig = ConfigLoader.load(configPath)
return fileConfig.mergeWith(environmentConfig).mergeWith(commandLineConfig)
}
internal expect fun ApplicationEngineEnvironmentBuilder.configureSSLConnectors(
host: String,
sslPort: String,
sslKeyStorePath: String?,
sslKeyStorePassword: String?,
sslPrivateKeyPassword: String?,
sslKeyAlias: String
)
internal expect fun ApplicationEngineEnvironmentBuilder.configurePlatformProperties(args: Array<String>)
internal expect fun getConfigFromEnvironment(): ApplicationConfig
/**
* Load engine's configuration suitable for all engines from [deploymentConfig]
*/
public fun BaseApplicationEngine.Configuration.loadCommonConfiguration(deploymentConfig: ApplicationConfig) {
deploymentConfig.propertyOrNull("callGroupSize")?.getString()?.toInt()?.let {
callGroupSize = it
}
deploymentConfig.propertyOrNull("connectionGroupSize")?.getString()?.toInt()?.let {
connectionGroupSize = it
}
deploymentConfig.propertyOrNull("workerGroupSize")?.getString()?.toInt()?.let {
workerGroupSize = it
}
}
internal fun String.splitPair(ch: Char): Pair<String, String>? = indexOf(ch).let { idx ->
when (idx) {
-1 -> null
else -> Pair(take(idx), drop(idx + 1))
}
}