Update UncivServer: (#6427)

- Add `clikt` lib.
- Add custom multiplayer file's folder.
This commit is contained in:
Tang 2022-03-28 09:36:42 -05:00 committed by GitHub
parent 477ac78856
commit ee1af44809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 21 deletions

View File

@ -76,6 +76,7 @@ project(":desktop") {
"implementation"("io.ktor:ktor-server-core:1.6.8")
"implementation"("io.ktor:ktor-server-netty:1.6.8")
"implementation"("ch.qos.logback:logback-classic:1.2.5")
"implementation"("com.github.ajalt.clikt:clikt:3.4.0")
}
}

View File

@ -1,5 +1,10 @@
package com.unciv.app.desktop
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.int
import com.github.ajalt.clikt.parameters.types.restrictTo
import io.ktor.application.*
import io.ktor.response.*
import io.ktor.routing.*
@ -12,24 +17,29 @@ import java.io.File
internal object UncivServer {
private var serverPort = 8080
@JvmStatic
fun main(args: Array<String>) {
args.forEach { arg ->
when {
arg.startsWith("-port=") -> with(arg.removePrefix("-port=").toIntOrNull() ?: 0) {
if (this in 1024..49151) serverPort = this
else println("'port' must be between 1024 and 49151")
}
}
fun main(args: Array<String>) = UncivServerRunner().main(args)
}
println("Server will run on $serverPort port, you can use '-port=XXXX' custom port.")
private class UncivServerRunner : CliktCommand() {
private val port by option(
"-p", "-port",
envvar = "UncivServerPort",
help = "Server port"
).int().restrictTo(1024..49151).default(8080)
val fileFolderName = "MultiplayerFiles"
File(fileFolderName).mkdir()
println(File(fileFolderName).absolutePath)
private val folder by option(
"-f", "-folder",
envvar = "UncivServerFolder",
help = "Multiplayer file's folder"
).default("MultiplayerFiles")
override fun run() {
serverRun(port, folder)
}
private fun serverRun(serverPort: Int, fileFolderName: String) {
echo("Starting UncivServer for ${File(fileFolderName).absolutePath} on port $serverPort")
embeddedServer(Netty, port = serverPort) {
routing {
get("/isalive") {