//! Atlas's configuration: `atlas.toml`, then `CARTO_ATLAS_*` from the environment, //! then the command line. //! //! **Unknown keys are an error.** A misspelt key that silently does nothing is how //! a service ends up running on defaults nobody chose — OpenSimulator's `.ini` //! sprawl is the cautionary tale `docs/notes/atlas.md` cites, and the shot //! manifest here already refuses unknown keys for the same reason. use std::net::SocketAddr; use std::path::{Path, PathBuf}; use figment::providers::{Env, Format, Serialized, Toml}; use figment::Figment; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields, default)] pub struct AtlasConfig { /// Where the HTTP surface listens. Loopback by default: a deployment puts /// Caddy in front of it, as it does the simulator's HTTP listener. pub listen: SocketAddr, /// Where built and cached data lives (`terrain/{z}/{x}/{y}.ctt`, …). pub data_dir: PathBuf, /// Requests to PDOK allowed in flight at once. PDOK is a shared public /// service and we are a guest on it; the client's own height layer uses 3. /// The pipelines take the same number as their per-dataset ceiling /// (`pipeline::Ctx::upstream`): pages, tiles and 3D blocks fetched at once. pub upstream_concurrency: usize, /// The tree the map data pipelines write, which Caddy serves as /// `3dbag.cartopolis.org` and the web UI reads coverage from. pub site_dir: PathBuf, /// The simulator's HTTP surface, asked whether a web UI bearer is an admin. pub admin_api: String, /// Where the satellite catalogue is mirrored from (`satellites`). Empty turns /// the refresh off; a copy already on disk is still served. pub satellites_source: String, /// Whether the plans feed is rebuilt from its sources (`plans`). Off, a /// feed already on disk is still served. pub plans_refresh: bool, } impl Default for AtlasConfig { fn default() -> Self { Self { listen: SocketAddr::from(([127, 0, 0, 1], 4749)), data_dir: PathBuf::from("data/atlas"), upstream_concurrency: 3, site_dir: PathBuf::from("data/atlas/site"), admin_api: "http://127.0.0.1:4748".to_string(), satellites_source: crate::satellites::DEFAULT_SOURCE.to_string(), plans_refresh: true, } } } impl AtlasConfig { /// Defaults, overlaid by the TOML file (if it exists), overlaid by the /// environment. pub fn load(file: &Path) -> anyhow::Result { Self::from_figment( Figment::from(Serialized::defaults(Self::default())) .merge(Toml::file(file)) .merge(Env::prefixed("CARTO_ATLAS_")), ) } /// [`Self::load`] from one table of a file that holds other settings too: /// `cartopolis-server` keeps the simulator's at the top of its /// `cartopolis.toml` and atlas's under `[atlas]`. Unknown keys inside the /// table are still refused. pub fn load_table(file: &Path, table: &str) -> anyhow::Result { Self::from_figment( Figment::from(Serialized::defaults(Self::default())) .merge(Figment::from(Toml::file(file)).focus(table)) .merge(Env::prefixed("CARTO_ATLAS_")), ) } fn from_figment(figment: Figment) -> anyhow::Result { let config: Self = figment.extract()?; anyhow::ensure!( config.upstream_concurrency >= 1, "upstream_concurrency must be at least 1" ); Ok(config) } } #[cfg(test)] mod tests { use super::*; fn from_toml(text: &str) -> anyhow::Result { AtlasConfig::from_figment( Figment::from(Serialized::defaults(AtlasConfig::default())).merge(Toml::string(text)), ) } #[test] fn a_misspelt_key_is_refused() { let err = from_toml("lisen = \"0.0.0.0:80\"").unwrap_err().to_string(); assert!( err.contains("lisen"), "the error did not name the key: {err}" ); } #[test] fn a_file_overrides_only_what_it_names() { let c = from_toml("upstream_concurrency = 2").unwrap(); assert_eq!(c.upstream_concurrency, 2); assert_eq!(c.listen, AtlasConfig::default().listen); } #[test] fn zero_upstream_slots_is_refused() { assert!(from_toml("upstream_concurrency = 0").is_err()); } } #[cfg(test)] mod table_tests { use super::*; #[test] fn a_table_of_a_shared_file_is_read_and_the_rest_left_alone() { let dir = tempfile::tempdir().unwrap(); let file = dir.path().join("cartopolis.toml"); std::fs::write( &file, "listen = \"127.0.0.1:4747\"\n[atlas]\ndata_dir = \"/srv/atlas\"\n", ) .unwrap(); let c = AtlasConfig::load_table(&file, "atlas").unwrap(); assert_eq!(c.data_dir, PathBuf::from("/srv/atlas")); assert_eq!( c.listen, AtlasConfig::default().listen, "the top level is not atlas's" ); std::fs::write(&file, "[atlas]\nlisen = \"0.0.0.0:80\"\n").unwrap(); assert!(AtlasConfig::load_table(&file, "atlas").is_err()); } }