softshipper
softshipper

Reputation: 34071

Suspension functions can be called only within coroutine body in vertx

I have the following code that does not get compiled:

private suspend fun createRoutes(router: Router, auth: OAuth2Auth): Unit {

    val oauth2 = OAuth2AuthHandler.create(vertx, auth)
    val authz = KeycloakAuthorization.create()


    router.route().handler(LoggerHandler.create())

    router.route("/api/*").handler(oauth2)

    router.route("/api/greet").handler {

      println(RoleBasedAuthorization.create("ad-admins").match(it.user()))
      authz.getAuthorizations(it.user()).await()
    }

  }

the compiler complains:

Suspension functions can be called only within coroutine body

Without coroutine, I have to write in callback style:

  private fun createRoutes(router: Router, auth: OAuth2Auth): Unit {

    val oauth2 = OAuth2AuthHandler.create(vertx, auth)
    val authz = KeycloakAuthorization.create()


    router.route().handler(LoggerHandler.create())

    router.route("/api/*").handler(oauth2)

     router.route("/api/greet").handler {

      println(RoleBasedAuthorization.create("ad-admins").match(it.user()))

      authz.getAuthorizations(it.user())
        .onSuccess { _ ->

          println(RoleBasedAuthorization.create("ad-admins").match(it.user()))

          val res = it.response()
          res.putHeader("content-type", "text/plain")

          // Write to the response and end it
          res.end("I am interests path")

        }
    }
  }

However I would like to use coroutine instead callback style.

Update

That is the whole code:

class MainVerticle : CoroutineVerticle() {

  private suspend fun initConfig(): JsonObject {
    val yamlConfigOpts = ConfigStoreOptions()
      .setFormat("yaml")
      .setType("file")
      .setConfig(JsonObject().put("path", "config.yaml"))

    val configRetrieverOpts = ConfigRetrieverOptions()
      .addStore(yamlConfigOpts)

    val configRetriever = ConfigRetriever.create(vertx, configRetrieverOpts)

    return configRetriever.config.await()
  }


  private suspend fun createJwtAuth(): OAuth2Auth =

    KeycloakAuth.discover(
      vertx,
      OAuth2Options()
        .setFlow(OAuth2FlowType.AUTH_CODE)
        .setClientID("svc")
        .setClientSecret("9d782e45-67e7-44b1-9b74-864f45f9a18f")
        .setSite("https://oic.dev.databaker.io/auth/realms/databaker")
    ).await()


  private suspend fun createRoutes(router: Router, auth: OAuth2Auth): Unit {
    

    val oauth2 = OAuth2AuthHandler.create(vertx, auth)
    val authz = KeycloakAuthorization.create()


    router.route().handler(LoggerHandler.create())

    router.route("/api/*").handler(oauth2)

     router.route("/api/greet").handler {

      println(RoleBasedAuthorization.create("ad-admins").match(it.user()))

      authz.getAuthorizations(it.user()).await()
    }

  }


  private suspend fun server(router: Router): HttpServer {
    val server = vertx.createHttpServer()

    return server.requestHandler(router)
      .listen(8080)
      .onSuccess {
        println("HTTP server started on port ${it.actualPort()}")
      }
      .onFailure {
        println("Failed to start the server. Reason ${it.message}")
      }
      .await()
  }


  override suspend fun start() {

    val router = Router.router(vertx)


    createRoutes(router, createJwtAuth())
    server(router)

  }

}

The error that I get is on:

authz.getAuthorizations(it.user()).await()

Upvotes: 0

Views: 886

Answers (2)

Alexey Soshin
Alexey Soshin

Reputation: 17701

You shouldn't use GlobalScope in Vert.x code, since it will break the concept of structured concurrency.

To start a coroutine from CoroutineVerticle, you can simply use launch() function:

router.route("/api/greet").handler {
    launch {
        // Your code goes here
    }
}

You'll have to mark your function with the suspend keyword though.

Upvotes: 0

DanDayne
DanDayne

Reputation: 179

If you're open to writing some kind of wrapper in Kotlin, using coroutines is really easy. For starters, take a look at the documentation.

The coroutine can be as simple as GlobalScope.launch { createRoutes(router, auth) }

Upvotes: 2

Related Questions