import io.ktor.server.engine.*
import io.ktor.server.http.content.*
import io.ktor.server.netty.*
import io.ktor.server.routing.*
import java.io.File
/**
* Sets up s simple web server that will serve files from a directory.
* By default, this is the `Demo` folder, but can be overridden by passing a single argument to the
* program's [main] entry point.
*
* It also serves the `.js` file from folder `build/dist/js/productionExecutable/`.
* e.g. for the `Demo` game, `http://localhost:8888/js/Demo.js` is served from
* `Demo/build/dist/js/productionExecutable/Demo.js`.
*
* Note, this is NOT designed to be used for production. It gives access to ALL files,
* not only the game's resources and the .js file.
*/
object DemoServer {
/**
* @param resourcesPath The directory containing images (which can be loaded on-demand).
*/
private fun startServer(port: Int, jsPath: File, resourcesPath: File) {
println("Starting server http://localhost:$port")
println(" resourcesPath = ${resourcesPath.absoluteFile}")
embeddedServer(Netty, port = port) {
routing {
staticFiles("/", resourcesPath)
staticFiles("/js", jsPath)
// Serves glok's resources. e.g. images for glok's controls.
// These aren't needed for most games. (But glok will probably grumble if it doesn't find them!)
staticResources("/glok", "")
}
}.start(wait = true)
}
@JvmStatic
fun main(vararg args: String) {
val gamePath = if (args.size == 1) {
args[0]
} else {
"Demo"
}
val resourcesPath = File(gamePath)
val jsPath = File(resourcesPath, "build/dist/js/productionExecutable/")
val port = 8888
startServer(port, jsPath, resourcesPath)
}
}