Idiomatic Kotlin Code / Vert.x


#1

I have the following code:

put("/:database").coroutineHandler {
    Auth(keycloak, "realm:create").handle(it)
    it.next()
}.handler(BodyHandler.create()).coroutineHandler {
    Create(location, false).handle(it)
}

basically chaining 3 handlers, with the extension function coroutineHandler being defined as:

/**
 * An extension method for simplifying coroutines usage with Vert.x Web routers.
 */
private fun Route.coroutineHandler(fn: suspend (RoutingContext) -> Unit): Route {
    return handler { ctx ->
        launch(ctx.vertx().dispatcher()) {
            try {
                fn(ctx)
            } catch (e: Exception) {
                ctx.fail(e)
            }
        }
    }
}

To me, this looks a bit ugly, but I’m new to Kotlin and Vert.x. Maybe a real CoroutineHandler defined as a suspending function would be nice, as normal Handlers are defined as follows (in Java):

@FunctionalInterface
public interface Handler<E> {
  /**
   * Something has happened, so handle it.
   *
   * @param event  the event to handle
   */
  void handle(E event);
}

The whole idea behind this chaining was, that first I want to do authorization checks, then add a BodyHandler for requests of any size, to PUT large files to a storage system (I hope that authorized users won’t kill their own storage-server) and then do the business logic stuff (for instance shredding the file into the internal binary representation of the storage system).

kind regards
Johannes