mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-29 22:38:36 +08:00
Add global headers for all upstreams with Option<T>
This commit is contained in:
@@ -9,9 +9,6 @@ upstreams:
|
||||
paths:
|
||||
"/":
|
||||
ssl: false
|
||||
headers:
|
||||
- "X-Some-Thing:Yaaaaaaaaaaaaaaa"
|
||||
- "X-Prox-From:Hopaaaaaaaaaaaar"
|
||||
servers:
|
||||
- "127.0.0.1:8000"
|
||||
- "127.0.0.2:8000"
|
||||
@@ -27,9 +24,6 @@ upstreams:
|
||||
- "127.0.0.2:8000"
|
||||
"/draw":
|
||||
ssl: false
|
||||
headers:
|
||||
- "X-Some-Thing:Yaaaaaaaaaaaaaaa"
|
||||
- "X-Prox-From:Hopaaaaaaaaaaaar"
|
||||
servers:
|
||||
- "192.168.1.1:8000"
|
||||
polo.netangels.net:
|
||||
@@ -38,14 +32,10 @@ upstreams:
|
||||
ssl: false
|
||||
headers:
|
||||
- "X-Some-Thing:Yaaaaaaaaaaaaaaa"
|
||||
- "X-Prox-From:Hopaaaaaaaaaaaar"
|
||||
servers:
|
||||
- "192.168.1.10:8000"
|
||||
"/ws":
|
||||
ssl: false
|
||||
headers:
|
||||
- "X-Some-Thing:Yaaaaaaaaaaaaaaa"
|
||||
- "X-Prox-From:Hopaaaaaaaaaaaar"
|
||||
servers:
|
||||
- "192.168.1.1:8000"
|
||||
glop.netangels.net:
|
||||
@@ -61,17 +51,11 @@ upstreams:
|
||||
paths:
|
||||
"/":
|
||||
ssl: true
|
||||
headers:
|
||||
- "X-Some-Thing:Yaaaaaaaaaaaaaaa"
|
||||
- "X-Prox-From:Hopaaaaaaaaaaaar"
|
||||
servers:
|
||||
- "apt.netangels.net:443"
|
||||
127.0.0.1:
|
||||
paths:
|
||||
"/camerastatus":
|
||||
ssl: false
|
||||
headers:
|
||||
- "X-Some-Thing:Yaaaaaaaaaaaaaaa"
|
||||
- "X-Prox-From:Hopaaaaaaaaaaaar"
|
||||
servers:
|
||||
- "192.168.1.5:8080"
|
||||
@@ -9,8 +9,8 @@ use std::sync::atomic::AtomicUsize;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Config {
|
||||
upstreams: HashMap<String, HostConfig>,
|
||||
globals: HashMap<String, Vec<String>>,
|
||||
upstreams: Option<HashMap<String, HostConfig>>,
|
||||
globals: Option<HashMap<String, Vec<String>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
@@ -22,12 +22,12 @@ struct HostConfig {
|
||||
struct PathConfig {
|
||||
ssl: bool,
|
||||
servers: Vec<String>,
|
||||
headers: Vec<String>,
|
||||
headers: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
pub fn load_yaml_to_dashmap(d: &str, kind: &str) -> Option<(UpstreamsDashMap, Headers)> {
|
||||
let dashmap = UpstreamsDashMap::new();
|
||||
let headers = DashMap::new();
|
||||
let headerm = DashMap::new();
|
||||
let mut yaml_data = d.to_string();
|
||||
match kind {
|
||||
"filepath" => {
|
||||
@@ -52,42 +52,47 @@ pub fn load_yaml_to_dashmap(d: &str, kind: &str) -> Option<(UpstreamsDashMap, He
|
||||
let p: Result<Config, Error> = serde_yaml::from_str(&yaml_data);
|
||||
match p {
|
||||
Ok(parsed) => {
|
||||
for (hostname, host_config) in parsed.upstreams {
|
||||
let path_map = DashMap::new();
|
||||
let header_list = DashMap::new();
|
||||
for (path, path_config) in host_config.paths {
|
||||
let mut server_list = Vec::new();
|
||||
let mut hl = Vec::new();
|
||||
|
||||
// Set global headers
|
||||
for headers in parsed.globals.get("headers").iter().by_ref() {
|
||||
for header in headers.iter() {
|
||||
if let Some((key, val)) = header.split_once(':') {
|
||||
hl.push((key.to_string(), val.to_string()));
|
||||
if let Some(headers) = parsed.upstreams {
|
||||
for (hostname, host_config) in headers {
|
||||
let path_map = DashMap::new();
|
||||
let header_list = DashMap::new();
|
||||
for (path, path_config) in host_config.paths {
|
||||
let mut server_list = Vec::new();
|
||||
let mut hl = Vec::new();
|
||||
// Set global headers
|
||||
if let Some(globals) = &parsed.globals {
|
||||
for headers in globals.get("headers").iter().by_ref() {
|
||||
for header in headers.iter() {
|
||||
if let Some((key, val)) = header.split_once(':') {
|
||||
hl.push((key.to_string(), val.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Set per host/path headers
|
||||
for header in path_config.headers.iter().by_ref() {
|
||||
if let Some((key, val)) = header.split_once(':') {
|
||||
hl.push((key.to_string(), val.to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
header_list.insert(path.clone(), hl);
|
||||
for server in path_config.servers {
|
||||
if let Some((ip, port_str)) = server.split_once(':') {
|
||||
if let Ok(port) = port_str.parse::<u16>() {
|
||||
server_list.push((ip.to_string(), port, path_config.ssl));
|
||||
// Set per host/path headers
|
||||
if let Some(headers) = &path_config.headers {
|
||||
for header in headers.iter().by_ref() {
|
||||
if let Some((key, val)) = header.split_once(':') {
|
||||
hl.push((key.to_string(), val.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
header_list.insert(path.clone(), hl);
|
||||
for server in path_config.servers {
|
||||
if let Some((ip, port_str)) = server.split_once(':') {
|
||||
if let Ok(port) = port_str.parse::<u16>() {
|
||||
server_list.push((ip.to_string(), port, path_config.ssl));
|
||||
}
|
||||
}
|
||||
}
|
||||
path_map.insert(path, (server_list, AtomicUsize::new(0)));
|
||||
}
|
||||
path_map.insert(path, (server_list, AtomicUsize::new(0)));
|
||||
headerm.insert(hostname.clone(), header_list);
|
||||
dashmap.insert(hostname, path_map);
|
||||
}
|
||||
headers.insert(hostname.clone(), header_list);
|
||||
dashmap.insert(hostname, path_map);
|
||||
}
|
||||
Some((dashmap, headers))
|
||||
|
||||
Some((dashmap, headerm))
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to parse upstreams file: {}", e);
|
||||
|
||||
@@ -11,10 +11,10 @@ pub fn print_upstreams(upstreams: &UpstreamsDashMap) {
|
||||
|
||||
for path_entry in host_entry.value().iter() {
|
||||
let path = path_entry.key();
|
||||
println!(" Path: {}", path);
|
||||
println!(" Path: {}", path);
|
||||
|
||||
for (ip, port, ssl) in path_entry.value().0.clone() {
|
||||
println!(" ===> IP: {}, Port: {}, SSL: {}", ip, port, ssl);
|
||||
println!(" ===> IP: {}, Port: {}, SSL: {}", ip, port, ssl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user