use crate::{backend::Upstream, error::*}; use rustc_hash::FxHashMap as HashMap; use serde::Deserialize; use std::fs; #[derive(Deserialize, Debug, Default)] pub struct ConfigToml { pub listen_port: Option, pub listen_port_tls: Option, pub listen_ipv6: Option, pub max_concurrent_streams: Option, pub max_clients: Option, pub apps: Option, pub default_app: Option, pub experimental: Option, } #[cfg(feature = "http3")] #[derive(Deserialize, Debug, Default)] pub struct Http3Option { pub alt_svc_max_age: Option, pub request_max_body_size: Option, pub max_concurrent_connections: Option, pub max_concurrent_bidistream: Option, pub max_concurrent_unistream: Option, } #[derive(Deserialize, Debug, Default)] pub struct Experimental { #[cfg(feature = "http3")] pub h3: Option, pub ignore_sni_consistency: Option, } #[derive(Deserialize, Debug, Default)] pub struct Apps(pub HashMap); #[derive(Deserialize, Debug, Default)] pub struct Application { pub server_name: Option, pub reverse_proxy: Option>, pub tls: Option, } #[derive(Deserialize, Debug, Default)] pub struct TlsOption { pub tls_cert_path: Option, pub tls_cert_key_path: Option, pub https_redirection: Option, } #[derive(Deserialize, Debug, Default)] pub struct ReverseProxyOption { pub path: Option, pub replace_path: Option, pub upstream: Vec, pub upstream_options: Option>, } #[derive(Deserialize, Debug, Default)] pub struct UpstreamParams { pub location: String, pub tls: Option, } impl UpstreamParams { pub fn to_upstream(&self) -> Result { let mut scheme = "http"; if let Some(t) = self.tls { if t { scheme = "https"; } } let location = format!("{}://{}", scheme, self.location); Ok(Upstream { uri: location.parse::().map_err(|e| anyhow!("{}", e))?, }) } } impl ConfigToml { pub fn new(config_file: &str) -> Result { let config_str = fs::read_to_string(config_file).context("Failed to read config file")?; toml::from_str(&config_str).context("Failed to parse toml config") } }