SNRK-98: fixed snark-main
This commit is contained in:
parent
45132e4355
commit
4ee51ebc0c
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user