1
0
forked from SPC/spc-site

Path universalization + bugfixes

This commit is contained in:
Alexander Nozik 2022-05-03 17:15:07 +03:00
parent 431a710459
commit 44a2cf4cf3
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
5 changed files with 41 additions and 48 deletions

View File

@ -13,10 +13,10 @@ import ru.mipt.spc.magprog.magProgPage
import space.kscience.dataforge.context.Context import space.kscience.dataforge.context.Context
import space.kscience.snark.SnarkPlugin import space.kscience.snark.SnarkPlugin
import java.net.URI import java.net.URI
import java.nio.file.FileSystemNotFoundException import java.nio.file.*
import java.nio.file.FileSystems import kotlin.io.path.createDirectories
import java.nio.file.Path import kotlin.io.path.isRegularFile
import java.nio.file.Paths import kotlin.io.path.relativeTo
fun CommonAttributeGroupFacade.css(block: CssBuilder.() -> Unit) { fun CommonAttributeGroupFacade.css(block: CssBuilder.() -> Unit) {
style = CssBuilder().block().toString() style = CssBuilder().block().toString()
@ -29,28 +29,39 @@ private fun useResource(uri: URI, block: (Path) -> Unit) {
try { try {
block(Paths.get(uri)) block(Paths.get(uri))
} catch (ex: FileSystemNotFoundException) { } catch (ex: FileSystemNotFoundException) {
//Copy everything into a temporary directory
FileSystems.newFileSystem(uri, emptyMap<String, Any>()).use { fs -> FileSystems.newFileSystem(uri, emptyMap<String, Any>()).use { fs ->
val p: Path = fs.provider().getPath(uri) val rootPath: Path = fs.provider().getPath(uri)
block(p) val tmpDirectory = Files.createTempDirectory("snark")
Files.walk(rootPath).forEach { source: Path ->
if (source.isRegularFile()) {
val relative = source.relativeTo(rootPath).toString()
val destination: Path = tmpDirectory.resolve(relative)
destination.parent.createDirectories()
Files.copy(source, destination)
}
}
block(tmpDirectory)
} }
} }
} }
fun main() { fun main() {
val context = Context("spc-site") { val context = Context("spc-site") {
plugin(SnarkPlugin) plugin(SnarkPlugin)
} }
embeddedServer(Netty, port = 8080, watchPaths = listOf("classes", "resources")) { embeddedServer(Netty, port = 7080, watchPaths = listOf("classes", "resources")) {
useResource(javaClass.getResource("/magprog")!!.toURI()) { install(StatusPages) {
install(StatusPages) { exception<AuthenticationException> { call, _ ->
exception<AuthenticationException> { call, _ -> call.respond(HttpStatusCode.Unauthorized)
call.respond(HttpStatusCode.Unauthorized)
}
exception<AuthorizationException> { call, _ ->
call.respond(HttpStatusCode.Forbidden)
}
} }
magProgPage(context, rootPath = it) exception<AuthorizationException> { call, _ ->
call.respond(HttpStatusCode.Forbidden)
}
}
useResource(javaClass.getResource("/magprog")!!.toURI()) { path ->
magProgPage(context, rootPath = path)
} }
}.start(wait = true) }.start(wait = true)

View File

@ -1,5 +1,6 @@
package ru.mipt.spc.magprog package ru.mipt.spc.magprog
import io.ktor.server.application.Application
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.html.div import kotlinx.html.div
import kotlinx.html.unsafe import kotlinx.html.unsafe
@ -25,6 +26,7 @@ import kotlin.reflect.full.isSubtypeOf
import kotlin.reflect.typeOf import kotlin.reflect.typeOf
class DataSetPageContext( class DataSetPageContext(
val application: Application,
override val context: Context, override val context: Context,
val prefix: String, val prefix: String,
val dataSet: DataSet<Any>, val dataSet: DataSet<Any>,

View File

@ -25,12 +25,8 @@ import space.kscience.snark.*
import java.nio.file.Path import java.nio.file.Path
import kotlin.collections.component1 import kotlin.collections.component1
import kotlin.collections.component2 import kotlin.collections.component2
import kotlin.collections.forEach
import kotlin.collections.listOf
import kotlin.collections.map
import kotlin.collections.mapValues
import kotlin.collections.set import kotlin.collections.set
import kotlin.collections.sortedBy
//fun CssBuilder.magProgCss() { //fun CssBuilder.magProgCss() {
// rule(".magprog-body") { // rule(".magprog-body") {
@ -144,12 +140,14 @@ context(PageContext) private fun FlowContent.team() {
team.forEach { member -> team.forEach { member ->
section { section {
id = member.id id = member.id
div("image left") { a(classes = "image", href = resolveRef("mentor-${member.id}")) {
member.photo?.let { photoPath -> member.photo?.let { photoPath ->
img( img(
src = resolveRef(photoPath), src = resolveRef(photoPath),
alt = member.name alt = member.name
) ) {
attributes["data-position"] = "center center"
}
} }
} }
@ -176,13 +174,13 @@ context(PageContext) private fun FlowContent.mentors() {
mentors.forEach { (name, mentor) -> mentors.forEach { (name, mentor) ->
section { section {
id = mentor.id id = mentor.id
div("image left") { a(classes = "image", href = resolveRef("mentor-${mentor.id}")) {
mentor.photo?.let { photoPath -> mentor.photo?.let { photoPath ->
a(href = resolveRef("mentor-${mentor.id}")) { img(
img( src = resolveRef(photoPath),
src = resolveRef(photoPath), alt = mentor.name
alt = mentor.name ) {
) attributes["data-position"] = "center center"
} }
} }
} }
@ -260,9 +258,6 @@ context(PageContext) internal fun BODY.magProgFooter() {
script { script {
src = resolveRef("js/util.js") src = resolveRef("js/util.js")
} }
script {
src = resolveRef("js/bootstrap.min.js")
}
script { script {
src = resolveRef("js/main.js") src = resolveRef("js/main.js")
} }
@ -270,13 +265,12 @@ context(PageContext) internal fun BODY.magProgFooter() {
internal val Person.mentorPageId get() = "mentor-${id}" internal val Person.mentorPageId get() = "mentor-${id}"
internal fun Application.magProgPage(context: Context, rootPath: Path, prefix: String = "/magprog") { internal fun Application.magProgPage(context: Context, rootPath: Path, prefix: String = "/magprog") {
val io = context.io val io = context.io
val content = DirectoryDataTree(io, rootPath.resolve("content")) val content = DirectoryDataTree(io, rootPath.resolve("content"))
val magprogPageContext: PageContext = DataSetPageContext(context, prefix, content) val magprogPageContext: PageContext = DataSetPageContext(this, context, prefix, content)
routing { routing {
route(prefix) { route(prefix) {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long