refactor: improve code quanity (#282)

- rename LogHttp to HttpLogger
This commit is contained in:
sigoden 2023-11-04 17:10:38 +08:00 committed by GitHub
parent 4ef07737e1
commit 80ac9afe68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 12 deletions

View file

@ -8,7 +8,7 @@ use std::net::IpAddr;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use crate::auth::AccessControl; use crate::auth::AccessControl;
use crate::log_http::LogHttp; use crate::http_logger::HttpLogger;
use crate::utils::encode_uri; use crate::utils::encode_uri;
pub fn build_cli() -> Command { pub fn build_cli() -> Command {
@ -260,7 +260,7 @@ pub struct Args {
pub assets: Option<PathBuf>, pub assets: Option<PathBuf>,
#[serde(deserialize_with = "deserialize_log_http")] #[serde(deserialize_with = "deserialize_log_http")]
#[serde(rename = "log-format")] #[serde(rename = "log-format")]
pub log_http: LogHttp, pub http_logger: HttpLogger,
pub tls_cert: Option<PathBuf>, pub tls_cert: Option<PathBuf>,
pub tls_key: Option<PathBuf>, pub tls_key: Option<PathBuf>,
} }
@ -361,7 +361,7 @@ impl Args {
} }
if let Some(log_format) = matches.get_one::<String>("log-format") { if let Some(log_format) = matches.get_one::<String>("log-format") {
args.log_http = log_format.parse()?; args.http_logger = log_format.parse()?;
} }
if let Some(assets_path) = matches.get_one::<PathBuf>("assets") { if let Some(assets_path) = matches.get_one::<PathBuf>("assets") {
@ -468,7 +468,7 @@ where
AccessControl::new(&rules).map_err(serde::de::Error::custom) AccessControl::new(&rules).map_err(serde::de::Error::custom)
} }
fn deserialize_log_http<'de, D>(deserializer: D) -> Result<LogHttp, D::Error> fn deserialize_log_http<'de, D>(deserializer: D) -> Result<HttpLogger, D::Error>
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {

View file

@ -5,11 +5,11 @@ use crate::{auth::get_auth_user, server::Request};
pub const DEFAULT_LOG_FORMAT: &str = r#"$remote_addr "$request" $status"#; pub const DEFAULT_LOG_FORMAT: &str = r#"$remote_addr "$request" $status"#;
#[derive(Debug)] #[derive(Debug)]
pub struct LogHttp { pub struct HttpLogger {
elements: Vec<LogElement>, elements: Vec<LogElement>,
} }
impl Default for LogHttp { impl Default for HttpLogger {
fn default() -> Self { fn default() -> Self {
DEFAULT_LOG_FORMAT.parse().unwrap() DEFAULT_LOG_FORMAT.parse().unwrap()
} }
@ -22,7 +22,7 @@ enum LogElement {
Literal(String), Literal(String),
} }
impl LogHttp { impl HttpLogger {
pub fn data(&self, req: &Request) -> HashMap<String, String> { pub fn data(&self, req: &Request) -> HashMap<String, String> {
let mut data = HashMap::default(); let mut data = HashMap::default();
for element in self.elements.iter() { for element in self.elements.iter() {
@ -70,7 +70,7 @@ impl LogHttp {
} }
} }
impl FromStr for LogHttp { impl FromStr for HttpLogger {
type Err = anyhow::Error; type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut elements = vec![]; let mut elements = vec![];

View file

@ -1,6 +1,6 @@
mod args; mod args;
mod auth; mod auth;
mod log_http; mod http_logger;
mod logger; mod logger;
mod server; mod server;
mod streamer; mod streamer;

View file

@ -98,7 +98,7 @@ impl Server {
let uri = req.uri().clone(); let uri = req.uri().clone();
let assets_prefix = &self.assets_prefix; let assets_prefix = &self.assets_prefix;
let enable_cors = self.args.enable_cors; 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 { if let Some(addr) = addr {
http_log_data.insert("remote_addr".to_string(), addr.ip().to_string()); http_log_data.insert("remote_addr".to_string(), addr.ip().to_string());
} }
@ -107,7 +107,7 @@ impl Server {
Ok(res) => { Ok(res) => {
http_log_data.insert("status".to_string(), res.status().as_u16().to_string()); http_log_data.insert("status".to_string(), res.status().as_u16().to_string());
if !uri.path().starts_with(assets_prefix) { 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 res
} }
@ -117,7 +117,7 @@ impl Server {
*res.status_mut() = status; *res.status_mut() = status;
http_log_data.insert("status".to_string(), status.as_u16().to_string()); http_log_data.insert("status".to_string(), status.as_u16().to_string());
self.args self.args
.log_http .http_logger
.log(&http_log_data, Some(err.to_string())); .log(&http_log_data, Some(err.to_string()));
res res
} }