Skip to content

Instantly share code, notes, and snippets.

@bnorm
Last active November 29, 2017 14:56
Show Gist options
  • Save bnorm/31f8e7822562a7185661cb8022ff8012 to your computer and use it in GitHub Desktop.
Save bnorm/31f8e7822562a7185661cb8022ff8012 to your computer and use it in GitHub Desktop.
Kotlin Script Engine
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory
import org.jetbrains.ktor.application.Application
import org.jetbrains.ktor.application.install
import org.jetbrains.ktor.features.CallLogging
import org.jetbrains.ktor.features.DefaultHeaders
import org.jetbrains.ktor.http.ContentType
import org.jetbrains.ktor.request.receiveText
import org.jetbrains.ktor.response.respondText
import org.jetbrains.ktor.routing.post
import org.jetbrains.ktor.routing.routing
import javax.script.ScriptEngineManager
fun Application.main() {
val semgr = ScriptEngineManager()
install(DefaultHeaders)
install(CallLogging)
routing {
post("/start") {
val engine = semgr.getEngineByExtension("kts")
val script = call.receiveText()
val result = engine.eval(script)?.toString()
call.respondText(result.orEmpty(), ContentType.Text.Plain)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment