Skip to content

Commit

Permalink
Fix webhost to see resource files
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Jan 21, 2025
1 parent 60e8944 commit a10eee2
Showing 1 changed file with 24 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.typewritermc.engine.paper.ui


import io.ktor.server.application.*
import com.typewritermc.engine.paper.logger
import com.typewritermc.engine.paper.plugin
import com.typewritermc.engine.paper.utils.config
import io.ktor.server.engine.*
import io.ktor.server.http.content.*
import io.ktor.server.response.*
import io.ktor.server.netty.*
import io.ktor.server.routing.*
import com.typewritermc.engine.paper.logger
import com.typewritermc.engine.paper.utils.config
import io.ktor.util.logging.*
import org.koin.core.component.KoinComponent
import java.io.File

class PanelHost : KoinComponent {
private val enabled: Boolean by config("panel.enabled", false)
Expand All @@ -22,39 +21,30 @@ class PanelHost : KoinComponent {
logger.warning("The panel is disabled while the websocket is enabled. This is only for development purposes. Please enable either both or none.")
return
}
server = embeddedServer(io.ktor.server.netty.Netty, port) {
routing {
static {
serveResources("web")
defaultResource("index.html", "web")
val classLoader = plugin.javaClass.classLoader

// We have to construct the environment manually as Paper doesn't allow the default classloader
// to see the web resources.
val environment = applicationEngineEnvironment {
this.classLoader = classLoader
this.log = KtorSimpleLogger("ktor.application")
this.module {
routing {
staticResources("/", "web") {
default("index.html")
}
}
}
}.start(wait = false)
connector {
host = "0.0.0.0"
port = this@PanelHost.port
}
}

server = embeddedServer(factory = Netty, environment = environment).start(wait = false)
}

fun dispose() {
server?.stop()
}
}

fun Route.serveResources(resourcePackage: String? = null) {
get("{static-resources...}/") {
call.serve(resourcePackage)
}

get("{static-resources...}") {
call.serve(resourcePackage)
}
}

suspend fun ApplicationCall.serve(resourcePackage: String? = null) {
val relativePath = parameters.getAll("static-resources")?.joinToString(File.separator) ?: return

// This is key part. We either resolve some resource or resolve index.html using path from the request
val content = resolveResource(relativePath, resourcePackage)
?: resolveResource("$relativePath/index.html", resourcePackage)

if (content != null) {
respond(content)
}
}

0 comments on commit a10eee2

Please sign in to comment.