asad.awadia
03/23/2019, 5:18 AMimport io.javalin.Javalin
fun main(args: Array<String>) {
val app = Javalin.create().start(7000)
app.get("/") { ctx -> ctx.result("Hello World") }
}
is it possible somehow to add coroutine support to this i.e. each request gets launched in its own coroutine and allows me to have controllers which are composed of suspending functions - maybe allow me to return a response on the context object from a launched coroutine?octylFractal
03/23/2019, 5:49 AMlaunch
on your app scope, but if not you'll have to use runBlocking
asad.awadia
03/23/2019, 5:54 AMfun main(args: Array<String>) = runBlocking {
val app = Javalin.create().start(8080)
val channel = Channel<Context>()
repeat(5) { launch(<http://Dispatchers.IO|Dispatchers.IO>) { workers(channel) } }
app.get("/") {
launch {
channel.send(it)
}
}
return@runBlocking
}
suspend fun workers(receiveChannel: ReceiveChannel<Context>) {
for (c in receiveChannel) {
c.result("foobar")
}
}
asad.awadia
03/23/2019, 5:55 AMoctylFractal
03/23/2019, 5:58 AMc.result
-- it probably doesn'toctylFractal
03/23/2019, 5:58 AMoshai
03/23/2019, 8:15 AMoshai
03/23/2019, 8:16 AM