aien-edge
aien-dev/aien-edge
Brought by drakestapleton
| 1 | //! TOML configuration for aien-edge. |
| 2 | |
| 3 | use serde::Deserialize; |
| 4 | use std::path::PathBuf; |
| 5 | |
| 6 | #[derive(Debug, Clone, Deserialize)] |
| 7 | #[serde(deny_unknown_fields)] |
| 8 | pub struct Config { |
| 9 | /// Address for plain HTTP. Serves redirects to HTTPS when `tls` is set, content otherwise. |
| 10 | #[serde(default = "default_http")] |
| 11 | pub http_listen: String, |
| 12 | /// TLS settings. Omit to run plain HTTP only (local testing, or behind a tunnel). |
| 13 | pub tls: Option<Tls>, |
| 14 | /// Upper bound on concurrent connections across both listeners. |
| 15 | #[serde(default = "default_max_conns")] |
| 16 | pub max_connections: usize, |
| 17 | #[serde(rename = "site", default)] |
| 18 | pub sites: Vec<Site>, |
| 19 | } |
| 20 | |
| 21 | #[derive(Debug, Clone, Deserialize)] |
| 22 | #[serde(deny_unknown_fields)] |
| 23 | pub struct Tls { |
| 24 | #[serde(default = "default_https")] |
| 25 | pub https_listen: String, |
| 26 | /// ACME account contact, for example "aien@aienos.com". |
| 27 | pub contact: String, |
| 28 | /// Directory where certificates and the ACME account key are cached. |
| 29 | pub cache_dir: PathBuf, |
| 30 | /// false uses the Let's Encrypt staging directory. |
| 31 | #[serde(default)] |
| 32 | pub production: bool, |
| 33 | } |
| 34 | |
| 35 | #[derive(Debug, Clone, Deserialize)] |
| 36 | #[serde(deny_unknown_fields)] |
| 37 | pub struct Site { |
| 38 | /// Canonical host, for example "www.aienos.com". |
| 39 | pub host: String, |
| 40 | /// Hosts that permanently redirect to `host`, for example "aienos.com". |
| 41 | #[serde(default)] |
| 42 | pub redirect_from: Vec<String>, |
| 43 | /// Directory holding the built site. |
| 44 | pub root: PathBuf, |
| 45 | /// Serve index.html for unknown paths (single page apps). Otherwise 404.html with status 404. |
| 46 | #[serde(default)] |
| 47 | pub spa: bool, |
| 48 | } |
| 49 | |
| 50 | fn default_http() -> String { |
| 51 | "[::]:80".into() |
| 52 | } |
| 53 | fn default_https() -> String { |
| 54 | "[::]:443".into() |
| 55 | } |
| 56 | fn default_max_conns() -> usize { |
| 57 | 2048 |
| 58 | } |
| 59 | |
| 60 | impl Config { |
| 61 | pub fn load(path: &std::path::Path) -> Result<Self, String> { |
| 62 | let text = |
| 63 | std::fs::read_to_string(path).map_err(|e| format!("read {}: {e}", path.display()))?; |
| 64 | let cfg: Config = |
| 65 | toml::from_str(&text).map_err(|e| format!("parse {}: {e}", path.display()))?; |
| 66 | if cfg.sites.is_empty() { |
| 67 | return Err("config has no [[site]] entries".into()); |
| 68 | } |
| 69 | Ok(cfg) |
| 70 | } |
| 71 | |
| 72 | /// Every host name that needs a certificate. |
| 73 | pub fn all_hosts(&self) -> Vec<String> { |
| 74 | let mut hosts: Vec<String> = self |
| 75 | .sites |
| 76 | .iter() |
| 77 | .flat_map(|s| std::iter::once(s.host.clone()).chain(s.redirect_from.iter().cloned())) |
| 78 | .collect(); |
| 79 | hosts.sort(); |
| 80 | hosts.dedup(); |
| 81 | hosts |
| 82 | } |
| 83 | } |