SNRK-98: fixed snark-main

This commit is contained in:
Anton Belyi 2023-05-17 14:58:50 +03:00
parent 45132e4355
commit 4ee51ebc0c
3 changed files with 19 additions and 14 deletions

View File

@ -12,20 +12,24 @@ import kotlinx.html.*
import io.ktor.server.routing.* import io.ktor.server.routing.*
import space.kscience.snark.storage.Directory import space.kscience.snark.storage.Directory
import space.kscience.snark.storage.unzip.unzip import space.kscience.snark.storage.unzip.unzip
import java.io.File
import java.nio.file.Path
import kotlin.io.createTempFile import kotlin.io.createTempFile
import kotlin.io.writeBytes import kotlin.io.writeBytes
import kotlin.io.path.Path
public interface DataHolder { 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 { 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) { private suspend fun receivePath(call: ApplicationCall) {
relativePath = call.receiveParameters()["path"]?:"/" val pathString = call.receiveParameters()["path"]?:""
relativePath = Path(pathString.dropWhile{it == '/'})
call.respondRedirect("/") call.respondRedirect("/")
} }
private suspend fun renderGet(call: ApplicationCall) { private suspend fun renderGet(call: ApplicationCall) {
@ -60,7 +64,7 @@ public class SNARKServer(private val dataHolder: DataHolder, private val port: I
+"SNARK" +"SNARK"
} }
p { p {
+("Path: " + relativePath) +("Path: /" + relativePath.toString())
} }
} }
body { body {

View File

@ -12,10 +12,10 @@ private class LocalDataHolder: DataHolder {
private var source: Path? = null private var source: Path? = null
private var response: String = "" private var response: String = ""
private fun getPath(relativePath: String) : Path { private fun getPath(relativePath: Path) : Path {
return source!! / Path(relativePath.dropWhile{it == '/'}) return source!! / relativePath
} }
override fun init(relativePath: String): Directory { override suspend fun init(relativePath: Path): Directory {
if (source == null) { if (source == null) {
source = createTempDirectory() 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) { if (source == null) {
"No data was loaded!" "No data was loaded!"
} else { } else {

View File

@ -3,13 +3,14 @@ package space.kscience.snark.main
import space.kscience.snark.ktor.DataHolder import space.kscience.snark.ktor.DataHolder
import space.kscience.snark.storage.Directory import space.kscience.snark.storage.Directory
import documentBuilder.* import documentBuilder.*
import kotlinx.html.HTML import java.nio.file.Path
internal class ServerDataHolder(private val directory: Directory): DataHolder { internal class ServerDataHolder(private val directory: Directory): DataHolder {
override fun init(): Directory =
directory
override suspend fun represent(): String { override suspend fun init(relativePath: Path): Directory = directory.getSubdir(relativePath)
return buildDocument(directory).toString()
override suspend fun represent(relativePath: Path): String {
return buildDocument(init(relativePath))
} }
} }