[WIP] refactor in progress
This commit is contained in:
parent
f65b537255
commit
72ac07db5a
@ -3,7 +3,6 @@ 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.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
|
||||||
@ -12,53 +11,67 @@ import io.ktor.server.routing.get
|
|||||||
import io.ktor.server.routing.routing
|
import io.ktor.server.routing.routing
|
||||||
import space.kscience.dataforge.context.Context
|
import space.kscience.dataforge.context.Context
|
||||||
import space.kscience.dataforge.context.request
|
import space.kscience.dataforge.context.request
|
||||||
import space.kscience.dataforge.data.DataTree
|
import space.kscience.dataforge.data.DataSourceBuilder
|
||||||
import space.kscience.dataforge.workspace.readDataDirectory
|
import space.kscience.dataforge.io.IOPlugin
|
||||||
import space.kscience.snark.html.*
|
import space.kscience.dataforge.misc.DFExperimental
|
||||||
import space.kscience.snark.ktor.prepareSnarkDataCacheDirectory
|
import space.kscience.dataforge.names.Name
|
||||||
|
import space.kscience.dataforge.workspace.monitorRawDirectory
|
||||||
|
import space.kscience.dataforge.workspace.readResources
|
||||||
|
import space.kscience.snark.html.SnarkHtml
|
||||||
|
import space.kscience.snark.html.fill
|
||||||
|
import space.kscience.snark.html.readSiteData
|
||||||
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) {
|
//private fun Application.copyResource(resource: String, dataDirectory: Path) {
|
||||||
val uri = javaClass.getResource("/$resource")?.toURI()
|
// val uri = javaClass.getResource("/$resource")?.toURI()
|
||||||
?: error("Resource $resource not found")
|
// ?: error("Resource $resource not found")
|
||||||
val targetPath = dataDirectory.resolve(resource)
|
// 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)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
if (Files.isDirectory(targetPath)) {
|
context(IOPlugin)
|
||||||
log.info("Using existing data directory at $targetPath.")
|
@OptIn(DFExperimental::class)
|
||||||
} else {
|
fun DataSourceBuilder<Any>.directory(path: Path, name: Name = Name.EMPTY) {
|
||||||
log.info("Copying data from $uri into $targetPath.")
|
node(name, monitorRawDirectory(path))
|
||||||
targetPath.createDirectories()
|
}
|
||||||
//Copy everything into a temporary directory
|
|
||||||
|
|
||||||
fun copyFromPath(rootPath: Path) {
|
context(IOPlugin)
|
||||||
Files.walk(rootPath).forEach { source: Path ->
|
@OptIn(DFExperimental::class)
|
||||||
if (source.isRegularFile()) {
|
fun DataSourceBuilder<Any>.resource(
|
||||||
val relative = source.relativeTo(rootPath).toString()
|
resource: String,
|
||||||
val destination: Path = targetPath.resolve(relative)
|
classLoader: ClassLoader = Thread.currentThread().contextClassLoader,
|
||||||
destination.parent.createDirectories()
|
) {
|
||||||
Files.copy(source, destination)
|
fill(readResources(resource, classLoader = classLoader))
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -78,14 +91,13 @@ fun Application.spcModule() {
|
|||||||
environment.config.tryGetString("ktor.environment.dataDirectory") ?: "data"
|
environment.config.tryGetString("ktor.environment.dataDirectory") ?: "data"
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!prepareSnarkDataCacheDirectory(dataDirectory)) {
|
val siteData = snark.readSiteData(context) {
|
||||||
copyResource("common", dataDirectory)
|
resource("common")
|
||||||
copyResource("home", dataDirectory)
|
resource("home")
|
||||||
copyResource("magprog", dataDirectory)
|
resource("magprog")
|
||||||
|
//directory(dataDirectory)
|
||||||
}
|
}
|
||||||
|
|
||||||
val siteData: DataTree<Any> = snark.io.readDataDirectory(dataDirectory)
|
|
||||||
|
|
||||||
routing {
|
routing {
|
||||||
get("magprog") {
|
get("magprog") {
|
||||||
call.respondRedirect("education/masters")
|
call.respondRedirect("education/masters")
|
||||||
|
@ -3,19 +3,25 @@ package center.sciprog
|
|||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.coroutineScope
|
||||||
import space.kscience.dataforge.context.Context
|
import space.kscience.dataforge.context.Context
|
||||||
import space.kscience.dataforge.context.request
|
import space.kscience.dataforge.context.request
|
||||||
import space.kscience.snark.html.*
|
import space.kscience.snark.html.SnarkHtml
|
||||||
|
import space.kscience.snark.html.readSiteData
|
||||||
import space.kscience.snark.html.static.staticSite
|
import space.kscience.snark.html.static.staticSite
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
|
|
||||||
suspend fun main(args: Array<String>) = coroutineScope{
|
suspend fun main(args: Array<String>) = coroutineScope {
|
||||||
val context = Context{
|
val context = Context {
|
||||||
plugin(SnarkHtml)
|
plugin(SnarkHtml)
|
||||||
}
|
}
|
||||||
|
|
||||||
val destinationPath = args.firstOrNull() ?: "build/public"
|
val destinationPath = args.firstOrNull() ?: "build/public"
|
||||||
|
|
||||||
val snark = context.request(SnarkHtml)
|
val snark = context.request(SnarkHtml)
|
||||||
val siteData = snark.readResources("common", "home", "magprog")
|
val siteData = snark.readSiteData(context) {
|
||||||
|
resource("common")
|
||||||
|
resource("home")
|
||||||
|
resource("magprog")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
snark.staticSite(siteData, Path.of(destinationPath), content = spcSite)
|
snark.staticSite(siteData, Path.of(destinationPath), content = spcSite)
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user