13
13
// limitations under the License.
14
14
15
15
use std:: {
16
+ convert:: Infallible ,
17
+ future:: IntoFuture ,
16
18
io:: { self , Write } ,
17
19
ops:: Range ,
18
20
path:: { Path , PathBuf } ,
@@ -21,9 +23,9 @@ use std::{
21
23
} ;
22
24
23
25
use anyhow:: { anyhow, bail} ;
26
+ use axum:: { response:: IntoResponse , routing:: any_service} ;
24
27
use futures_util:: StreamExt ;
25
28
use http:: { Method , StatusCode } ;
26
- use hyper:: { server:: conn:: AddrIncoming , service:: service_fn, Body , Server } ;
27
29
use matrix_sdk:: {
28
30
config:: SyncSettings ,
29
31
oidc:: {
@@ -49,7 +51,7 @@ use matrix_sdk_ui::sync_service::SyncService;
49
51
use rand:: { distributions:: Alphanumeric , thread_rng, Rng } ;
50
52
use serde:: { Deserialize , Serialize } ;
51
53
use tokio:: { fs, io:: AsyncBufReadExt as _, net:: TcpListener , sync:: oneshot} ;
52
- use tower:: make :: Shared ;
54
+ use tower:: service_fn ;
53
55
use url:: Url ;
54
56
55
57
/// A command-line tool to demonstrate the steps requiring an interaction with
@@ -875,29 +877,31 @@ async fn spawn_local_server(
875
877
} ;
876
878
877
879
// 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
+ }
887
887
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 ( ) ;
891
892
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
+ }
894
895
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 ( ) ;
901
905
902
906
tokio:: spawn ( server) ;
903
907
0 commit comments