mirror of
				https://github.com/Tim-Paik/srv.git
				synced 2024-10-13 00:29:43 +00:00 
			
		
		
		
	Fixed the problem that log cannot be displayed in doc mode
This commit is contained in:
		
							
								
								
									
										190
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										190
									
								
								src/main.rs
									
									
									
									
									
								
							| @ -197,11 +197,11 @@ fn render_index( | |||||||
|         let res = actix_files::NamedFile::open(index)? |         let res = actix_files::NamedFile::open(index)? | ||||||
|             .set_content_type(mime_guess::mime::TEXT_HTML_UTF_8) |             .set_content_type(mime_guess::mime::TEXT_HTML_UTF_8) | ||||||
|             .into_response(req); |             .into_response(req); | ||||||
|         return Ok(ServiceResponse::new(req.clone(), res)); |         return Ok(ServiceResponse::new(req.to_owned(), res)); | ||||||
|     } |     } | ||||||
|     if var("NOINDEX").unwrap_or_else(|_| "false".to_string()) == "true" { |     if var("NOINDEX").unwrap_or_else(|_| "false".to_string()) == "true" { | ||||||
|         return Ok(ServiceResponse::new( |         return Ok(ServiceResponse::new( | ||||||
|             req.clone(), |             req.to_owned(), | ||||||
|             HttpResponse::NotFound().body(""), |             HttpResponse::NotFound().body(""), | ||||||
|         )); |         )); | ||||||
|     } |     } | ||||||
| @ -292,7 +292,7 @@ fn render_index( | |||||||
|     let res = HttpResponse::Ok() |     let res = HttpResponse::Ok() | ||||||
|         .content_type("text/html; charset=utf-8") |         .content_type("text/html; charset=utf-8") | ||||||
|         .body(index); |         .body(index); | ||||||
|     Ok(ServiceResponse::new(req.clone(), res)) |     Ok(ServiceResponse::new(req.to_owned(), res)) | ||||||
| } | } | ||||||
|  |  | ||||||
| #[inline] | #[inline] | ||||||
| @ -479,6 +479,97 @@ async fn main() -> std::io::Result<()> { | |||||||
|         open_in_browser(&url); |         open_in_browser(&url); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     if let Some(matches) = matches.subcommand_matches("doc") { | ||||||
|  |         if !matches.is_present("log") { | ||||||
|  |             set_var("RUST_LOG", "info,actix_web::middleware::logger=off"); | ||||||
|  |         } | ||||||
|  |         if matches.is_present("quietall") { | ||||||
|  |             set_var("RUST_LOG", "off"); | ||||||
|  |         } | ||||||
|  |         if matches.is_present("nocolor") { | ||||||
|  |             set_var("RUST_LOG_STYLE", "never"); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")) | ||||||
|  |         .format(move |buf, record| { | ||||||
|  |             let data = record.args().to_string(); | ||||||
|  |             let mut style = buf.style(); | ||||||
|  |             let blue = style.set_color(Color::Cyan); | ||||||
|  |             let mut style = buf.style(); | ||||||
|  |             let red = style.set_color(Color::Red); | ||||||
|  |             let mut style = buf.style(); | ||||||
|  |             let green = style.set_color(Color::Green); | ||||||
|  |             if record.target() == "actix_web::middleware::logger" { | ||||||
|  |                 let data: Vec<&str> = data.splitn(5, '^').collect(); | ||||||
|  |                 let time = blue.value( | ||||||
|  |                     data[0] | ||||||
|  |                         .parse::<chrono::DateTime<chrono::Utc>>() | ||||||
|  |                         .unwrap() | ||||||
|  |                         .format("%Y/%m/%d %H:%M:%S") | ||||||
|  |                         .to_string(), | ||||||
|  |                 ); | ||||||
|  |                 let ipaddr = blue.value(data[1]); | ||||||
|  |                 let status_code = data[2].parse().unwrap_or(500); | ||||||
|  |                 let status_code = if status_code < 400 { | ||||||
|  |                     green.value(status_code) | ||||||
|  |                 } else { | ||||||
|  |                     red.value(status_code) | ||||||
|  |                 }; | ||||||
|  |                 let process_time: Vec<&str> = data[3].splitn(2, '.').collect(); | ||||||
|  |                 let process_time = process_time[0].to_string() + "ms"; | ||||||
|  |                 let process_time = blue.value(if process_time.len() == 3 { | ||||||
|  |                     "  ".to_string() + &process_time | ||||||
|  |                 } else if process_time.len() == 4 { | ||||||
|  |                     " ".to_string() + &process_time | ||||||
|  |                 } else { | ||||||
|  |                     process_time | ||||||
|  |                 }); | ||||||
|  |                 let content = blue.value( | ||||||
|  |                     urlencoding::decode(data[4]) | ||||||
|  |                         .unwrap_or(std::borrow::Cow::Borrowed("[Parse URL Error]")) | ||||||
|  |                         .into_owned(), | ||||||
|  |                 ); | ||||||
|  |                 return writeln!( | ||||||
|  |                     buf, | ||||||
|  |                     "[{}] {} | {} | {} | {}", | ||||||
|  |                     time, ipaddr, status_code, process_time, content | ||||||
|  |                 ); | ||||||
|  |             } else if record.target() == "actix_server::builder" { | ||||||
|  |                 if data.starts_with("Starting ") && data.ends_with(" workers") { | ||||||
|  |                     return Ok(()); | ||||||
|  |                 } | ||||||
|  |             } else if record.target() == "actix_server::server" { | ||||||
|  |                 if data == "Actix runtime found; starting in Actix runtime" { | ||||||
|  |                     let data = format!( | ||||||
|  |                         "[INFO] Serving {} on {}", | ||||||
|  |                         var("ROOT").unwrap_or_else(|_| ".".to_string()), | ||||||
|  |                         var("LISTEN_ADDRESS").unwrap_or_else(|_| "0.0.0.0:8000".to_string()) | ||||||
|  |                     ); | ||||||
|  |                     return writeln!(buf, "\r{}", green.value(data)); | ||||||
|  |                 } | ||||||
|  |                 if data == "SIGINT received; starting forced shutdown" { | ||||||
|  |                     return writeln!(buf, "\r{}", green.value("[INFO] SIGINT received; starting forced shutdown")); | ||||||
|  |                     // Add '\r' to remove the input ^C | ||||||
|  |                 } | ||||||
|  |                 return Ok(()); | ||||||
|  |             } else if record.target() == "actix_server::worker" | ||||||
|  |                 || record.target() == "actix_server::accept" | ||||||
|  |             { | ||||||
|  |                 return Ok(()); | ||||||
|  |             } | ||||||
|  |             if data.starts_with("[ERROR]") | ||||||
|  |                 || data.starts_with("TLS alert") | ||||||
|  |                 || data.starts_with("Failed") | ||||||
|  |             { | ||||||
|  |                 writeln!(buf, "\r{}", red.value(data)) | ||||||
|  |             } else { | ||||||
|  |                 writeln!(buf, "\r{}", green.value(data)) | ||||||
|  |             } | ||||||
|  |         }) | ||||||
|  |         .init(); | ||||||
|  |  | ||||||
|         let addr = if let Some(matches) = matches.subcommand_matches("doc") { |         let addr = if let Some(matches) = matches.subcommand_matches("doc") { | ||||||
|             let mut cargo_toml = match std::fs::File::open("./Cargo.toml") { |             let mut cargo_toml = match std::fs::File::open("./Cargo.toml") { | ||||||
|                 Ok(file) => file, |                 Ok(file) => file, | ||||||
| @ -545,98 +636,11 @@ async fn main() -> std::io::Result<()> { | |||||||
|             if !matches.is_present("noopen") { |             if !matches.is_present("noopen") { | ||||||
|                 open_in_browser(&url); |                 open_in_browser(&url); | ||||||
|             } |             } | ||||||
|         if !matches.is_present("log") { |  | ||||||
|             set_var("RUST_LOG", "info,actix_web::middleware::logger=off"); |  | ||||||
|         } |  | ||||||
|         if matches.is_present("quietall") { |  | ||||||
|             set_var("RUST_LOG", "off"); |  | ||||||
|         } |  | ||||||
|         if matches.is_present("nocolor") { |  | ||||||
|             set_var("RUST_LOG_STYLE", "never"); |  | ||||||
|         } |  | ||||||
|             addr |             addr | ||||||
|         } else { |         } else { | ||||||
|             addr |             addr | ||||||
|         }; |         }; | ||||||
|  |         set_var("LISTEN_ADDRESS", addr); | ||||||
|     let addr_copy = addr.clone(); |  | ||||||
|     env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")) |  | ||||||
|         .format(move |buf, record| { |  | ||||||
|             let data = record.args().to_string(); |  | ||||||
|             let mut style = buf.style(); |  | ||||||
|             let blue = style.set_color(Color::Cyan); |  | ||||||
|             let mut style = buf.style(); |  | ||||||
|             let red = style.set_color(Color::Red); |  | ||||||
|             let mut style = buf.style(); |  | ||||||
|             let green = style.set_color(Color::Green); |  | ||||||
|             if record.target() == "actix_web::middleware::logger" { |  | ||||||
|                 let data: Vec<&str> = data.splitn(5, '^').collect(); |  | ||||||
|                 let time = blue.value( |  | ||||||
|                     data[0] |  | ||||||
|                         .parse::<chrono::DateTime<chrono::Utc>>() |  | ||||||
|                         .unwrap() |  | ||||||
|                         .format("%Y/%m/%d %H:%M:%S") |  | ||||||
|                         .to_string(), |  | ||||||
|                 ); |  | ||||||
|                 let ipaddr = blue.value(data[1]); |  | ||||||
|                 let status_code = data[2].parse().unwrap_or(500); |  | ||||||
|                 let status_code = if status_code < 400 { |  | ||||||
|                     green.value(status_code) |  | ||||||
|                 } else { |  | ||||||
|                     red.value(status_code) |  | ||||||
|                 }; |  | ||||||
|                 let process_time: Vec<&str> = data[3].splitn(2, '.').collect(); |  | ||||||
|                 let process_time = process_time[0].to_string() + "ms"; |  | ||||||
|                 let process_time = blue.value(if process_time.len() == 3 { |  | ||||||
|                     "  ".to_string() + &process_time |  | ||||||
|                 } else if process_time.len() == 4 { |  | ||||||
|                     " ".to_string() + &process_time |  | ||||||
|                 } else { |  | ||||||
|                     process_time |  | ||||||
|                 }); |  | ||||||
|                 let content = blue.value( |  | ||||||
|                     urlencoding::decode(data[4]) |  | ||||||
|                         .unwrap_or(std::borrow::Cow::Borrowed("[Parse URL Error]")) |  | ||||||
|                         .into_owned(), |  | ||||||
|                 ); |  | ||||||
|                 return writeln!( |  | ||||||
|                     buf, |  | ||||||
|                     "[{}] {} | {} | {} | {}", |  | ||||||
|                     time, ipaddr, status_code, process_time, content |  | ||||||
|                 ); |  | ||||||
|             } else if record.target() == "actix_server::builder" { |  | ||||||
|                 if data.starts_with("Starting ") && data.ends_with(" workers") { |  | ||||||
|                     return Ok(()); |  | ||||||
|                 } |  | ||||||
|             } else if record.target() == "actix_server::server" { |  | ||||||
|                 if data == "Actix runtime found; starting in Actix runtime" { |  | ||||||
|                     let data = format!( |  | ||||||
|                         "[INFO] Serving {} on {}", |  | ||||||
|                         var("ROOT").unwrap_or_else(|_| ".".to_string()), |  | ||||||
|                         addr_copy |  | ||||||
|                     ); |  | ||||||
|                     return writeln!(buf, "\r{}", green.value(data)); |  | ||||||
|                 } |  | ||||||
|                 if data == "SIGINT received; starting forced shutdown" { |  | ||||||
|                     return writeln!(buf, "\r{}", green.value("[INFO] SIGINT received; starting forced shutdown")); |  | ||||||
|                     // Add '\r' to remove the input ^C |  | ||||||
|                 } |  | ||||||
|                 return Ok(()); |  | ||||||
|             } else if record.target() == "actix_server::worker" |  | ||||||
|                 || record.target() == "actix_server::accept" |  | ||||||
|             { |  | ||||||
|                 return Ok(()); |  | ||||||
|             } |  | ||||||
|             if data.starts_with("[ERROR]") |  | ||||||
|                 || data.starts_with("TLS alert") |  | ||||||
|                 || data.starts_with("Failed") |  | ||||||
|             { |  | ||||||
|                 writeln!(buf, "\r{}", red.value(data)) |  | ||||||
|             } else { |  | ||||||
|                 writeln!(buf, "\r{}", green.value(data)) |  | ||||||
|             } |  | ||||||
|         }) |  | ||||||
|         .init(); |  | ||||||
|      |      | ||||||
|     let server = HttpServer::new(move || { |     let server = HttpServer::new(move || { | ||||||
|         let app = App::new() |         let app = App::new() | ||||||
| @ -729,9 +733,9 @@ async fn main() -> std::io::Result<()> { | |||||||
|             .with_no_client_auth() |             .with_no_client_auth() | ||||||
|             .with_single_cert(cert, key) |             .with_single_cert(cert, key) | ||||||
|             .expect("bad certificate/key"); |             .expect("bad certificate/key"); | ||||||
|         server.bind_rustls(addr, config) |         server.bind_rustls(var("LISTEN_ADDRESS").unwrap_or_else(|_| "0.0.0.0:8000".to_string()), config) | ||||||
|     } else { |     } else { | ||||||
|         server.bind(addr) |         server.bind(var("LISTEN_ADDRESS").unwrap_or_else(|_| "0.0.0.0:8000".to_string())) | ||||||
|     }; |     }; | ||||||
|     server?.run().await |     server?.run().await | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user