如何在 router.route( "/api/*" ).handler 中使用协程?



我正试图在路由处理程序lambda中使用协程,如下所示:

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()
}
}

编译器在authz.getAuthorizations(it.user()).await()上抱怨Suspension functions can be called only within coroutine body。我做错了什么?

整个代码:

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)
}
}

提示:我正在使用Vertx 4.0.0 RC1

编译器,因为authz.getAuthorizations(it.user()).await()不是在挂起的函数中调用的:它是从Vert.x Web路由处理程序调用的。

您必须使用launch:包装您的调用

router.route("/api/greet").handler {
println(RoleBasedAuthorization.create("ad-admins").match(it.user()))
launch {
authz.getAuthorizations(it.user()).await()
}
}

如果该代码是在CoroutineVerticle中定义的,则协同程序将绑定到vertical上下文(以及在vertical的事件循环中调用的代码(。

最新更新