Created
December 8, 2017 14:31
-
-
Save withoutclass/6ba00200a3cc32ed181f9e359a1dfac8 to your computer and use it in GitHub Desktop.
Vertx Coroutine Server failing to hand back post params/body
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
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