Skip to content

Commit

Permalink
Serve skiko on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgonmic committed May 1, 2024
1 parent 6abd1a1 commit b6dffc2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
17 changes: 17 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.gradle.kotlin.dsl.support.serviceOf
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
Expand Down Expand Up @@ -53,6 +54,11 @@ allprojects {
}
}

val resourceDependency: Configuration by configurations.creating {
isCanBeResolved = true
isCanBeConsumed = false
}

dependencies {
annotationProcessor("org.springframework:spring-context-indexer")
implementation("com.google.code.gson:gson")
Expand All @@ -79,6 +85,8 @@ dependencies {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")

resourceDependency("org.jetbrains.skiko:skiko-js-wasm-runtime:0.7.90")
}

fun buildPropertyFile() {
Expand Down Expand Up @@ -156,6 +164,15 @@ val buildLambda by tasks.creating(Zip::class) {
}
}

tasks.named<Copy>("processResources") {
val archiveOperation = project.serviceOf<ArchiveOperations>()
from(resourceDependency.map {
archiveOperation.zipTree(it)
}) {
into("com/compiler/server")
}
}

tasks.withType<Test> {
dependsOn(rootProject.the<NodeJsRootExtension>().nodeJsSetupTaskProvider)
useJUnitPlatform()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.compiler.server.controllers

import org.springframework.core.io.FileSystemResource
import org.springframework.core.io.Resource
import org.springframework.http.*
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.util.concurrent.TimeUnit


@RestController
@RequestMapping(value = ["/api/resource", "/api/**/resource"])
class ResourceRestController {
@GetMapping("/skiko.mjs")
fun getSkikoMjs(): ResponseEntity<Resource> {
return cacheableResource("/com/compiler/server/skiko.mjs", MediaType("text", "javascript"))
}

@GetMapping("/skiko.wasm")
fun getSkikoWasm(): ResponseEntity<Resource> {
return cacheableResource("/com/compiler/server/skiko.wasm", MediaType("application", "wasm"))
}

private fun cacheableResource(path: String, mediaType: MediaType): ResponseEntity<Resource> {
val resourcePath = javaClass.getResource(path)?.path
?: return ResponseEntity.internalServerError().build()

val resource = FileSystemResource(resourcePath)
val headers = HttpHeaders().apply {
contentType = mediaType
cacheControl = CacheControl.maxAge(365, TimeUnit.DAYS).headerValue
}

return ResponseEntity(resource, headers, HttpStatus.OK)
}
}

0 comments on commit b6dffc2

Please sign in to comment.