Skip to content

Commit

Permalink
feat: add created at field for config
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 2, 2024
1 parent ada3ed8 commit 1ab62c5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/config/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ pub struct PingapConf {
pub group: Option<String>,
pub threads: Option<usize>,
pub work_stealing: Option<bool>,
pub created_at: Option<String>,
}

impl PingapConf {
Expand Down Expand Up @@ -243,6 +244,7 @@ struct TomlConfig {
group: Option<String>,
threads: Option<usize>,
work_stealing: Option<bool>,
created_at: Option<String>,
}

fn format_toml(value: &Value) -> String {
Expand All @@ -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(())
Expand Down Expand Up @@ -318,6 +322,7 @@ pub fn load_config(path: &str, admin: bool) -> Result<PingapConf> {
group: data.group,
threads,
work_stealing: data.work_stealing,
created_at: data.created_at,
..Default::default()
};
for (name, value) in data.upstreams {
Expand Down
2 changes: 2 additions & 0 deletions src/proxy/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
4 changes: 2 additions & 2 deletions src/serve/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})?;
Expand Down Expand Up @@ -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")
})?;
Expand Down

0 comments on commit 1ab62c5

Please sign in to comment.