1
+ use std:: time:: Duration ;
2
+
1
3
use axum:: {
2
- http:: Method , routing:: get, Router
4
+ body:: Body ,
5
+ http:: { Method , Request , Response } ,
6
+ routing:: get,
7
+ Router
8
+ } ;
9
+ use tower_http:: {
10
+ cors:: { Any , CorsLayer } ,
11
+ trace:: TraceLayer
3
12
} ;
4
- use tower_http:: { cors:: { Any , CorsLayer } , trace:: TraceLayer } ;
5
13
use anyhow:: Result ;
14
+ use tracing:: { Level , Span } ;
15
+ use tracing_subscriber:: fmt:: time;
6
16
use utils:: env:: ENV_CONFIG ;
7
17
8
18
@@ -17,17 +27,39 @@ async fn main() -> Result<()> {
17
27
let env_config = ENV_CONFIG . get ( ) . expect ( "Failed to load env config." ) ;
18
28
19
29
tracing_subscriber:: fmt ( )
20
- . with_max_level ( tracing:: Level :: INFO )
30
+ . with_timer ( time:: time ( ) )
31
+ . with_target ( false )
32
+ . with_max_level ( Level :: INFO )
21
33
. init ( ) ;
22
34
35
+ let trace = TraceLayer :: new_for_http ( )
36
+ . on_request (
37
+ |request : & Request < Body > , _span : & Span | {
38
+ tracing:: info!(
39
+ " Incoming [ {} ] {}" ,
40
+ request. method( ) ,
41
+ request. uri( ) . path( )
42
+ ) ;
43
+ }
44
+ )
45
+ . on_response (
46
+ |_response : & Response < Body > , _latency : Duration , _span : & Span | {
47
+ tracing:: info!(
48
+ " Outgoing [ {} ] Took {} ms" ,
49
+ _response. status( ) . as_u16( ) ,
50
+ _latency. as_millis( )
51
+ ) ;
52
+ }
53
+ ) ;
54
+
23
55
let cors = CorsLayer :: new ( )
24
56
. allow_methods ( [ Method :: GET ] )
25
57
. allow_origin ( Any ) ;
26
58
27
59
let app = Router :: new ( )
28
60
. route ( "/images/{target}" , get ( endpoints:: legacy_image:: handler) )
29
61
. route ( "/images/{season}/{episode}/{target}" , get ( endpoints:: image:: handler) )
30
- . layer ( TraceLayer :: new_for_http ( ) )
62
+ . layer ( trace )
31
63
. layer ( cors) ;
32
64
33
65
let listener = tokio:: net:: TcpListener :: bind (
0 commit comments