@@ -4,7 +4,7 @@ use hyper_util::rt::{TokioExecutor, TokioIo};
4
4
use hyper_util:: server:: conn:: auto:: Builder ;
5
5
use hyper_util:: service:: TowerToHyperService ;
6
6
use tokio:: net:: TcpListener ;
7
- use tonic:: { transport :: Server , Request , Response , Status } ;
7
+ use tonic:: { service :: Routes , Request , Response , Status } ;
8
8
9
9
use hello_world:: greeter_server:: { Greeter , GreeterServer } ;
10
10
use hello_world:: { HelloReply , HelloRequest } ;
@@ -39,9 +39,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
39
39
println ! ( "GreeterServer listening on {}" , addr) ;
40
40
41
41
let incoming = TcpListener :: bind ( addr) . await ?;
42
- let svc = Server :: builder ( )
43
- . add_service ( GreeterServer :: new ( greeter) )
44
- . into_router ( ) ;
42
+ let svc = Routes :: new ( GreeterServer :: new ( greeter) ) ;
45
43
46
44
let h2c = h2c:: H2c { s : svc } ;
47
45
@@ -71,8 +69,8 @@ mod h2c {
71
69
use http:: { Request , Response } ;
72
70
use hyper:: body:: Incoming ;
73
71
use hyper_util:: { rt:: TokioExecutor , service:: TowerToHyperService } ;
74
- use tonic:: { body:: empty_body, service :: AxumBody } ;
75
- use tower:: Service ;
72
+ use tonic:: body:: { empty_body, BoxBody } ;
73
+ use tower:: { Service , ServiceExt } ;
76
74
77
75
#[ derive( Clone ) ]
78
76
pub struct H2c < S > {
@@ -83,12 +81,12 @@ mod h2c {
83
81
84
82
impl < S > Service < Request < Incoming > > for H2c < S >
85
83
where
86
- S : Service < Request < Incoming > , Response = Response < AxumBody > > + Clone + Send + ' static ,
84
+ S : Service < Request < BoxBody > , Response = Response < BoxBody > > + Clone + Send + ' static ,
87
85
S :: Future : Send + ' static ,
88
86
S :: Error : Into < BoxError > + Sync + Send + ' static ,
89
87
S :: Response : Send + ' static ,
90
88
{
91
- type Response = hyper:: Response < tonic :: body :: BoxBody > ;
89
+ type Response = hyper:: Response < BoxBody > ;
92
90
type Error = hyper:: Error ;
93
91
type Future =
94
92
Pin < Box < dyn std:: future:: Future < Output = Result < Self :: Response , Self :: Error > > + Send > > ;
@@ -100,8 +98,12 @@ mod h2c {
100
98
std:: task:: Poll :: Ready ( Ok ( ( ) ) )
101
99
}
102
100
103
- fn call ( & mut self , mut req : hyper:: Request < Incoming > ) -> Self :: Future {
104
- let svc = self . s . clone ( ) ;
101
+ fn call ( & mut self , req : hyper:: Request < Incoming > ) -> Self :: Future {
102
+ let mut req = req. map ( tonic:: body:: boxed) ;
103
+ let svc = self
104
+ . s
105
+ . clone ( )
106
+ . map_request ( |req : Request < _ > | req. map ( tonic:: body:: boxed) ) ;
105
107
Box :: pin ( async move {
106
108
tokio:: spawn ( async move {
107
109
let upgraded_io = hyper:: upgrade:: on ( & mut req) . await . unwrap ( ) ;
0 commit comments