Skip to content

Commit 0e0a406

Browse files
committed
Replace hyper with axum for oidc_cli example
Signed-off-by: Kévin Commaille <[email protected]>
1 parent 04e5643 commit 0e0a406

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/oidc_cli/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ test = false
1010

1111
[dependencies]
1212
anyhow = "1"
13+
axum = "0.7.4"
1314
dirs = "5.0.1"
1415
futures-util = { version = "0.3.21", default-features = false }
15-
http = { workspace = true }
16-
hyper = { version = "0.14.20", features = ["http1", "http2", "server"] }
16+
http = "1.1.0"
1717
matrix-sdk-ui = { path = "../../crates/matrix-sdk-ui" }
1818
rand = { workspace = true }
1919
serde = { workspace = true }

examples/oidc_cli/src/main.rs

+26-22
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// limitations under the License.
1414

1515
use std::{
16+
convert::Infallible,
17+
future::IntoFuture,
1618
io::{self, Write},
1719
ops::Range,
1820
path::{Path, PathBuf},
@@ -21,9 +23,9 @@ use std::{
2123
};
2224

2325
use anyhow::{anyhow, bail};
26+
use axum::{response::IntoResponse, routing::any_service};
2427
use futures_util::StreamExt;
2528
use http::{Method, StatusCode};
26-
use hyper::{server::conn::AddrIncoming, service::service_fn, Body, Server};
2729
use matrix_sdk::{
2830
config::SyncSettings,
2931
oidc::{
@@ -49,7 +51,7 @@ use matrix_sdk_ui::sync_service::SyncService;
4951
use rand::{distributions::Alphanumeric, thread_rng, Rng};
5052
use serde::{Deserialize, Serialize};
5153
use tokio::{fs, io::AsyncBufReadExt as _, net::TcpListener, sync::oneshot};
52-
use tower::make::Shared;
54+
use tower::service_fn;
5355
use url::Url;
5456

5557
/// A command-line tool to demonstrate the steps requiring an interaction with
@@ -875,29 +877,31 @@ async fn spawn_local_server(
875877
};
876878

877879
// Set up the server.
878-
let incoming = AddrIncoming::from_listener(listener)?;
879-
let server = Server::builder(incoming)
880-
.serve(Shared::new(service_fn(move |request| {
881-
let data_tx_mutex = data_tx_mutex.clone();
882-
async move {
883-
// Reject methods others than HEAD or GET.
884-
if request.method() != Method::HEAD && request.method() != Method::GET {
885-
return http::Response::builder().status(StatusCode::METHOD_NOT_ALLOWED).body(Body::default());
886-
}
880+
let router = any_service(service_fn(move |request: http::Request<_>| {
881+
let data_tx_mutex = data_tx_mutex.clone();
882+
async move {
883+
// Reject methods others than HEAD or GET.
884+
if request.method() != Method::HEAD && request.method() != Method::GET {
885+
return Ok::<_, Infallible>(StatusCode::METHOD_NOT_ALLOWED.into_response());
886+
}
887887

888-
// We only need to get the first response so we consume the transmitter the first time.
889-
if let Some(data_tx) = data_tx_mutex.lock().unwrap().take() {
890-
let query_string = request.uri().query().unwrap_or_default();
888+
// We only need to get the first response so we consume the transmitter the
889+
// first time.
890+
if let Some(data_tx) = data_tx_mutex.lock().unwrap().take() {
891+
let query_string = request.uri().query().unwrap_or_default();
891892

892-
data_tx.send(query_string.to_owned()).expect("The receiver is still alive");
893-
}
893+
data_tx.send(query_string.to_owned()).expect("The receiver is still alive");
894+
}
894895

895-
Ok(http::Response::new(Body::from("The authorization step is complete. You can close this page and go back to the oidc-cli.")))
896-
}
897-
})))
898-
.with_graceful_shutdown(async {
899-
signal_rx.await.ok();
900-
});
896+
Ok("The authorization step is complete. You can close this page and go back to the oidc-cli.".into_response())
897+
}
898+
}));
899+
900+
let server = axum::serve(listener, router)
901+
.with_graceful_shutdown(async {
902+
signal_rx.await.ok();
903+
})
904+
.into_future();
901905

902906
tokio::spawn(server);
903907

0 commit comments

Comments
 (0)