-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
473 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2024 Robert Jaros | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package dev.kilua | ||
|
||
import dev.kilua.utils.nativeListOf | ||
|
||
/** | ||
* Kilua CSS register. | ||
*/ | ||
public object CssRegister { | ||
|
||
public val cssFiles: MutableList<String> = nativeListOf() | ||
|
||
/** | ||
* Register CSS file used by the Kilua Modules. | ||
*/ | ||
public fun register(cssFile: String) { | ||
cssFiles.add(cssFile) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
plugins { | ||
kotlin("multiplatform") | ||
alias(libs.plugins.kotlinx.serialization) | ||
alias(libs.plugins.detekt) | ||
alias(libs.plugins.dokka) | ||
alias(libs.plugins.nmcp) | ||
id("maven-publish") | ||
id("signing") | ||
} | ||
|
||
detekt { | ||
toolVersion = libs.versions.detekt.get() | ||
config.setFrom("../../detekt-config.yml") | ||
buildUponDefaultConfig = true | ||
} | ||
|
||
kotlin { | ||
explicitApi() | ||
compilerOptions() | ||
kotlinJvmTargets() | ||
sourceSets { | ||
val jvmMain by getting { | ||
dependencies { | ||
implementation(project(":modules:kilua-ssr-server")) | ||
api(libs.ktor.server.core) | ||
} | ||
} | ||
} | ||
} | ||
|
||
tasks.register<Jar>("javadocJar") { | ||
dependsOn(tasks.dokkaHtml) | ||
from(tasks.dokkaHtml.flatMap { it.outputDirectory }) | ||
archiveClassifier.set("javadoc") | ||
} | ||
|
||
setupPublishing() | ||
|
||
nmcp { | ||
publishAllPublications {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
delete config.webpack.optimization; |
76 changes: 76 additions & 0 deletions
76
modules/kilua-ssr-server-ktor/src/jvmMain/kotlin/dev/kilua/ssr/InitSsr.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (c) 2024 Robert Jaros | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package dev.kilua.ssr | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.http.content.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import io.ktor.util.* | ||
import io.ktor.util.pipeline.* | ||
import java.util.* | ||
|
||
internal val ssrEngineKey: AttributeKey<SsrEngine> = AttributeKey("ssrEngine") | ||
|
||
/** | ||
* Initialization function for Kilua Server-Side Rendering. | ||
*/ | ||
public fun Application.initSsr() { | ||
val nodeExecutable = environment.config.propertyOrNull("ssr.nodeExecutable")?.getString() | ||
val port = environment.config.propertyOrNull("ssr.port")?.getString()?.toIntOrNull() | ||
val externalSsrService = environment.config.propertyOrNull("ssr.externalSsrService")?.getString() | ||
val rpcUrlPrefix = environment.config.propertyOrNull("ssr.rpcUrlPrefix")?.getString() | ||
val rootId = environment.config.propertyOrNull("ssr.rootId")?.getString() ?: "root" | ||
val ssrEngine = SsrEngine(nodeExecutable, port, externalSsrService, rpcUrlPrefix, rootId) | ||
attributes.put(ssrEngineKey, ssrEngine) | ||
routing { | ||
get("/index.html") { | ||
respondSsr() | ||
} | ||
singlePageApplication { | ||
defaultPage = UUID.randomUUID().toString() // Non-existing resource | ||
filesPath = "/assets" | ||
useResources = true | ||
} | ||
route("/") { | ||
route("{static-content-path-parameter...}") {// Important name from Ktor sources! | ||
get { | ||
respondSsr(call.request.uri) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun PipelineContext<Unit, ApplicationCall>.respondSsr(uri: String = "/") { | ||
if (uri == "/favicon.ico") { | ||
call.respond(HttpStatusCode.NotFound) | ||
} else { | ||
val ssrEngine = call.application.attributes[ssrEngineKey] | ||
call.respondText(ContentType.Text.Html, HttpStatusCode.OK) { | ||
ssrEngine.getSsrContent(uri) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
plugins { | ||
kotlin("multiplatform") | ||
alias(libs.plugins.kotlinx.serialization) | ||
alias(libs.plugins.detekt) | ||
alias(libs.plugins.dokka) | ||
alias(libs.plugins.nmcp) | ||
id("maven-publish") | ||
id("signing") | ||
} | ||
|
||
detekt { | ||
toolVersion = libs.versions.detekt.get() | ||
config.setFrom("../../detekt-config.yml") | ||
buildUponDefaultConfig = true | ||
} | ||
|
||
kotlin { | ||
explicitApi() | ||
compilerOptions() | ||
kotlinJvmTargets() | ||
sourceSets { | ||
val jvmMain by getting { | ||
dependencies { | ||
api(libs.ktor.client.core) | ||
api(libs.ktor.client.apache) | ||
api(libs.logback.classic) | ||
} | ||
} | ||
} | ||
} | ||
|
||
tasks.register<Jar>("javadocJar") { | ||
dependsOn(tasks.dokkaHtml) | ||
from(tasks.dokkaHtml.flatMap { it.outputDirectory }) | ||
archiveClassifier.set("javadoc") | ||
} | ||
|
||
setupPublishing() | ||
|
||
nmcp { | ||
publishAllPublications {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
delete config.webpack.optimization; |
80 changes: 80 additions & 0 deletions
80
modules/kilua-ssr-server/src/jvmMain/kotlin/dev/kilua/ssr/FileUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2024 Robert Jaros | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package dev.kilua.ssr | ||
|
||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.util.zip.ZipEntry | ||
import java.util.zip.ZipInputStream | ||
|
||
/** | ||
* Converted from https://github.com/eugenp/tutorials/blob/master/core-java-modules/core-java-io/src/main/java/com/baeldung/unzip/UnzipFile.java | ||
*/ | ||
internal object FileUtils { | ||
@Throws(IOException::class) | ||
@JvmStatic | ||
fun unzip(inputStream: InputStream, destinationDir: File) { | ||
val buffer = ByteArray(1024) | ||
val zis = ZipInputStream(inputStream) | ||
var zipEntry = zis.nextEntry | ||
while (zipEntry != null) { | ||
val newFile = newFile(destinationDir, zipEntry) | ||
if (zipEntry.isDirectory) { | ||
if (!newFile.isDirectory && !newFile.mkdirs()) { | ||
throw IOException("Failed to create directory $newFile") | ||
} | ||
} else { | ||
val parent = newFile.parentFile | ||
if (!parent.isDirectory && !parent.mkdirs()) { | ||
throw IOException("Failed to create directory $parent") | ||
} | ||
|
||
val fos = FileOutputStream(newFile) | ||
var len: Int | ||
while ((zis.read(buffer).also { len = it }) > 0) { | ||
fos.write(buffer, 0, len) | ||
} | ||
fos.close() | ||
} | ||
zipEntry = zis.nextEntry | ||
} | ||
zis.closeEntry() | ||
zis.close() | ||
} | ||
|
||
@Throws(IOException::class) | ||
private fun newFile(destinationDir: File, zipEntry: ZipEntry): File { | ||
val destFile = File(destinationDir, zipEntry.name) | ||
|
||
val destDirPath = destinationDir.canonicalPath | ||
val destFilePath = destFile.canonicalPath | ||
|
||
if (!destFilePath.startsWith(destDirPath + File.separator)) { | ||
throw IOException("Entry is outside of the target dir: " + zipEntry.name) | ||
} | ||
|
||
return destFile | ||
} | ||
} |
Oops, something went wrong.