Skip to content

Commit

Permalink
Replace hyper with axum for oidc_cli example
Browse files Browse the repository at this point in the history
Signed-off-by: Kévin Commaille <[email protected]>
  • Loading branch information
zecakeh committed Apr 27, 2024
1 parent 04e5643 commit 0e0a406
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/oidc_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ test = false

[dependencies]
anyhow = "1"
axum = "0.7.4"
dirs = "5.0.1"
futures-util = { version = "0.3.21", default-features = false }
http = { workspace = true }
hyper = { version = "0.14.20", features = ["http1", "http2", "server"] }
http = "1.1.0"
matrix-sdk-ui = { path = "../../crates/matrix-sdk-ui" }
rand = { workspace = true }
serde = { workspace = true }
Expand Down
48 changes: 26 additions & 22 deletions examples/oidc_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.

use std::{
convert::Infallible,
future::IntoFuture,
io::{self, Write},
ops::Range,
path::{Path, PathBuf},
Expand All @@ -21,9 +23,9 @@ use std::{
};

use anyhow::{anyhow, bail};
use axum::{response::IntoResponse, routing::any_service};
use futures_util::StreamExt;
use http::{Method, StatusCode};
use hyper::{server::conn::AddrIncoming, service::service_fn, Body, Server};
use matrix_sdk::{
config::SyncSettings,
oidc::{
Expand All @@ -49,7 +51,7 @@ use matrix_sdk_ui::sync_service::SyncService;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use serde::{Deserialize, Serialize};
use tokio::{fs, io::AsyncBufReadExt as _, net::TcpListener, sync::oneshot};
use tower::make::Shared;
use tower::service_fn;
use url::Url;

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

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

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

data_tx.send(query_string.to_owned()).expect("The receiver is still alive");
}
data_tx.send(query_string.to_owned()).expect("The receiver is still alive");
}

Ok(http::Response::new(Body::from("The authorization step is complete. You can close this page and go back to the oidc-cli.")))
}
})))
.with_graceful_shutdown(async {
signal_rx.await.ok();
});
Ok("The authorization step is complete. You can close this page and go back to the oidc-cli.".into_response())
}
}));

let server = axum::serve(listener, router)
.with_graceful_shutdown(async {
signal_rx.await.ok();
})
.into_future();

tokio::spawn(server);

Expand Down

0 comments on commit 0e0a406

Please sign in to comment.