Some code cleaning
This commit is contained in:
@@ -84,6 +84,35 @@ private fun launchServer() {
|
|||||||
val viewContext = createContext("view", "/")
|
val viewContext = createContext("view", "/")
|
||||||
|
|
||||||
// handle properties
|
// handle properties
|
||||||
|
readProperties(apiContext, viewContext)
|
||||||
|
|
||||||
|
val webappUrl = serverProps.getProperty("webapp.url")?.let { URL(it) } ?: throw Error("missing property webapp.url")
|
||||||
|
val secure = webappUrl.protocol == "https"
|
||||||
|
|
||||||
|
// create server
|
||||||
|
val server =
|
||||||
|
if (secure) Server()
|
||||||
|
else Server(webappUrl.port)
|
||||||
|
|
||||||
|
server.apply {
|
||||||
|
// register webapps
|
||||||
|
handler = ContextHandlerCollection(apiContext, viewContext)
|
||||||
|
if (secure) {
|
||||||
|
val connector = buildSecureConnector(server, webappUrl.port)
|
||||||
|
addConnector(connector)
|
||||||
|
}
|
||||||
|
// launch server
|
||||||
|
start()
|
||||||
|
join()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createContext(webapp: String, contextPath: String) = WebAppContext().also { context ->
|
||||||
|
context.war = "$tmp/pairgoth/webapps/$webapp-webapp-$version.war"
|
||||||
|
context.contextPath = contextPath
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun readProperties(vararg contexts: WebAppContext) {
|
||||||
val defaultProps = getResource("/server.default.properties") ?: throw Error("missing default server properties")
|
val defaultProps = getResource("/server.default.properties") ?: throw Error("missing default server properties")
|
||||||
defaultProps.openStream().use {
|
defaultProps.openStream().use {
|
||||||
serverProps.load(InputStreamReader(it, StandardCharsets.UTF_8))
|
serverProps.load(InputStreamReader(it, StandardCharsets.UTF_8))
|
||||||
@@ -97,8 +126,9 @@ private fun launchServer() {
|
|||||||
if (property.startsWith("logger.")) {
|
if (property.startsWith("logger.")) {
|
||||||
// special handling for logger properties
|
// special handling for logger properties
|
||||||
val webappLoggerPropKey = "webapp-slf4j-logger.${property.substring(7)}"
|
val webappLoggerPropKey = "webapp-slf4j-logger.${property.substring(7)}"
|
||||||
apiContext.setInitParameter(webappLoggerPropKey, value)
|
contexts.forEach { context ->
|
||||||
viewContext.setInitParameter(webappLoggerPropKey, value)
|
context.setInitParameter(webappLoggerPropKey, value)
|
||||||
|
}
|
||||||
} else if (property.startsWith("webapp.ssl.")) {
|
} else if (property.startsWith("webapp.ssl.")) {
|
||||||
// do not propagate ssl properties further
|
// do not propagate ssl properties further
|
||||||
} else {
|
} else {
|
||||||
@@ -106,13 +136,9 @@ private fun launchServer() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// create server
|
private fun buildSecureConnector(server: Server, port: Int): ServerConnector {
|
||||||
val server = Server()
|
|
||||||
|
|
||||||
// register webapps
|
|
||||||
server.handler = ContextHandlerCollection(apiContext, viewContext)
|
|
||||||
|
|
||||||
// set up http/2
|
// set up http/2
|
||||||
val httpConfig = HttpConfiguration().apply {
|
val httpConfig = HttpConfiguration().apply {
|
||||||
addCustomizer(SecureRequestCustomizer())
|
addCustomizer(SecureRequestCustomizer())
|
||||||
@@ -124,7 +150,8 @@ private fun launchServer() {
|
|||||||
}
|
}
|
||||||
val cert = getResourceProperty("webapp.ssl.cert").readBytes()
|
val cert = getResourceProperty("webapp.ssl.cert").readBytes()
|
||||||
val key = getResourceProperty("webapp.ssl.key").readText().let {
|
val key = getResourceProperty("webapp.ssl.key").readText().let {
|
||||||
val encodedKey = Pattern.compile("(?m)(?s)^---*BEGIN.*---*$(.*)^---*END.*---*$.*").matcher(it).replaceFirst("$1")
|
val encodedKey =
|
||||||
|
Pattern.compile("(?m)(?s)^---*BEGIN.*---*$(.*)^---*END.*---*$.*").matcher(it).replaceFirst("$1")
|
||||||
Base64.getDecoder().decode(encodedKey.replace("\n", ""))
|
Base64.getDecoder().decode(encodedKey.replace("\n", ""))
|
||||||
}
|
}
|
||||||
val pass = serverProps.getProperty("webapp.ssl.pass") ?: "foobar"
|
val pass = serverProps.getProperty("webapp.ssl.pass") ?: "foobar"
|
||||||
@@ -136,27 +163,25 @@ private fun launchServer() {
|
|||||||
val certificateFactory = CertificateFactory.getInstance("X.509")
|
val certificateFactory = CertificateFactory.getInstance("X.509")
|
||||||
val store = KeyStore.getInstance("JKS").apply {
|
val store = KeyStore.getInstance("JKS").apply {
|
||||||
load(null)
|
load(null)
|
||||||
setCertificateEntry("certificate", certificateFactory.generateCertificate(ByteArrayInputStream(cert)) as X509Certificate)
|
setCertificateEntry(
|
||||||
setKeyEntry("key", privKey, pass.toCharArray(), arrayOf(certificateFactory.generateCertificate(ByteArrayInputStream(cert))))
|
"certificate",
|
||||||
|
certificateFactory.generateCertificate(ByteArrayInputStream(cert)) as X509Certificate
|
||||||
|
)
|
||||||
|
setKeyEntry(
|
||||||
|
"key",
|
||||||
|
privKey,
|
||||||
|
pass.toCharArray(),
|
||||||
|
arrayOf(certificateFactory.generateCertificate(ByteArrayInputStream(cert)))
|
||||||
|
)
|
||||||
}
|
}
|
||||||
val sslContextFactory = SslContextFactory.Server().apply {
|
val sslContextFactory = SslContextFactory.Server().apply {
|
||||||
keyStoreType = "JKS"
|
keyStoreType = "JKS"
|
||||||
keyStore = store
|
keyStore = store
|
||||||
keyStorePassword = pass
|
keyStorePassword = pass
|
||||||
// if (pass.isNotEmpty()) keyManagerPassword = pass
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val tls = SslConnectionFactory(sslContextFactory, alpn.protocol)
|
val tls = SslConnectionFactory(sslContextFactory, alpn.protocol)
|
||||||
val connector = ServerConnector(server, tls, alpn, h2, http11)
|
val connector = ServerConnector(server, tls, alpn, h2, http11)
|
||||||
connector.port = 8443
|
connector.port = port
|
||||||
server.addConnector(connector)
|
return connector
|
||||||
|
|
||||||
// launch server
|
|
||||||
server.start()
|
|
||||||
server.join()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun createContext(webapp: String, contextPath: String) = WebAppContext().also { context ->
|
|
||||||
context.war = "$tmp/pairgoth/webapps/$webapp-webapp-$version.war"
|
|
||||||
context.contextPath = contextPath
|
|
||||||
}
|
}
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
webapp.url = https://localhost:8443
|
||||||
webapp.ssl.key = jar:file:$jar!/ssl/localhost.key
|
webapp.ssl.key = jar:file:$jar!/ssl/localhost.key
|
||||||
# webapp.ssl.pass = foobar (not supported for now)
|
# webapp.ssl.pass = foobar (not supported for now)
|
||||||
webapp.ssl.cert = jar:file:$jar!/ssl/localhost.crt
|
webapp.ssl.cert = jar:file:$jar!/ssl/localhost.crt
|
||||||
|
Reference in New Issue
Block a user