feat: v0.2.0-rc, optimize code
This commit is contained in:
26
src/main.rs
26
src/main.rs
@@ -1,3 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use log::LevelFilter;
|
||||
use pingora::{
|
||||
server::{configuration::Opt, Server},
|
||||
@@ -32,6 +34,10 @@ pub fn main() {
|
||||
services.push(Box::new(prometheus_service_http));
|
||||
}
|
||||
|
||||
if services.is_empty() {
|
||||
panic!("No services is configured!");
|
||||
}
|
||||
|
||||
log::info!("start listen...");
|
||||
my_server.add_services(services);
|
||||
my_server.run_forever();
|
||||
@@ -47,8 +53,9 @@ fn build_services(server: &Server, proxy_config: &ProxyConfig) -> Vec<Box<dyn Se
|
||||
let mut services: Vec<Box<dyn Service>> = vec![];
|
||||
for group in &proxy_config.groups {
|
||||
let listen_address = format!("0.0.0.0:{}", group.port);
|
||||
let host_configs = build_host_configs(group);
|
||||
let host_config_map = build_host_config_map(group);
|
||||
let lookup_dns = group.lookup_dns.unwrap_or(false);
|
||||
|
||||
log::info!("Listen at: {}, tls: {}, lookup_dns: {}", listen_address, group.tls.is_some(), lookup_dns);
|
||||
|
||||
match &group.tls {
|
||||
@@ -57,7 +64,7 @@ fn build_services(server: &Server, proxy_config: &ProxyConfig) -> Vec<Box<dyn Se
|
||||
&server.configuration,
|
||||
&listen_address,
|
||||
lookup_dns,
|
||||
host_configs,
|
||||
host_config_map,
|
||||
)));
|
||||
}
|
||||
Some(proxy_tls) => {
|
||||
@@ -66,7 +73,7 @@ fn build_services(server: &Server, proxy_config: &ProxyConfig) -> Vec<Box<dyn Se
|
||||
&listen_address,
|
||||
lookup_dns,
|
||||
proxy_tls,
|
||||
host_configs,
|
||||
host_config_map,
|
||||
)));
|
||||
}
|
||||
}
|
||||
@@ -74,16 +81,17 @@ fn build_services(server: &Server, proxy_config: &ProxyConfig) -> Vec<Box<dyn Se
|
||||
services
|
||||
}
|
||||
|
||||
fn build_host_configs(group: &ProxyGroup) -> Vec<HostConfig> {
|
||||
let mut host_configs = vec![];
|
||||
fn build_host_config_map(group: &ProxyGroup) -> HashMap<String, HostConfig> {
|
||||
let mut host_config_map = HashMap::new();
|
||||
if let Some(proxy_map) = &group.proxy_map {
|
||||
for (hostname, proxy_item) in proxy_map {
|
||||
host_configs.push(HostConfig {
|
||||
let host_config = HostConfig {
|
||||
proxy_addr: proxy_item.address.clone(),
|
||||
proxy_tls: proxy_item.tls.unwrap_or(false),
|
||||
proxy_hostname: proxy_item.sni.clone().unwrap_or_else(|| hostname.clone()),
|
||||
});
|
||||
proxy_servername: proxy_item.sni.clone().unwrap_or_else(|| hostname.clone()),
|
||||
};
|
||||
host_config_map.insert(hostname.to_string(), host_config);
|
||||
}
|
||||
}
|
||||
host_configs
|
||||
host_config_map
|
||||
}
|
||||
Reference in New Issue
Block a user