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