Last active
June 7, 2022 17:06
-
-
Save AlexeySoshin/211a417f36d19f33e953db48f8165e90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.ktor.network.tls.certificates.buildKeyStore | |
import io.ktor.network.tls.certificates.saveToFile | |
import io.ktor.network.tls.extensions.HashAlgorithm | |
import io.ktor.network.tls.extensions.SignatureAlgorithm | |
import io.ktor.routing.routing | |
import io.ktor.server.engine.applicationEngineEnvironment | |
import io.ktor.server.engine.embeddedServer | |
import io.ktor.server.engine.sslConnector | |
import io.ktor.server.netty.Netty | |
import io.ktor.server.netty.NettyApplicationEngine | |
import java.io.File | |
import java.security.KeyStore | |
class HttpsServer { | |
val pass = "abcd1234" | |
val server: NettyApplicationEngine | |
init { | |
server = createServer() | |
} | |
private fun createServer(): NettyApplicationEngine { | |
val alias = "certificateAlias" | |
val keystore = buildKeyStore { | |
certificate(alias) { | |
hash = HashAlgorithm.SHA256 | |
sign = SignatureAlgorithm.ECDSA | |
keySizeInBits = 256 | |
password = pass | |
} | |
} | |
val server = embeddedServer(Netty, applicationEngineEnvironment { | |
sslConnector(keystore, | |
alias, | |
{ "".toCharArray() }, | |
{ pass.toCharArray() }) { | |
port = 8181 | |
keyStorePath = keyStore.asFile.absoluteFile | |
module { | |
// Your usual definitions, such as Content Negotiation, go here | |
routing { | |
// Your routes go here | |
} | |
} | |
} | |
}) | |
return server | |
} | |
fun start() { | |
server.start() | |
} | |
private val KeyStore.asFile: File | |
get() { | |
val keyStoreFile = File("build/temp.jks") | |
this.saveToFile(keyStoreFile, pass) | |
return keyStoreFile | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dear Sir
very thx your demo
i also ref other demo
this one use extra config file : "application.$environment.conf"
if i run ktor server at android with the same config
do you know how load ?
because "ConfigFactory.load" base on ClassLoader
THX
https://github.com/samgarasx/ktor-webapi/blob/0a9b0c56879a00919d62fdd8b9933e2e45e6309d/src/main/kotlin/ktorwebapi/Server.kt#L15