Skip to content

Commit

Permalink
refactor: adjust get hash from pingap conf
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 15, 2024
1 parent a023344 commit e985473
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TODO

- [ ] ip block proxy plugin
- [ ] support etcd or other storage for config
- [ ] better error handler
- [ ] log rotate
Expand Down
13 changes: 8 additions & 5 deletions src/config/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ pub struct LocationConf {
pub headers: Option<Vec<String>>,
pub rewrite: Option<String>,
pub weight: Option<u16>,
pub remark: Option<String>,
pub proxy_plugins: Option<Vec<String>>,
pub remark: Option<String>,
}

impl LocationConf {
Expand Down Expand Up @@ -214,7 +214,6 @@ pub struct ServerConf {
pub threads: Option<usize>,
pub tls_cert: Option<String>,
pub tls_key: Option<String>,
pub stats_path: Option<String>,
pub remark: Option<String>,
}

Expand Down Expand Up @@ -243,7 +242,6 @@ impl ServerConf {

#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct PingapConf {
pub hash: String,
pub upstreams: HashMap<String, UpstreamConf>,
pub locations: HashMap<String, LocationConf>,
pub servers: HashMap<String, ServerConf>,
Expand Down Expand Up @@ -287,6 +285,13 @@ impl PingapConf {
}
Ok(())
}
pub fn hash(&self) -> Result<String> {
let data = toml::to_string_pretty(self).context(SerSnafu)?;
let mut lines: Vec<&str> = data.split('\n').collect();
lines.sort();
let hash = crc32fast::hash(lines.join("\n").as_bytes());
Ok(format!("{:X}", hash))
}
}

#[derive(Deserialize, Debug, Serialize)]
Expand Down Expand Up @@ -416,7 +421,6 @@ pub fn load_config(path: &str, admin: bool) -> Result<PingapConf> {
let mut buf = std::fs::read(&filepath).context(IoSnafu { file: filepath })?;
data.append(&mut buf);
}
let hash = crc32fast::hash(&data);
let data: TomlConfig = toml::from_str(
std::string::String::from_utf8_lossy(&data)
.to_string()
Expand All @@ -433,7 +437,6 @@ pub fn load_config(path: &str, admin: bool) -> Result<PingapConf> {
None
};
let mut conf = PingapConf {
hash: format!("{:X}", hash),
error_template: data.error_template.unwrap_or_default(),
pid_file: data.pid_file,
upgrade_sock: data.upgrade_sock,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn run() -> Result<(), Box<dyn Error>> {
return Ok(());
}
config::set_config_path(&args.conf);
config::set_config_hash(&conf.hash);
config::set_config_hash(&conf.hash().unwrap_or_default());

if let Ok(exec_path) = std::env::current_exe() {
let mut cmd = state::RestartProcessCommand {
Expand Down
1 change: 0 additions & 1 deletion src/proxy/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use pingora::{
proxy::{ProxyHttp, Session},
upstreams::peer::HttpPeer,
};
use serde::Serialize;
use snafu::{ResultExt, Snafu};
use std::fs;
use std::sync::atomic::{AtomicI32, AtomicU64, Ordering};
Expand Down
24 changes: 24 additions & 0 deletions web/src/pages/upstream-info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ export default function UpstreamInfo() {
},
],
},
{
id: "ipv4_only",
label: "Ipv4 Only",
defaultValue: upstream.ipv4_only,
span: 4,
category: FormItemCategory.CHECKBOX,
options: [
{
label: "Yes",
option: 1,
value: true,
},
{
label: "No",
option: 0,
value: false,
},
{
label: "None",
option: -1,
value: null,
},
],
},
{
id: "remark",
label: "Remark",
Expand Down

0 comments on commit e985473

Please sign in to comment.