1
0
forked from SPC/spc-site

91 lines
2.9 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-24 21:39:01 +03:00
import java.time.LocalDateTime
import kotlin.io.path.*
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-24 21:39:01 +03:00
const val DEPLOY_DATE_FILE = "deployDate"
const val BUILD_DATE_FILE = "buildDate"
2022-05-14 11:08:28 +03:00
@Suppress("unused")
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-24 21:39:01 +03:00
// Clear data directory if it is outdated
val deployDate = dataPath.resolve(DEPLOY_DATE_FILE).takeIf { it.exists() }
?.readText()?.let { LocalDateTime.parse(it) }
val buildDate = javaClass.getResource(BUILD_DATE_FILE)?.readText()?.let { LocalDateTime.parse(it) }
if (deployDate != null && buildDate != null && buildDate.isAfter(deployDate)) {
log.info("Outdated data. Resetting data directory.")
dataPath.deleteIfExists()
//Writing deploy date file
dataPath.createDirectories()
dataPath.resolve(DEPLOY_DATE_FILE).writeText(LocalDateTime.now().toString())
2022-05-25 12:59:39 +03:00
} else if (deployDate == null && buildDate != null) {
//Writing deploy date in production mode
dataPath.resolve(DEPLOY_DATE_FILE).writeText(LocalDateTime.now().toString())
2022-05-24 21:39:01 +03:00
}
2022-05-25 12:59:39 +03:00
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)