Olivier Patry
01/09/2024, 7:53 PMOlivier Patry
01/09/2024, 7:54 PMsuspend fun authorize(requestUserAuthorization: (url: String) -> Unit): String {
return callbackFlow {
val url = Url("... details omitted here ...")
val server = embeddedServer(Netty, port = url.port, host = url.host) {
routing {
get("... details omitted here ...") {
try {
// ... details omitted here ...
call.respond(HttpStatusCode.OK)
send("my result")
close(null)
} catch (e: Exception) {
call.respond(HttpStatusCode.BadRequest)
close(e)
}
}
}
}
server.start(wait = false)
// FIXME ensure server is started before
delay(150)
requestUserAuthorization("<https://third-party-service/oauth/authorize?callback_uri=$url>")
awaitClose(server::stop)
}.first()
}
Olivier Patry
01/09/2024, 7:55 PMserver.start(wait = true)
it won't go further.
If I call my callback to open the URL in a Web browser before the server creation & start, chances are my server isn't ready yet once the browser requests my callback URL.Olivier Patry
01/09/2024, 7:56 PMdelay
Aleksei Tirman [JB]
01/10/2024, 8:24 AMApplicationStarted
event to make sure the server is ready to serve requests. For more information please read the documentation.Olivier Patry
01/10/2024, 8:58 AMserver.environment.monitor.subscribe(ApplicationStarted) {
requestUserAuthorization("<https://third-party-service/oauth/authorize?callback_uri=$url>")
}
server.start(wait = false)