1
- use http_body_util:: { Either , Full } ;
1
+ use http_body_util:: { combinators :: BoxBody , BodyExt , Full } ;
2
2
use hyper:: { body:: Incoming , service:: service_fn, Request , Response , StatusCode } ;
3
3
use hyper_util:: rt:: { TokioExecutor , TokioIo } ;
4
4
use opentelemetry:: {
@@ -21,32 +21,44 @@ fn extract_context_from_request(req: &Request<Incoming>) -> Context {
21
21
}
22
22
23
23
// Separate async function for the handle endpoint
24
- async fn handle_health_check ( _req : Request < Incoming > ) -> Result < Response < Full < Bytes > > , Infallible > {
24
+ async fn handle_health_check (
25
+ _req : Request < Incoming > ,
26
+ ) -> Result < Response < BoxBody < Bytes , hyper:: Error > > , Infallible > {
25
27
let tracer = global:: tracer ( "example/server" ) ;
26
28
let mut span = tracer
27
29
. span_builder ( "health_check" )
28
30
. with_kind ( SpanKind :: Internal )
29
31
. start ( & tracer) ;
30
32
span. add_event ( "Health check accessed" , vec ! [ ] ) ;
31
- let res = Response :: new ( Full :: new ( Bytes :: from_static ( b"Server is up and running!" ) ) ) ;
33
+
34
+ let res = Response :: new (
35
+ Full :: new ( Bytes :: from_static ( b"Server is up and running!" ) )
36
+ . map_err ( |err| match err { } )
37
+ . boxed ( ) ,
38
+ ) ;
39
+
32
40
Ok ( res)
33
41
}
34
42
35
43
// Separate async function for the echo endpoint
36
- async fn handle_echo ( req : Request < Incoming > ) -> Result < Response < Incoming > , Infallible > {
44
+ async fn handle_echo (
45
+ req : Request < Incoming > ,
46
+ ) -> Result < Response < BoxBody < Bytes , hyper:: Error > > , Infallible > {
37
47
let tracer = global:: tracer ( "example/server" ) ;
38
48
let mut span = tracer
39
49
. span_builder ( "echo" )
40
50
. with_kind ( SpanKind :: Internal )
41
51
. start ( & tracer) ;
42
52
span. add_event ( "Echoing back the request" , vec ! [ ] ) ;
43
- let res = Response :: new ( req. into_body ( ) ) ;
53
+
54
+ let res = Response :: new ( req. into_body ( ) . boxed ( ) ) ;
55
+
44
56
Ok ( res)
45
57
}
46
58
47
59
async fn router (
48
60
req : Request < Incoming > ,
49
- ) -> Result < Response < Either < Full < Bytes > , Incoming > > , Infallible > {
61
+ ) -> Result < Response < BoxBody < Bytes , hyper :: Error > > , Infallible > {
50
62
// Extract the context from the incoming request headers
51
63
let parent_cx = extract_context_from_request ( & req) ;
52
64
let response = {
@@ -61,18 +73,12 @@ async fn router(
61
73
62
74
let cx = Context :: default ( ) . with_span ( span) ;
63
75
match ( req. method ( ) , req. uri ( ) . path ( ) ) {
64
- ( & hyper:: Method :: GET , "/health" ) => handle_health_check ( req)
65
- . with_context ( cx)
66
- . await
67
- . map ( |response| response. map ( Either :: Left ) ) ,
68
- ( & hyper:: Method :: GET , "/echo" ) => handle_echo ( req)
69
- . with_context ( cx)
70
- . await
71
- . map ( |response| response. map ( Either :: Right ) ) ,
76
+ ( & hyper:: Method :: GET , "/health" ) => handle_health_check ( req) . with_context ( cx) . await ,
77
+ ( & hyper:: Method :: GET , "/echo" ) => handle_echo ( req) . with_context ( cx) . await ,
72
78
_ => {
73
79
cx. span ( )
74
80
. set_attribute ( KeyValue :: new ( trace:: HTTP_RESPONSE_STATUS_CODE , 404 ) ) ;
75
- let mut not_found = Response :: new ( Either :: Left ( Full :: default ( ) ) ) ;
81
+ let mut not_found = Response :: new ( BoxBody :: default ( ) ) ;
76
82
* not_found. status_mut ( ) = StatusCode :: NOT_FOUND ;
77
83
Ok ( not_found)
78
84
}
0 commit comments