use std::net::IpAddr;
use std::num::NonZeroU32;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use ipnetwork::IpNetwork;
use serde::Deserialize;
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
pub vip: VirtualIp,
pub etcd: Etcd,
pub healthcheck: Healthcheck,
pub monitoring: Option<Monitoring>,
}
#[derive(Deserialize)]
pub struct VirtualIp {
pub cidr: IpNetwork,
pub network_link: String,
pub broadcast_period_seconds: NonZeroU32,
}
#[derive(Deserialize)]
pub struct Monitoring {
pub ip: IpAddr,
pub port: u16,
}
#[derive(Deserialize)]
pub struct Etcd {
pub endpoints: Vec<String>,
pub retry: Retry,
pub keep_alive: KeepAlive,
pub lease: Lease,
pub tls: Option<Tls>,
}
#[derive(Deserialize)]
pub struct KeepAlive {
pub period_seconds: NonZeroU32,
pub timeout_seconds: NonZeroU32,
}
#[derive(Deserialize, Copy, Clone)]
pub struct Retry {
pub backoff_seconds: u32,
pub max_attempts: NonZeroU32,
}
#[derive(Deserialize)]
pub struct Lease {
pub time_to_live_seconds: NonZeroU32,
pub keep_alive_period_seconds: NonZeroU32,
}
#[derive(Deserialize)]
pub struct Healthcheck {
pub period_seconds: NonZeroU32,
pub timeout_seconds: NonZeroU32,
pub success_threshold: NonZeroU32,
pub failure_threshold: NonZeroU32,
pub checks: Vec<Check>,
}
#[derive(Deserialize)]
pub struct Check {
pub url: String,
pub headers: Option<Vec<Header>>,
pub tls: Option<Tls>,
}
#[derive(Deserialize)]
pub struct Header {
pub name: String,
pub value: String,
}
#[derive(Deserialize)]
pub struct Tls {
pub server_ca_certificate: Option<PathBuf>,
pub client_certificates: Option<PathBuf>,
pub client_private_key: Option<PathBuf>,
}
impl Config {
pub fn new(path: &Path) -> Result<Config> {
let config_file =
std::fs::read_to_string(path).with_context(|| format!("Failed to read config file {}", path.display()))?;
let config: Config =
toml::from_str(&config_file).with_context(|| format!("Failed to parse config file {}", path.display()))?;
Ok(config)
}
}