1
0
forked from SPC/spc-site

71 lines
2.1 KiB
Kotlin
Raw Normal View History

2022-05-03 13:33:43 +03:00
package ru.mipt.spc
2022-05-06 15:54:59 +03:00
import io.ktor.server.application.Application
import io.ktor.server.application.log
2022-05-03 13:33:43 +03:00
import kotlinx.css.CssBuilder
import kotlinx.html.CommonAttributeGroupFacade
import kotlinx.html.style
import space.kscience.dataforge.context.Context
import space.kscience.snark.SnarkPlugin
import java.net.URI
2022-05-06 15:54:59 +03:00
import java.nio.file.FileSystems
import java.nio.file.Files
import java.nio.file.Path
2022-05-03 17:15:07 +03:00
import kotlin.io.path.createDirectories
2022-05-13 17:21:06 +03:00
import kotlin.io.path.div
2022-05-03 17:15:07 +03:00
import kotlin.io.path.isRegularFile
import kotlin.io.path.relativeTo
2022-05-03 13:33:43 +03:00
fun CommonAttributeGroupFacade.css(block: CssBuilder.() -> Unit) {
style = CssBuilder().block().toString()
}
2022-05-06 15:54:59 +03:00
private fun Application.resolveData(uri: URI, targetPath: Path): Path {
if (Files.isDirectory(targetPath)) {
log.info("Using existing data directory at $targetPath.")
} else {
log.info("Copying data from $uri into $targetPath.")
targetPath.createDirectories()
2022-05-03 17:15:07 +03:00
//Copy everything into a temporary directory
2022-05-03 13:33:43 +03:00
FileSystems.newFileSystem(uri, emptyMap<String, Any>()).use { fs ->
2022-05-03 17:15:07 +03:00
val rootPath: Path = fs.provider().getPath(uri)
Files.walk(rootPath).forEach { source: Path ->
if (source.isRegularFile()) {
val relative = source.relativeTo(rootPath).toString()
2022-05-06 15:54:59 +03:00
val destination: Path = targetPath.resolve(relative)
2022-05-03 17:15:07 +03:00
destination.parent.createDirectories()
Files.copy(source, destination)
}
}
2022-05-03 13:33:43 +03:00
}
}
2022-05-06 15:54:59 +03:00
return targetPath
2022-05-03 13:33:43 +03:00
}
2022-05-13 17:21:06 +03:00
fun Application.spcModule() {
2022-05-03 13:33:43 +03:00
val context = Context("spc-site") {
plugin(SnarkPlugin)
}
2022-05-06 15:54:59 +03:00
val dataPath = Path.of("data")
2022-05-13 17:21:06 +03:00
val homeDataPath = resolveData(
javaClass.getResource("/home")!!.toURI(),
dataPath / "home"
)
2022-05-06 15:54:59 +03:00
2022-05-13 17:21:06 +03:00
spcHome(context, rootPath = homeDataPath)
2022-05-06 15:54:59 +03:00
2022-05-13 17:21:06 +03:00
val mastersDataPath = resolveData(
javaClass.getResource("/magprog")!!.toURI(),
dataPath / "magprog"
)
2022-05-03 13:33:43 +03:00
2022-05-13 17:21:06 +03:00
spcMaster(context, dataPath = mastersDataPath)
2022-05-03 13:33:43 +03:00
}
2022-05-13 17:21:06 +03:00
fun main(args: Array<String>) = io.ktor.server.netty.EngineMain.main(args)