Skip to content
This repository was archived by the owner on Apr 15, 2026. It is now read-only.

Commit 32fa369

Browse files
committed
Shutdown webserver before starting new one
1 parent e90c589 commit 32fa369

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

src/main/kotlin/org/sandboxpowered/fabric/Main.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object Main {
1414
lateinit var resourceManager: ServerResourceManager
1515

1616
val loader = SandboxLoader()
17-
lateinit var webServer: WebServer
17+
var webServer: WebServer? = null
1818

1919
fun startSandboxInternals() {
2020

@@ -28,15 +28,19 @@ object Main {
2828
fun startSandboxDedicatedServer() {
2929
initSandboxContent()
3030
loader.load(Side.SERVER)
31-
webServer = WebServer()
32-
webServer.start()
31+
webServer?.stop()
32+
webServer = WebServer().apply { start() }
3333
}
3434

3535
fun startSandboxClient() {
3636
initSandboxContent()
3737
loader.load(Side.CLIENT)
3838
}
3939

40+
fun shutdownSandbox() {
41+
webServer?.stop()
42+
}
43+
4044
private fun initSandboxContent() {
4145
val c = Properties::class.java
4246
c.declaredFields.forEach {

src/main/kotlin/org/sandboxpowered/fabric/loading/WebServer.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ import io.ktor.server.engine.*
88
import io.ktor.server.netty.*
99
import org.apache.logging.log4j.LogManager.getLogger
1010
import team.yi.ktor.features.banner
11+
import java.util.concurrent.TimeUnit
1112
import kotlin.io.path.Path
1213
import kotlin.io.path.isDirectory
1314
import kotlin.io.path.notExists
1415

1516
class WebServer {
1617
private val globalPath = Path(".sandbox/cache/").toAbsolutePath()
18+
private var server: NettyApplicationEngine? = null
1719
fun start() {
1820
val log = getLogger()
19-
embeddedServer(Netty, port = 25566) {
21+
server = embeddedServer(Netty, port = 25566) {
2022
banner {
2123
bannerText = "Sandbox 2"
2224

@@ -41,12 +43,17 @@ class WebServer {
4143
HttpStatusCode.Forbidden,
4244
"Invalid Access"
4345
)
44-
cachePath.notExists() or cachePath.isDirectory() -> call.respond(HttpStatusCode.NotFound, "Unknown Path")
46+
cachePath.notExists() or cachePath.isDirectory() -> call.respond(HttpStatusCode.NotFound,
47+
"Unknown Path")
4548
else -> call.respondFile(cachePath.toFile())
4649
}
4750
}
4851
}
4952
}
5053
}.start(false)
5154
}
55+
56+
fun stop() {
57+
server?.stop(10, 10, TimeUnit.SECONDS)
58+
}
5259
}

0 commit comments

Comments
 (0)