[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.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
|
||||
@ -12,53 +11,67 @@ import io.ktor.server.routing.get
|
||||
import io.ktor.server.routing.routing
|
||||
import space.kscience.dataforge.context.Context
|
||||
import space.kscience.dataforge.context.request
|
||||
import space.kscience.dataforge.data.DataTree
|
||||
import space.kscience.dataforge.workspace.readDataDirectory
|
||||
import space.kscience.snark.html.*
|
||||
import space.kscience.snark.ktor.prepareSnarkDataCacheDirectory
|
||||
import space.kscience.dataforge.data.DataSourceBuilder
|
||||
import space.kscience.dataforge.io.IOPlugin
|
||||
import space.kscience.dataforge.misc.DFExperimental
|
||||
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 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)
|
||||
//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)
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
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
|
||||
context(IOPlugin)
|
||||
@OptIn(DFExperimental::class)
|
||||
fun DataSourceBuilder<Any>.directory(path: Path, name: Name = Name.EMPTY) {
|
||||
node(name, monitorRawDirectory(path))
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
context(IOPlugin)
|
||||
@OptIn(DFExperimental::class)
|
||||
fun DataSourceBuilder<Any>.resource(
|
||||
resource: String,
|
||||
classLoader: ClassLoader = Thread.currentThread().contextClassLoader,
|
||||
) {
|
||||
fill(readResources(resource, classLoader = classLoader))
|
||||
}
|
||||
|
||||
|
||||
@ -78,14 +91,13 @@ fun Application.spcModule() {
|
||||
environment.config.tryGetString("ktor.environment.dataDirectory") ?: "data"
|
||||
)
|
||||
|
||||
if (!prepareSnarkDataCacheDirectory(dataDirectory)) {
|
||||
copyResource("common", dataDirectory)
|
||||
copyResource("home", dataDirectory)
|
||||
copyResource("magprog", dataDirectory)
|
||||
val siteData = snark.readSiteData(context) {
|
||||
resource("common")
|
||||
resource("home")
|
||||
resource("magprog")
|
||||
//directory(dataDirectory)
|
||||
}
|
||||
|
||||
val siteData: DataTree<Any> = snark.io.readDataDirectory(dataDirectory)
|
||||
|
||||
routing {
|
||||
get("magprog") {
|
||||
call.respondRedirect("education/masters")
|
||||
|
@ -3,19 +3,25 @@ package center.sciprog
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import space.kscience.dataforge.context.Context
|
||||
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 java.nio.file.Path
|
||||
|
||||
suspend fun main(args: Array<String>) = coroutineScope{
|
||||
val context = Context{
|
||||
suspend fun main(args: Array<String>) = coroutineScope {
|
||||
val context = Context {
|
||||
plugin(SnarkHtml)
|
||||
}
|
||||
|
||||
val destinationPath = args.firstOrNull() ?: "build/public"
|
||||
|
||||
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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user