use crate::error::*; use serde::Deserialize; use std::{collections::HashMap, fs}; #[derive(Deserialize, Debug, Default)] pub struct ConfigToml { pub listen_port: Option, pub listen_port_tls: Option, pub apps: 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 upstream: Vec, } #[derive(Deserialize, Debug, Default)] pub struct UpstreamOption { pub location: String, pub tls: Option, } impl UpstreamOption { pub fn to_uri(&self) -> Result { let mut scheme = "http"; if let Some(t) = self.tls { if t { scheme = "https"; } } let location = format!("{}://{}", scheme, self.location); location.parse::().map_err(|e| anyhow!("{}", e)) } } impl ConfigToml { pub fn new(config_file: &str) -> Result { let config_str = if let Ok(s) = fs::read_to_string(config_file) { s } else { bail!("Failed to read config file"); }; let parsed: Result = toml::from_str(&config_str) .map_err(|e: toml::de::Error| anyhow!("Failed to parse toml config: {:?}", e)); parsed } }