1
- use std:: { error :: Error as StdError , future:: Future , sync:: Arc } ;
1
+ use std:: { fmt :: Debug , future:: Future , sync:: Arc } ;
2
2
3
3
use anyhow:: Result ;
4
- use axum:: body:: HttpBody ;
5
- use hyper:: {
6
- header:: CONTENT_TYPE ,
7
- server:: { conn:: AddrIncoming , Server as HyperServer } ,
8
- service:: make_service_fn,
9
- Body , Request ,
10
- } ;
4
+ use axum:: body:: Body ;
5
+ use axum:: serve:: Listener ;
6
+ use hyper:: { header:: CONTENT_TYPE , Request } ;
11
7
use sshx_core:: proto:: { sshx_service_server:: SshxServiceServer , FILE_DESCRIPTOR_SET } ;
12
- use tonic:: transport :: Server as TonicServer ;
13
- use tower:: { steer :: Steer , ServiceBuilder , ServiceExt } ;
8
+ use tonic:: service :: Routes as TonicRoutes ;
9
+ use tower:: { make :: Shared , steer :: Steer , ServiceExt } ;
14
10
use tower_http:: trace:: TraceLayer ;
15
11
16
12
use crate :: { grpc:: GrpcServer , web, ServerState } ;
@@ -19,33 +15,34 @@ use crate::{grpc::GrpcServer, web, ServerState};
19
15
///
20
16
/// This internal method is responsible for multiplexing the HTTP and gRPC
21
17
/// servers onto a single, consolidated `hyper` service.
22
- pub ( crate ) async fn start_server (
18
+ pub ( crate ) async fn start_server < L > (
23
19
state : Arc < ServerState > ,
24
- incoming : AddrIncoming ,
25
- signal : impl Future < Output = ( ) > ,
26
- ) -> Result < ( ) > {
27
- type BoxError = Box < dyn StdError + Send + Sync > ;
28
-
20
+ listener : L ,
21
+ signal : impl Future < Output = ( ) > + Send + ' static ,
22
+ ) -> Result < ( ) >
23
+ where
24
+ L : Listener ,
25
+ L :: Addr : Debug ,
26
+ {
29
27
let http_service = web:: app ( )
30
28
. with_state ( state. clone ( ) )
31
29
. layer ( TraceLayer :: new_for_http ( ) )
32
- . map_response ( |r| r. map ( |b| b. map_err ( BoxError :: from) . boxed_unsync ( ) ) )
33
- . map_err ( BoxError :: from)
30
+ . into_service ( )
34
31
. boxed_clone ( ) ;
35
32
36
- let grpc_service = TonicServer :: builder ( )
33
+ let grpc_service = TonicRoutes :: default ( )
37
34
. add_service ( SshxServiceServer :: new ( GrpcServer :: new ( state) ) )
38
35
. add_service (
39
36
tonic_reflection:: server:: Builder :: configure ( )
40
37
. register_encoded_file_descriptor_set ( FILE_DESCRIPTOR_SET )
41
- . build ( ) ?,
38
+ . build_v1 ( ) ?,
42
39
)
43
- . into_service ( ) ;
44
-
45
- let grpc_service = ServiceBuilder :: new ( )
40
+ . into_axum_router ( )
46
41
. layer ( TraceLayer :: new_for_grpc ( ) )
47
- . service ( grpc_service)
48
- . map_response ( |r| r. map ( |b| b. map_err ( BoxError :: from) . boxed_unsync ( ) ) )
42
+ . into_service ( )
43
+ // This type conversion is necessary because Tonic 0.12 uses Axum 0.7, so its `axum::Router`
44
+ // and `axum::Body` are based on an older `axum_core` version.
45
+ . map_response ( |r| r. map ( Body :: new) )
49
46
. boxed_clone ( ) ;
50
47
51
48
let svc = Steer :: new (
@@ -58,14 +55,9 @@ pub(crate) async fn start_server(
58
55
}
59
56
} ,
60
57
) ;
61
- let make_svc = make_service_fn ( move |_| {
62
- let svc = svc. clone ( ) ;
63
- async { Ok :: < _ , std:: convert:: Infallible > ( svc) }
64
- } ) ;
58
+ let make_svc = Shared :: new ( svc) ;
65
59
66
- HyperServer :: builder ( incoming)
67
- . tcp_nodelay ( true )
68
- . serve ( make_svc)
60
+ axum:: serve ( listener, make_svc)
69
61
. with_graceful_shutdown ( signal)
70
62
. await ?;
71
63
0 commit comments