@@ -110,13 +110,14 @@ impl Server {
110110 self . handle_web_index ( & mut res) . await
111111 } else if path == "/subscribe/traffics" {
112112 self . handle_subscribe_traffics ( & mut res) . await
113+ } else if let Some ( id) = path. strip_prefix ( "/subscribe/websocket/" ) {
114+ self . handle_subscribe_websocket ( & mut res, id) . await
113115 } else if path == "/traffics" {
114- self . handle_list_traffics ( & mut res) . await
116+ let query = req. uri ( ) . query ( ) . unwrap_or_default ( ) ;
117+ self . handle_list_traffics ( & mut res, query) . await
115118 } else if let Some ( id) = path. strip_prefix ( "/traffic/" ) {
116119 let query = req. uri ( ) . query ( ) . unwrap_or_default ( ) ;
117120 self . handle_get_traffic ( & mut res, id, query) . await
118- } else if let Some ( id) = path. strip_prefix ( "/subscribe/websocket/" ) {
119- self . handle_subscribe_websocket ( & mut res, id) . await
120121 } else {
121122 * res. status_mut ( ) = StatusCode :: NOT_FOUND ;
122123 return Ok ( res) ;
@@ -239,7 +240,7 @@ impl Server {
239240 }
240241
241242 async fn handle_subscribe_traffics ( & self , res : & mut Response ) -> Result < ( ) > {
242- let ( init_data, receiver) = ( self . state . list ( ) , self . state . subscribe_traffics ( ) ) ;
243+ let ( init_data, receiver) = ( self . state . list_heads ( ) , self . state . subscribe_traffics ( ) ) ;
243244 let stream = BroadcastStream :: new ( receiver) ;
244245 let stream = stream
245246 . map_ok ( |head| ndjson_frame ( & head) )
@@ -262,12 +263,19 @@ impl Server {
262263 Ok ( ( ) )
263264 }
264265
265- async fn handle_list_traffics ( & self , res : & mut Response ) -> Result < ( ) > {
266- set_res_body ( res, serde_json:: to_string_pretty ( & self . state . list ( ) ) ?) ;
267- res. headers_mut ( ) . insert (
268- CONTENT_TYPE ,
269- HeaderValue :: from_static ( "application/json; charset=UTF-8" ) ,
270- ) ;
266+ async fn handle_list_traffics ( & self , res : & mut Response , query : & str ) -> Result < ( ) > {
267+ if query. is_empty ( ) {
268+ set_res_body ( res, serde_json:: to_string_pretty ( & self . state . list_heads ( ) ) ?) ;
269+ res. headers_mut ( ) . insert (
270+ CONTENT_TYPE ,
271+ HeaderValue :: from_static ( "application/json; charset=UTF-8" ) ,
272+ ) ;
273+ } else {
274+ let ( data, mime) = self . state . export_traffics ( query) ?;
275+ set_res_body ( res, data) ;
276+ res. headers_mut ( )
277+ . insert ( CONTENT_TYPE , HeaderValue :: from_str ( mime) ?) ;
278+ }
271279 res. headers_mut ( )
272280 . insert ( CACHE_CONTROL , HeaderValue :: from_static ( "no-cache" ) ) ;
273281 Ok ( ( ) )
@@ -276,21 +284,10 @@ impl Server {
276284 async fn handle_get_traffic ( & self , res : & mut Response , id : & str , query : & str ) -> Result < ( ) > {
277285 match id. parse ( ) . ok ( ) . and_then ( |id| self . state . get_traffic ( id) ) {
278286 Some ( traffic) => {
279- match query {
280- "markdown" | "curl" | "har" | "res-body" => {
281- let ( data, mime) = traffic. export ( query) ?;
282- set_res_body ( res, data) ;
283- res. headers_mut ( )
284- . insert ( CONTENT_TYPE , HeaderValue :: from_str ( mime) ?) ;
285- }
286- _ => {
287- set_res_body ( res, serde_json:: to_string_pretty ( & traffic) ?) ;
288- res. headers_mut ( ) . insert (
289- CONTENT_TYPE ,
290- HeaderValue :: from_static ( "application/json; charset=UTF-8" ) ,
291- ) ;
292- }
293- }
287+ let ( data, mime) = traffic. export ( query) ?;
288+ set_res_body ( res, data) ;
289+ res. headers_mut ( )
290+ . insert ( CONTENT_TYPE , HeaderValue :: from_str ( mime) ?) ;
294291 res. headers_mut ( )
295292 . insert ( CACHE_CONTROL , HeaderValue :: from_static ( "no-cache" ) ) ;
296293 }
0 commit comments