From 4ee51ebc0c7088c0f9956ec3b820b09be70a5353 Mon Sep 17 00:00:00 2001 From: Anton Belyi Date: Wed, 17 May 2023 14:58:50 +0300 Subject: [PATCH] SNRK-98: fixed snark-main --- .../space/kscience/snark/ktor/WebInterface.kt | 14 +++++++++----- .../kotlin/space/kscience/snark/ktor/testServer.kt | 8 ++++---- .../space/kscience/snark/main/ServerDataHolder.kt | 11 ++++++----- 3 files changed, 19 insertions(+), 14 deletions(-) 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 ccdf32a..5bae320 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 @@ -12,20 +12,24 @@ import kotlinx.html.* import io.ktor.server.routing.* import space.kscience.snark.storage.Directory import space.kscience.snark.storage.unzip.unzip +import java.io.File +import java.nio.file.Path import kotlin.io.createTempFile import kotlin.io.writeBytes +import kotlin.io.path.Path public interface DataHolder { - public fun init(relativePath: String = "/") : Directory + public suspend fun init(relativePath: Path) : Directory - public fun represent(relativePath: String = "/"): String + public suspend fun represent(relativePath: Path): String } public class SNARKServer(private val dataHolder: DataHolder, private val port: Int): Runnable { - private var relativePath = "/" + private var relativePath = Path("") private suspend fun receivePath(call: ApplicationCall) { - relativePath = call.receiveParameters()["path"]?:"/" + val pathString = call.receiveParameters()["path"]?:"" + relativePath = Path(pathString.dropWhile{it == '/'}) call.respondRedirect("/") } private suspend fun renderGet(call: ApplicationCall) { @@ -60,7 +64,7 @@ public class SNARKServer(private val dataHolder: DataHolder, private val port: I +"SNARK" } p { - +("Path: " + relativePath) + +("Path: /" + relativePath.toString()) } } body { 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 index 6a16f68..9cc5e74 100644 --- a/snark-ktor/src/test/kotlin/space/kscience/snark/ktor/testServer.kt +++ b/snark-ktor/src/test/kotlin/space/kscience/snark/ktor/testServer.kt @@ -12,10 +12,10 @@ private class LocalDataHolder: DataHolder { private var source: Path? = null private var response: String = "" - private fun getPath(relativePath: String) : Path { - return source!! / Path(relativePath.dropWhile{it == '/'}) + private fun getPath(relativePath: Path) : Path { + return source!! / relativePath } - override fun init(relativePath: String): Directory { + override suspend fun init(relativePath: Path): Directory { if (source == null) { source = createTempDirectory() } @@ -34,7 +34,7 @@ private class LocalDataHolder: DataHolder { } } } - override fun represent(relativePath: String) : String = + override suspend fun represent(relativePath: Path) : String = if (source == null) { "No data was loaded!" } else { diff --git a/snark-main/src/main/kotlin/space/kscience/snark/main/ServerDataHolder.kt b/snark-main/src/main/kotlin/space/kscience/snark/main/ServerDataHolder.kt index ce1a9e1..1b7b4a7 100644 --- a/snark-main/src/main/kotlin/space/kscience/snark/main/ServerDataHolder.kt +++ b/snark-main/src/main/kotlin/space/kscience/snark/main/ServerDataHolder.kt @@ -3,13 +3,14 @@ package space.kscience.snark.main import space.kscience.snark.ktor.DataHolder import space.kscience.snark.storage.Directory import documentBuilder.* -import kotlinx.html.HTML +import java.nio.file.Path internal class ServerDataHolder(private val directory: Directory): DataHolder { - override fun init(): Directory = - directory - override suspend fun represent(): String { - return buildDocument(directory).toString() + override suspend fun init(relativePath: Path): Directory = directory.getSubdir(relativePath) + + + override suspend fun represent(relativePath: Path): String { + return buildDocument(init(relativePath)) } } \ No newline at end of file