Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Simple async HTTP server (for OAuth2)
I'm looking for a very simple async HTTP server. Something like tiny_http
, but async.
Context: I'm using OAuth2. That means I have to spawn an HTTP server, which should only wait for the very first connection, read out the URL query parameters, respond with "Authorization succesfull", and then close the server. Right now I do it with tiny_http
:
fn catch_redirect() -> anyhow::Result<RedirectArgs> {
// listen for request
let server = Server::http(SOCKET_ADDRESS).map_err(anyhow::Error::from_boxed)?;
let request = server.recv()?;
// extract params
let url = format!("http://localhost{}", request.url());
let args = extract_args(url);
// respond
let response = match &args {
Ok(_) => Response::from_string(
"Authorization successful! You can now close this tab and return to the app.",
),
Err(err) => Response::from_string(format!("Error: {}", err)),
};
request.respond(response)?;
args
}
As you can see, it really only responds to the first request and then closes the server. (In this case by dropping it.)
Now I want to build it pretty much exactly like this, but in async. That means, the only difference really should be that server.recv()
would be replaced by something like server.recv().await
. I can't just use tokio::task::spawn_blocking(...)
, because I need to be able to abort the task when the user cancels the OAuth2 process. That's the reason I need async.
But unfortunately, I can't find a good crate for that. The go-to async HTTP server seems to be hyper
, but it seems like it requires this Service
architecture. A Service
responds to all incoming requests. That would make it unnecessarily complicated to realize my use case: I would need something like a oneshot channel or so and then abort the server from "externally". Instead I really just want something like server.receive().await
, so that it doesn't even care about the second request.
Any ideas?
Thanks for your help!
1 post - 1 participant
🏷️ rust_feed