Skip to content

Instantly share code, notes, and snippets.

@withoutclass
Created December 8, 2017 14:31
Show Gist options
  • Save withoutclass/6ba00200a3cc32ed181f9e359a1dfac8 to your computer and use it in GitHub Desktop.
Save withoutclass/6ba00200a3cc32ed181f9e359a1dfac8 to your computer and use it in GitHub Desktop.
Vertx Coroutine Server failing to hand back post params/body
class App : CoroutineVerticle() {
suspend override fun start() {
// Build Vert.x Web router
val router = Router.router(vertx)
router.post("/ingredient").coroutineHandler { ctx -> addIngredient(ctx) }
router.get("/").coroutineHandler { ctx -> ctx.response().end("Hello dude") }
// Start the server
awaitResult<HttpServer> {
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(config.getInteger("http.port", 8080), it)
}
}
private suspend fun addIngredient(ctx: RoutingContext) {
println(ctx.bodyAsString)
ctx.response().end("taco")
}
}
fun Route.coroutineHandler(fn: suspend (RoutingContext) -> Unit) {
handler { ctx ->
launch(ctx.vertx().dispatcher()) {
try {
fn(ctx)
} catch (e: Exception) {
ctx.fail(e)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment