From 80ac9afe683e90316e1fc2e8cd4c793bc3c8c740 Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 4 Nov 2023 17:10:38 +0800 Subject: [PATCH] refactor: improve code quanity (#282) - rename LogHttp to HttpLogger --- src/args.rs | 8 ++++---- src/{log_http.rs => http_logger.rs} | 8 ++++---- src/main.rs | 2 +- src/server.rs | 6 +++--- tests/{log_http.rs => http_logger.rs} | 0 5 files changed, 12 insertions(+), 12 deletions(-) rename src/{log_http.rs => http_logger.rs} (97%) rename tests/{log_http.rs => http_logger.rs} (100%) diff --git a/src/args.rs b/src/args.rs index 65fd58c..a822d70 100644 --- a/src/args.rs +++ b/src/args.rs @@ -8,7 +8,7 @@ use std::net::IpAddr; use std::path::{Path, PathBuf}; use crate::auth::AccessControl; -use crate::log_http::LogHttp; +use crate::http_logger::HttpLogger; use crate::utils::encode_uri; pub fn build_cli() -> Command { @@ -260,7 +260,7 @@ pub struct Args { pub assets: Option, #[serde(deserialize_with = "deserialize_log_http")] #[serde(rename = "log-format")] - pub log_http: LogHttp, + pub http_logger: HttpLogger, pub tls_cert: Option, pub tls_key: Option, } @@ -361,7 +361,7 @@ impl Args { } if let Some(log_format) = matches.get_one::("log-format") { - args.log_http = log_format.parse()?; + args.http_logger = log_format.parse()?; } if let Some(assets_path) = matches.get_one::("assets") { @@ -468,7 +468,7 @@ where AccessControl::new(&rules).map_err(serde::de::Error::custom) } -fn deserialize_log_http<'de, D>(deserializer: D) -> Result +fn deserialize_log_http<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>, { diff --git a/src/log_http.rs b/src/http_logger.rs similarity index 97% rename from src/log_http.rs rename to src/http_logger.rs index 49e712e..0b56580 100644 --- a/src/log_http.rs +++ b/src/http_logger.rs @@ -5,11 +5,11 @@ use crate::{auth::get_auth_user, server::Request}; pub const DEFAULT_LOG_FORMAT: &str = r#"$remote_addr "$request" $status"#; #[derive(Debug)] -pub struct LogHttp { +pub struct HttpLogger { elements: Vec, } -impl Default for LogHttp { +impl Default for HttpLogger { fn default() -> Self { DEFAULT_LOG_FORMAT.parse().unwrap() } @@ -22,7 +22,7 @@ enum LogElement { Literal(String), } -impl LogHttp { +impl HttpLogger { pub fn data(&self, req: &Request) -> HashMap { let mut data = HashMap::default(); for element in self.elements.iter() { @@ -70,7 +70,7 @@ impl LogHttp { } } -impl FromStr for LogHttp { +impl FromStr for HttpLogger { type Err = anyhow::Error; fn from_str(s: &str) -> Result { let mut elements = vec![]; diff --git a/src/main.rs b/src/main.rs index ebc38ee..a3ac7d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ mod args; mod auth; -mod log_http; +mod http_logger; mod logger; mod server; mod streamer; diff --git a/src/server.rs b/src/server.rs index c9f0b70..95de3dd 100644 --- a/src/server.rs +++ b/src/server.rs @@ -98,7 +98,7 @@ impl Server { let uri = req.uri().clone(); let assets_prefix = &self.assets_prefix; let enable_cors = self.args.enable_cors; - let mut http_log_data = self.args.log_http.data(&req); + let mut http_log_data = self.args.http_logger.data(&req); if let Some(addr) = addr { http_log_data.insert("remote_addr".to_string(), addr.ip().to_string()); } @@ -107,7 +107,7 @@ impl Server { Ok(res) => { http_log_data.insert("status".to_string(), res.status().as_u16().to_string()); if !uri.path().starts_with(assets_prefix) { - self.args.log_http.log(&http_log_data, None); + self.args.http_logger.log(&http_log_data, None); } res } @@ -117,7 +117,7 @@ impl Server { *res.status_mut() = status; http_log_data.insert("status".to_string(), status.as_u16().to_string()); self.args - .log_http + .http_logger .log(&http_log_data, Some(err.to_string())); res } diff --git a/tests/log_http.rs b/tests/http_logger.rs similarity index 100% rename from tests/log_http.rs rename to tests/http_logger.rs