Skip to content

Instantly share code, notes, and snippets.

@wickedev
Last active March 23, 2021 04:10
Show Gist options
  • Save wickedev/88ef9365c7f648b2c2ddb8ffe94eb78a to your computer and use it in GitHub Desktop.
Save wickedev/88ef9365c7f648b2c2ddb8ffe94eb78a to your computer and use it in GitHub Desktop.
webflux reactor API vs webflux kotlin coroutine
// Before Coroutine
fun matches(exchange: ServerWebExchange): Mono<MatchResult> {
return Mono.just(exchange)
.map(ServerWebExchange::getRequest)
.map(ServerHttpRequest::getHeaders)
.filter { header -> header.containsKey(HttpHeaders.AUTHORIZATION)) }
.flatMap { MatchResult.match() }
.switchIfEmpty { MatchResult.notMatch() }
}
// After Coroutine
fun matches(exchange: ServerWebExchange): Mono<MatchResult> = mono {
val request = exchange.request
val header = request.headers
if (header.containsKey(HttpHeaders.AUTHORIZATION)) {
return@mono MatchResult.match().await()
}
return@mono MatchResult.notMatch().await()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment