From f61bb5cc900c338626018a73d890db91474790f4 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 27 Mar 2023 13:23:03 +0300 Subject: [PATCH] Temporary workaround for resource-based data --- src/main/kotlin/center/sciprog/Application.kt | 65 ++++++++++++++++--- src/main/kotlin/center/sciprog/spcSite.kt | 1 - src/main/resources/application.conf | 4 ++ 3 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/center/sciprog/Application.kt b/src/main/kotlin/center/sciprog/Application.kt index 9d66819..afa6952 100644 --- a/src/main/kotlin/center/sciprog/Application.kt +++ b/src/main/kotlin/center/sciprog/Application.kt @@ -3,6 +3,8 @@ package center.sciprog import io.ktor.server.application.Application import io.ktor.server.application.call 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.XForwardedHeaders import io.ktor.server.response.respondRedirect @@ -10,17 +12,55 @@ import io.ktor.server.routing.get import io.ktor.server.routing.routing import space.kscience.dataforge.context.Global import space.kscience.dataforge.context.request -import space.kscience.dataforge.data.DataSet 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.SnarkHtmlPlugin import space.kscience.snark.html.readDirectory import space.kscience.snark.ktor.prepareSnarkDataCacheDirectory import space.kscience.snark.ktor.site +import java.nio.file.FileSystems +import java.nio.file.Files 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()).use { fs -> + val rootPath: Path = fs.provider().getPath(uri) + copyFromPath(rootPath) + } + } else { + val rootPath = uri.toPath() + copyFromPath(rootPath) + } + } +} @Suppress("unused") @@ -29,12 +69,19 @@ fun Application.spcModule() { install(ForwardedHeaders) install(XForwardedHeaders) - val dataPath = Path.of("data") - - prepareSnarkDataCacheDirectory(dataPath) - 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 = snark.readDirectory(dataDirectory) site(snark, siteData, block = SiteBuilder::spcSite) diff --git a/src/main/kotlin/center/sciprog/spcSite.kt b/src/main/kotlin/center/sciprog/spcSite.kt index 18e0632..a3ffba5 100644 --- a/src/main/kotlin/center/sciprog/spcSite.kt +++ b/src/main/kotlin/center/sciprog/spcSite.kt @@ -14,7 +14,6 @@ private fun DataSet.siteData(branchName: String): DataTree = Dat } fun SiteBuilder.spcSite() { -// val commonData = data.branch("common") spcHome(data.siteData("home")) spcMasters(data.siteData("magprog")) // bmk(data.branch("bmk").withBranch("common", commonData)) diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf index 1933922..612a5b6 100644 --- a/src/main/resources/application.conf +++ b/src/main/resources/application.conf @@ -9,4 +9,8 @@ ktor { } development = true + + environment{ + production = false + } } \ No newline at end of file