From 1ab62c5aa50149d9e3c539a9c2dfb29cba6e0cb4 Mon Sep 17 00:00:00 2001 From: vicanso Date: Tue, 2 Apr 2024 21:22:29 +0800 Subject: [PATCH] feat: add created at field for config --- src/config/load.rs | 9 +++++++-- src/proxy/logger.rs | 2 ++ src/serve/admin.rs | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/config/load.rs b/src/config/load.rs index 0a571f0a..2dff386e 100644 --- a/src/config/load.rs +++ b/src/config/load.rs @@ -209,6 +209,7 @@ pub struct PingapConf { pub group: Option, pub threads: Option, pub work_stealing: Option, + pub created_at: Option, } impl PingapConf { @@ -243,6 +244,7 @@ struct TomlConfig { group: Option, threads: Option, work_stealing: Option, + created_at: Option, } fn format_toml(value: &Value) -> String { @@ -256,10 +258,12 @@ fn format_toml(value: &Value) -> String { /// Save the confog to path. /// /// Validate the config before save. -pub fn save_config(path: &str, conf: &PingapConf) -> Result<()> { +pub fn save_config(path: &str, conf: &mut PingapConf) -> Result<()> { conf.validate()?; + conf.created_at = Some(chrono::Local::now().to_rfc3339()); + let filepath = utils::resolve_path(path); - let buf = toml::to_string_pretty(conf).context(SerSnafu)?; + let buf = toml::to_string_pretty(&conf).context(SerSnafu)?; std::fs::write(&filepath, buf).context(IoSnafu { file: filepath })?; Ok(()) @@ -318,6 +322,7 @@ pub fn load_config(path: &str, admin: bool) -> Result { group: data.group, threads, work_stealing: data.work_stealing, + created_at: data.created_at, ..Default::default() }; for (name, value) in data.upstreams { diff --git a/src/proxy/logger.rs b/src/proxy/logger.rs index 8a814aca..2f7bff59 100644 --- a/src/proxy/logger.rs +++ b/src/proxy/logger.rs @@ -248,6 +248,8 @@ impl Parser { TagCategory::Host => { if let Some(host) = get_req_header_value(req_header, "Host") { buf.push_str(host); + } else if let Some(host) = req_header.uri.host() { + buf.push_str(host); } } TagCategory::Method => { diff --git a/src/serve/admin.rs b/src/serve/admin.rs index fd8d0986..6ddecf51 100644 --- a/src/serve/admin.rs +++ b/src/serve/admin.rs @@ -85,7 +85,7 @@ impl AdminServe { } _ => {} }; - save_config(&config::get_config_path(), &conf).map_err(|e| { + save_config(&config::get_config_path(), &mut conf).map_err(|e| { error!("failed to save config: {e}"); pingora::Error::new_str("Save config fail") })?; @@ -136,7 +136,7 @@ impl AdminServe { conf.work_stealing = basic_conf.work_stealing; } }; - save_config(&config::get_config_path(), &conf).map_err(|e| { + save_config(&config::get_config_path(), &mut conf).map_err(|e| { error!("failed to save config: {e}"); pingora::Error::new_str("Save config fail") })?;