From ff7b02d98aada727fab0a13db8b2cd8d3f3ecbaa Mon Sep 17 00:00:00 2001 From: Anton Belyi Date: Fri, 5 May 2023 15:24:01 +0300 Subject: [PATCH] SNRK-83: changed decomposition and moved main to test/ --- .../space/kscience/snark/ktor/WebInterface.kt | 110 ++++++++++-------- .../space/kscience/snark/ktor/testServer.kt | 5 + 2 files changed, 67 insertions(+), 48 deletions(-) create mode 100644 snark-ktor/src/test/kotlin/space/kscience/snark/ktor/testServer.kt diff --git a/snark-ktor/src/main/kotlin/space/kscience/snark/ktor/WebInterface.kt b/snark-ktor/src/main/kotlin/space/kscience/snark/ktor/WebInterface.kt index 2765170..368bad6 100644 --- a/snark-ktor/src/main/kotlin/space/kscience/snark/ktor/WebInterface.kt +++ b/snark-ktor/src/main/kotlin/space/kscience/snark/ktor/WebInterface.kt @@ -11,6 +11,7 @@ import io.ktor.server.response.* import io.ktor.server.http.content.* import kotlinx.html.* import io.ktor.server.routing.* +import kotlinx.css.html import java.nio.file.Path import space.kscience.snark.storage.Directory import space.kscience.snark.storage.local.localStorage @@ -24,11 +25,13 @@ public interface DataHolder { fun init() : Directory fun represent(): String + //will be HTML later } class LocalDataHolder: DataHolder { private var source: Path? = null private var response: String = "" override fun init(): Directory { + source?.toFile()?.deleteRecursively() source = createTempDirectory() return localStorage(source!!) } @@ -50,58 +53,69 @@ class LocalDataHolder: DataHolder { response } } -fun main() { - val dataHolder: DataHolder = LocalDataHolder() - embeddedServer(Netty, 9090) { - routing { - get("/") { - call.respondHtml(HttpStatusCode.OK) { - head { - title { - +"SNARK" - } - } - body { - h1 { - +"SNARK" - } - } - body { - postForm (action = "/upload", encType = FormEncType.multipartFormData) { - label { - +"Choose zip archive: " - } - input (name = "file", type = InputType.file) {} - button { - +"Upload file" - } - } - a("/data") { - +"Show data\n" - } - } + +public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable { + private suspend fun renderGet(call: ApplicationCall) { + call.respondText(dataHolder.represent()) + } + private suspend fun renderUpload(call: ApplicationCall) { + val multipartData = call.receiveMultipart() + val tmp = createTempFile(suffix=".zip") + multipartData.forEachPart { part -> + when (part) { + is PartData.FileItem -> { + val fileBytes = part.streamProvider().readBytes() + tmp.writeBytes(fileBytes) + } + else -> { } } - post("/upload") { - val multipartData = call.receiveMultipart() - val tmp = createTempFile(suffix=".zip") - multipartData.forEachPart { part -> - when (part) { - is PartData.FileItem -> { - val fileBytes = part.streamProvider().readBytes() - tmp.writeBytes(fileBytes) - } - else -> { - } - } - part.dispose() + part.dispose() + } + unzip(tmp.toPath().toString(), dataHolder.init()) + call.respondText("File is successfully uploaded") + } + private suspend fun renderMainPage(call: ApplicationCall) { + call.respondHtml(HttpStatusCode.OK) { + head { + title { + +"SNARK" } - unzip(tmp.toPath().toString(), dataHolder.init()) - call.respondText("File is successfully uploaded") } - get("/data") { - call.respondText(dataHolder.represent()) + body { + h1 { + +"SNARK" + } + } + body { + postForm (action = "/upload", encType = FormEncType.multipartFormData) { + label { + +"Choose zip archive: " + } + input (name = "file", type = InputType.file) {} + button { + +"Upload file" + } + } + a("/data") { + +"Show data\n" + } } } - }.start(wait = true) + } + override fun run() { + embeddedServer(Netty, port) { + routing { + get("/") { + renderMainPage(call) + } + post("/upload") { + renderUpload(call) + } + get("/data") { + renderGet(call) + } + } + }.start(wait = true) + } } \ No newline at end of file diff --git a/snark-ktor/src/test/kotlin/space/kscience/snark/ktor/testServer.kt b/snark-ktor/src/test/kotlin/space/kscience/snark/ktor/testServer.kt new file mode 100644 index 0000000..6c4ee11 --- /dev/null +++ b/snark-ktor/src/test/kotlin/space/kscience/snark/ktor/testServer.kt @@ -0,0 +1,5 @@ +package space.kscience.snark.ktor + +fun main() { + SNARKServer(LocalDataHolder(), 9090).run() +} \ No newline at end of file