1
0
forked from SPC/spc-site

Temporary workaround for resource-based data

This commit is contained in:
Alexander Nozik 2023-03-27 13:23:03 +03:00
parent 1f0d0a8124
commit f61bb5cc90
3 changed files with 60 additions and 10 deletions

View File

@ -3,6 +3,8 @@ package center.sciprog
import io.ktor.server.application.Application import io.ktor.server.application.Application
import io.ktor.server.application.call import io.ktor.server.application.call
import io.ktor.server.application.install import io.ktor.server.application.install
import io.ktor.server.application.log
import io.ktor.server.config.tryGetString
import io.ktor.server.plugins.forwardedheaders.ForwardedHeaders import io.ktor.server.plugins.forwardedheaders.ForwardedHeaders
import io.ktor.server.plugins.forwardedheaders.XForwardedHeaders import io.ktor.server.plugins.forwardedheaders.XForwardedHeaders
import io.ktor.server.response.respondRedirect import io.ktor.server.response.respondRedirect
@ -10,17 +12,55 @@ import io.ktor.server.routing.get
import io.ktor.server.routing.routing import io.ktor.server.routing.routing
import space.kscience.dataforge.context.Global import space.kscience.dataforge.context.Global
import space.kscience.dataforge.context.request import space.kscience.dataforge.context.request
import space.kscience.dataforge.data.DataSet
import space.kscience.dataforge.data.DataTree import space.kscience.dataforge.data.DataTree
import space.kscience.dataforge.data.node
import space.kscience.dataforge.data.populateFrom
import space.kscience.dataforge.misc.DFExperimental
import space.kscience.snark.html.SiteBuilder import space.kscience.snark.html.SiteBuilder
import space.kscience.snark.html.SnarkHtmlPlugin import space.kscience.snark.html.SnarkHtmlPlugin
import space.kscience.snark.html.readDirectory import space.kscience.snark.html.readDirectory
import space.kscience.snark.ktor.prepareSnarkDataCacheDirectory import space.kscience.snark.ktor.prepareSnarkDataCacheDirectory
import space.kscience.snark.ktor.site import space.kscience.snark.ktor.site
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.isRegularFile
import kotlin.io.path.relativeTo
import kotlin.io.path.toPath
private fun Application.copyResource(resource: String, dataDirectory: Path) {
val uri = javaClass.getResource("/$resource")?.toURI()
?: error("Resource $resource not found")
val targetPath = dataDirectory.resolve(resource)
if (Files.isDirectory(targetPath)) {
log.info("Using existing data directory at $targetPath.")
} else {
log.info("Copying data from $uri into $targetPath.")
targetPath.createDirectories()
//Copy everything into a temporary directory
fun copyFromPath(rootPath: Path) {
Files.walk(rootPath).forEach { source: Path ->
if (source.isRegularFile()) {
val relative = source.relativeTo(rootPath).toString()
val destination: Path = targetPath.resolve(relative)
destination.parent.createDirectories()
Files.copy(source, destination)
}
}
}
if ("jar" == uri.scheme) {
FileSystems.newFileSystem(uri, emptyMap<String, Any>()).use { fs ->
val rootPath: Path = fs.provider().getPath(uri)
copyFromPath(rootPath)
}
} else {
val rootPath = uri.toPath()
copyFromPath(rootPath)
}
}
}
@Suppress("unused") @Suppress("unused")
@ -29,12 +69,19 @@ fun Application.spcModule() {
install(ForwardedHeaders) install(ForwardedHeaders)
install(XForwardedHeaders) install(XForwardedHeaders)
val dataPath = Path.of("data")
prepareSnarkDataCacheDirectory(dataPath)
val snark = Global.request(SnarkHtmlPlugin) val snark = Global.request(SnarkHtmlPlugin)
val siteData = snark.readDirectory(dataPath)
val dataDirectory = Path.of(
environment.config.tryGetString("ktor.environment.dataDirectory") ?: "data"
)
if (!prepareSnarkDataCacheDirectory(dataDirectory)) {
copyResource("common", dataDirectory)
copyResource("home", dataDirectory)
copyResource("magprog", dataDirectory)
}
val siteData: DataTree<Any> = snark.readDirectory(dataDirectory)
site(snark, siteData, block = SiteBuilder::spcSite) site(snark, siteData, block = SiteBuilder::spcSite)

View File

@ -14,7 +14,6 @@ private fun <T : Any> DataSet<T>.siteData(branchName: String): DataTree<T> = Dat
} }
fun SiteBuilder.spcSite() { fun SiteBuilder.spcSite() {
// val commonData = data.branch("common")
spcHome(data.siteData("home")) spcHome(data.siteData("home"))
spcMasters(data.siteData("magprog")) spcMasters(data.siteData("magprog"))
// bmk(data.branch("bmk").withBranch("common", commonData)) // bmk(data.branch("bmk").withBranch("common", commonData))

View File

@ -9,4 +9,8 @@ ktor {
} }
development = true development = true
environment{
production = false
}
} }