Last active
November 29, 2017 14:56
-
-
Save bnorm/31f8e7822562a7185661cb8022ff8012 to your computer and use it in GitHub Desktop.
Kotlin Script Engine
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
org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory |
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
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