SNRK-83: changed decomposition and moved main to test/

This commit is contained in:
Anton Belyi 2023-05-05 15:24:01 +03:00
parent b4423e8bba
commit ff7b02d98a
2 changed files with 67 additions and 48 deletions

View File

@ -11,6 +11,7 @@ import io.ktor.server.response.*
import io.ktor.server.http.content.* import io.ktor.server.http.content.*
import kotlinx.html.* import kotlinx.html.*
import io.ktor.server.routing.* import io.ktor.server.routing.*
import kotlinx.css.html
import java.nio.file.Path import java.nio.file.Path
import space.kscience.snark.storage.Directory import space.kscience.snark.storage.Directory
import space.kscience.snark.storage.local.localStorage import space.kscience.snark.storage.local.localStorage
@ -24,11 +25,13 @@ public interface DataHolder {
fun init() : Directory fun init() : Directory
fun represent(): String fun represent(): String
//will be HTML later
} }
class LocalDataHolder: DataHolder { class LocalDataHolder: DataHolder {
private var source: Path? = null private var source: Path? = null
private var response: String = "" private var response: String = ""
override fun init(): Directory { override fun init(): Directory {
source?.toFile()?.deleteRecursively()
source = createTempDirectory() source = createTempDirectory()
return localStorage(source!!) return localStorage(source!!)
} }
@ -50,58 +53,69 @@ class LocalDataHolder: DataHolder {
response response
} }
} }
fun main() {
val dataHolder: DataHolder = LocalDataHolder() public class SNARKServer(val dataHolder: DataHolder, val port: Int): Runnable {
embeddedServer(Netty, 9090) { private suspend fun renderGet(call: ApplicationCall) {
routing { call.respondText(dataHolder.represent())
get("/") { }
call.respondHtml(HttpStatusCode.OK) { private suspend fun renderUpload(call: ApplicationCall) {
head { val multipartData = call.receiveMultipart()
title { val tmp = createTempFile(suffix=".zip")
+"SNARK" multipartData.forEachPart { part ->
} when (part) {
} is PartData.FileItem -> {
body { val fileBytes = part.streamProvider().readBytes()
h1 { tmp.writeBytes(fileBytes)
+"SNARK" }
} else -> {
}
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"
}
}
} }
} }
post("/upload") { part.dispose()
val multipartData = call.receiveMultipart() }
val tmp = createTempFile(suffix=".zip") unzip(tmp.toPath().toString(), dataHolder.init())
multipartData.forEachPart { part -> call.respondText("File is successfully uploaded")
when (part) { }
is PartData.FileItem -> { private suspend fun renderMainPage(call: ApplicationCall) {
val fileBytes = part.streamProvider().readBytes() call.respondHtml(HttpStatusCode.OK) {
tmp.writeBytes(fileBytes) head {
} title {
else -> { +"SNARK"
}
}
part.dispose()
} }
unzip(tmp.toPath().toString(), dataHolder.init())
call.respondText("File is successfully uploaded")
} }
get("/data") { body {
call.respondText(dataHolder.represent()) 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)
}
} }

View File

@ -0,0 +1,5 @@
package space.kscience.snark.ktor
fun main() {
SNARKServer(LocalDataHolder(), 9090).run()
}