Dizey.toml is the one place the process is configured
Browse files at this commit70cc24c69c8da177b43311322ef5300625934f32Parent: 667d04c- feat
- dizey.toml is the one place the process is configured
6 changed files +217 -147
- .gitignore +1 -0
- Cargo.lock +9 -0
- Cargo.toml +1 -0
- crates/dizey-core/Cargo.toml +1 -0
- crates/dizey-core/src/config.rs +199 -142
- crates/dizey-web/src/main.rs +6 -5
.gitignoremodified+1 -0
| @@ -5,3 +5,4 @@ | |||
| 5 | *.db-* | 5 | *.db-* |
| 6 | .env | 6 | .env |
| 7 | dizey.key | 7 | dizey.key |
| 8 | +dizey.toml | ||
Cargo.lockmodified+9 -0
| @@ -980,6 +980,7 @@ dependencies = [ | |||
| 980 | "thiserror", | 980 | "thiserror", |
| 981 | "time", | 981 | "time", |
| 982 | "tokio", | 982 | "tokio", |
| 983 | + "toml", | ||
| 983 | "turso", | 984 | "turso", |
| 984 | "uuid", | 985 | "uuid", |
| 985 | ] | 986 | ] |
| @@ -4101,10 +4102,12 @@ version = "1.1.4+spec-1.1.0" | |||
| 4101 | source = "registry+https://github.com/rust-lang/crates.io-index" | 4102 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 4102 | checksum = "3aace63f4bbcdfc2c965b059de67119c89c4017a70d633be6c104910f67056f5" | 4103 | checksum = "3aace63f4bbcdfc2c965b059de67119c89c4017a70d633be6c104910f67056f5" |
| 4103 | dependencies = [ | 4104 | dependencies = [ |
| 4105 | + "indexmap", | ||
| 4104 | "serde_core", | 4106 | "serde_core", |
| 4105 | "serde_spanned", | 4107 | "serde_spanned", |
| 4106 | "toml_datetime", | 4108 | "toml_datetime", |
| 4107 | "toml_parser", | 4109 | "toml_parser", |
| 4110 | + "toml_writer", | ||
| 4108 | "winnow", | 4111 | "winnow", |
| 4109 | ] | 4112 | ] |
| 4110 | 4113 | ||
| @@ -4126,6 +4129,12 @@ dependencies = [ | |||
| 4126 | "winnow", | 4129 | "winnow", |
| 4127 | ] | 4130 | ] |
| 4128 | 4131 | ||
| 4132 | +[[package]] | ||
| 4133 | +name = "toml_writer" | ||
| 4134 | +version = "1.1.2+spec-1.1.0" | ||
| 4135 | +source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 4136 | +checksum = "7d56353a2a665ad0f41a421187180aab746c8c325620617ad883a99a1cbe66d2" | ||
| 4137 | + | ||
| 4129 | [[package]] | 4138 | [[package]] |
| 4130 | name = "tower" | 4139 | name = "tower" |
| 4131 | version = "0.5.3" | 4140 | version = "0.5.3" |
Cargo.tomlmodified+1 -0
| @@ -23,6 +23,7 @@ sha2 = "0.11.0" | |||
| 23 | subtle = "2.6.1" | 23 | subtle = "2.6.1" |
| 24 | chacha20poly1305 = "0.10.1" | 24 | chacha20poly1305 = "0.10.1" |
| 25 | base64 = "0.22.1" | 25 | base64 = "0.22.1" |
| 26 | +toml = "1" | ||
| 26 | 27 | ||
| 27 | [profile.release] | 28 | [profile.release] |
| 28 | codegen-units = 1 | 29 | codegen-units = 1 |
crates/dizey-core/Cargo.tomlmodified+1 -0
| @@ -35,6 +35,7 @@ serde.workspace = true | |||
| 35 | serde_json.workspace = true | 35 | serde_json.workspace = true |
| 36 | thiserror.workspace = true | 36 | thiserror.workspace = true |
| 37 | time.workspace = true | 37 | time.workspace = true |
| 38 | +toml.workspace = true | ||
| 38 | turso = { workspace = true, optional = true } | 39 | turso = { workspace = true, optional = true } |
| 39 | uuid = { workspace = true, optional = true } | 40 | uuid = { workspace = true, optional = true } |
| 40 | 41 | ||
crates/dizey-core/src/config.rsmodified+199 -142
| @@ -1,30 +1,53 @@ | |||
| 1 | -//! Every environment variable Dizey reads, in one place. | 1 | +//! Everything Dizey reads from `dizey.toml`, in one place. |
| 2 | //! | 2 | //! |
| 3 | -//! Nothing here has a silent default. A missing variable stops the boot and | 3 | +//! Nothing here has a silent default once the file exists. A key that is |
| 4 | -//! says which name is missing, because the alternative is worse than not | 4 | +//! missing, empty or unusable stops the boot and says which key and which |
| 5 | -//! starting: a wrong `DIZEY_DATABASE` does not mean "no data", it means a | 5 | +//! file, because the alternative is worse than not starting: a wrong |
| 6 | -//! second Dizey quietly writing a different file while everyone believes they | 6 | +//! `database` does not mean "no data", it means a second Dizey quietly |
| 7 | -//! are looking at the same board — and Turso is single-writer, so the two are | 7 | +//! writing a different file while everyone believes they are looking at the |
| 8 | -//! not even reconcilable afterwards. A wrong `DIZEY_BASE_URL` mails people a | 8 | +//! same board — and Turso is single-writer, so the two are not even |
| 9 | -//! sign-in link pointing at a host that is not us. | 9 | +//! reconcilable afterwards. A wrong `base_url` mails people a sign-in link |
| 10 | +//! pointing at a host that is not us. | ||
| 10 | //! | 11 | //! |
| 11 | -//! Development still needs to be one command, so `DIZEY_DEV=1` is the opt-in | 12 | +//! Development still needs to be one command, so the *absence* of |
| 12 | -//! that turns the defaults back on. It is opt-in on purpose: a deployment that | 13 | +//! `dizey.toml` is the opt-in that takes the development defaults: the app |
| 13 | -//! forgets it fails loudly, and a deployment that sets it did so deliberately. | 14 | +//! writes the file itself, with those defaults in it and comments saying |
| 15 | +//! what each key does, and starts. That is opt-in on purpose too — it only | ||
| 16 | +//! ever happens once, because the second boot finds the file it wrote the | ||
| 17 | +//! first time and reads it like any other. A real deployment is handed the | ||
| 18 | +//! same file and edits it; it never writes itself over a deployment's | ||
| 19 | +//! choices, because it only writes when the file is not there at all. | ||
| 14 | //! | 20 | //! |
| 15 | -//! Whatever is finally resolved is printed once at startup — the database path | 21 | +//! Whatever is finally resolved is printed once at startup — the database |
| 16 | -//! absolute, the base URL as it will appear in mail — so "which file are we | 22 | +//! path absolute, the base URL as it will appear in mail — so "which file |
| 17 | -//! on" is answered by the log rather than by someone's memory. | 23 | +//! are we on" is answered by the log rather than by someone's memory. |
| 18 | //! | 24 | //! |
| 19 | -//! The sender is not here. Host, port, username, password and from-address are | 25 | +//! The sender is not here. Host, port, username, password and from-address |
| 20 | -//! workspace settings an admin writes on the Settings screen, so that changing | 26 | +//! are workspace settings an admin writes on the Settings screen, so that |
| 21 | -//! where mail goes out through does not need a shell on the box and a restart. | 27 | +//! changing where mail goes out through does not need a shell on the box and |
| 28 | +//! a restart. | ||
| 22 | 29 | ||
| 30 | +use serde::Deserialize; | ||
| 23 | use std::fmt; | 31 | use std::fmt; |
| 24 | use std::path::{Path, PathBuf}; | 32 | use std::path::{Path, PathBuf}; |
| 25 | 33 | ||
| 26 | -/// Every variable the app reads, in the order the report prints them. | 34 | +/// The name of the file `Config::load` reads and, failing that, writes. |
| 27 | -pub const VARIABLES: &[&str] = &["DIZEY_DEV", "DIZEY_DATABASE", "DIZEY_BASE_URL"]; | 35 | +const FILE_NAME: &str = "dizey.toml"; |
| 36 | + | ||
| 37 | +/// What a freshly written `dizey.toml` says, comments included. Development | ||
| 38 | +/// defaults, so that a plain `dizey` in an empty directory is one command. | ||
| 39 | +const DEVELOPMENT_DEFAULTS: &str = r#"# Where the one database file lives. One process holds it. | ||
| 40 | +database = "dizey.db" | ||
| 41 | +# The origin sign-in links in mail point at. | ||
| 42 | +base_url = "http://127.0.0.1:3000" | ||
| 43 | +"#; | ||
| 44 | + | ||
| 45 | +/// The shape of `dizey.toml`, before the values are checked. | ||
| 46 | +#[derive(Deserialize)] | ||
| 47 | +struct Toml { | ||
| 48 | + database: Option<String>, | ||
| 49 | + base_url: Option<String>, | ||
| 50 | +} | ||
| 28 | 51 | ||
| 29 | /// What the process needs to know before it opens a socket. | 52 | /// What the process needs to know before it opens a socket. |
| 30 | #[derive(Clone, Debug, PartialEq, Eq)] | 53 | #[derive(Clone, Debug, PartialEq, Eq)] |
| @@ -33,42 +56,44 @@ pub struct Config { | |||
| 33 | pub database: PathBuf, | 56 | pub database: PathBuf, |
| 34 | /// The origin links in mail point at, with no trailing slash. | 57 | /// The origin links in mail point at, with no trailing slash. |
| 35 | pub base_url: String, | 58 | pub base_url: String, |
| 36 | - /// Whether the development defaults were taken. | 59 | + /// Whether `dizey.toml` did not exist and was just written with the |
| 37 | - pub dev: bool, | 60 | + /// development defaults this boot. |
| 61 | + pub defaulted: bool, | ||
| 38 | } | 62 | } |
| 39 | 63 | ||
| 40 | /// Why the process is not starting. | 64 | /// Why the process is not starting. |
| 41 | #[derive(Clone, Debug, PartialEq, Eq)] | 65 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 42 | pub enum ConfigError { | 66 | pub enum ConfigError { |
| 43 | - /// Named variables are unset and have no default outside development. | 67 | + /// The file exists but is not valid TOML. |
| 44 | - Missing(Vec<&'static str>), | 68 | + Unparseable { why: String }, |
| 45 | - /// A variable is set to something the app cannot use. | 69 | + /// A key is missing or set to an empty value. |
| 46 | - Invalid { variable: &'static str, why: String }, | 70 | + Missing(&'static str), |
| 71 | + /// A key is set to something the app cannot use. | ||
| 72 | + Invalid { key: &'static str, why: String }, | ||
| 73 | + /// The file could not be read for a reason other than "it is not there" | ||
| 74 | + /// (permissions, for instance), or the default could not be written. | ||
| 75 | + Io(String), | ||
| 47 | } | 76 | } |
| 48 | 77 | ||
| 49 | impl fmt::Display for ConfigError { | 78 | impl fmt::Display for ConfigError { |
| 50 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 79 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 51 | match self { | 80 | match self { |
| 52 | - ConfigError::Missing(names) => { | 81 | + ConfigError::Unparseable { why } => { |
| 53 | - write!(f, "not starting: ")?; | 82 | + write!(f, "not starting: {FILE_NAME} is not valid TOML — {why}") |
| 54 | - let them = if names.len() == 1 { | 83 | + } |
| 55 | - write!(f, "{} is not set", names[0])?; | 84 | + ConfigError::Missing(key) => { |
| 56 | - "it" | ||
| 57 | - } else { | ||
| 58 | - write!(f, "{} are not set", names.join(", "))?; | ||
| 59 | - "them" | ||
| 60 | - }; | ||
| 61 | write!( | 85 | write!( |
| 62 | f, | 86 | f, |
| 63 | - ". Set {them}, or set DIZEY_DEV=1 to take the development defaults." | 87 | + "not starting: {FILE_NAME} has no {key}. Add it, or delete {FILE_NAME} to take the development defaults." |
| 64 | ) | 88 | ) |
| 65 | } | 89 | } |
| 66 | - ConfigError::Invalid { variable, why } => { | 90 | + ConfigError::Invalid { key, why } => { |
| 67 | write!( | 91 | write!( |
| 68 | f, | 92 | f, |
| 69 | - "not starting: {variable} is set to something unusable — {why}" | 93 | + "not starting: {FILE_NAME}'s {key} is set to something unusable — {why}" |
| 70 | ) | 94 | ) |
| 71 | } | 95 | } |
| 96 | + ConfigError::Io(why) => write!(f, "not starting: {why}"), | ||
| 72 | } | 97 | } |
| 73 | } | 98 | } |
| 74 | } | 99 | } |
| @@ -76,49 +101,58 @@ impl fmt::Display for ConfigError { | |||
| 76 | impl std::error::Error for ConfigError {} | 101 | impl std::error::Error for ConfigError {} |
| 77 | 102 | ||
| 78 | impl Config { | 103 | impl Config { |
| 79 | - /// Reads the environment. Every failure names the variable behind it. | 104 | + /// Reads `dizey.toml` from the current directory, writing it with the |
| 80 | - pub fn from_env() -> Result<Config, ConfigError> { | 105 | + /// development defaults first if it is not there. |
| 81 | - Config::read(|name| std::env::var(name).ok()) | 106 | + pub fn load() -> Result<Config, ConfigError> { |
| 107 | + Config::load_from(Path::new(".")) | ||
| 82 | } | 108 | } |
| 83 | 109 | ||
| 84 | - /// The same reading, against any source — which is how it is tested | 110 | + /// The same reading, against any directory — which is how it is tested |
| 85 | - /// without a test being able to disturb another test's environment. | 111 | + /// without a test being able to disturb another test's working |
| 86 | - pub fn read(source: impl Fn(&str) -> Option<String>) -> Result<Config, ConfigError> { | 112 | + /// directory, or another test's `dizey.toml`. |
| 87 | - let value = |name: &str| { | 113 | + pub fn load_from(dir: &Path) -> Result<Config, ConfigError> { |
| 88 | - source(name).and_then(|raw| { | 114 | + let path = dir.join(FILE_NAME); |
| 89 | - let trimmed = raw.trim().to_string(); | 115 | + match std::fs::read_to_string(&path) { |
| 90 | - (!trimmed.is_empty()).then_some(trimmed) | 116 | + Ok(text) => Config::parse(&text, dir, false), |
| 91 | - }) | 117 | + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { |
| 92 | - }; | 118 | + std::fs::write(&path, DEVELOPMENT_DEFAULTS).map_err(|err| { |
| 93 | - let dev = matches!(value("DIZEY_DEV").as_deref(), Some("1" | "true" | "yes")); | 119 | + ConfigError::Io(format!("could not write {}: {err}", path.display())) |
| 94 | - | 120 | + })?; |
| 95 | - let mut missing = Vec::new(); | 121 | + println!( |
| 96 | - let database = value("DIZEY_DATABASE").or_else(|| dev.then(|| "dizey.db".to_string())); | 122 | + "dizey wrote {FILE_NAME} with development defaults — edit it for a real deployment" |
| 97 | - if database.is_none() { | 123 | + ); |
| 98 | - missing.push("DIZEY_DATABASE"); | 124 | + Config::parse(DEVELOPMENT_DEFAULTS, dir, true) |
| 99 | - } | 125 | + } |
| 100 | - let base_url = | 126 | + Err(err) => Err(ConfigError::Io(format!( |
| 101 | - value("DIZEY_BASE_URL").or_else(|| dev.then(|| "http://127.0.0.1:3000".to_string())); | 127 | + "could not read {}: {err}", |
| 102 | - if base_url.is_none() { | 128 | + path.display() |
| 103 | - missing.push("DIZEY_BASE_URL"); | 129 | + ))), |
| 104 | - } | ||
| 105 | - if !missing.is_empty() { | ||
| 106 | - return Err(ConfigError::Missing(missing)); | ||
| 107 | } | 130 | } |
| 131 | + } | ||
| 132 | + | ||
| 133 | + /// The reading itself, against a string — which is how it is tested | ||
| 134 | + /// without a test being able to disturb another test's filesystem. | ||
| 135 | + pub fn parse(text: &str, dir: &Path, defaulted: bool) -> Result<Config, ConfigError> { | ||
| 136 | + let toml: Toml = | ||
| 137 | + toml::from_str(text).map_err(|err| ConfigError::Unparseable { why: err.to_string() })?; | ||
| 138 | + | ||
| 139 | + let value = |raw: Option<String>| raw.filter(|value| !value.trim().is_empty()); | ||
| 140 | + | ||
| 141 | + let database = value(toml.database).ok_or(ConfigError::Missing("database"))?; | ||
| 142 | + let base_url = value(toml.base_url).ok_or(ConfigError::Missing("base_url"))?; | ||
| 108 | 143 | ||
| 109 | - let base_url = base_url.expect("checked above"); | ||
| 110 | if !(base_url.starts_with("http://") || base_url.starts_with("https://")) { | 144 | if !(base_url.starts_with("http://") || base_url.starts_with("https://")) { |
| 111 | return Err(ConfigError::Invalid { | 145 | return Err(ConfigError::Invalid { |
| 112 | - variable: "DIZEY_BASE_URL", | 146 | + key: "base_url", |
| 113 | why: format!("{base_url:?} is not an http:// or https:// origin"), | 147 | why: format!("{base_url:?} is not an http:// or https:// origin"), |
| 114 | }); | 148 | }); |
| 115 | } | 149 | } |
| 116 | let base_url = base_url.trim_end_matches('/').to_string(); | 150 | let base_url = base_url.trim_end_matches('/').to_string(); |
| 117 | 151 | ||
| 118 | Ok(Config { | 152 | Ok(Config { |
| 119 | - database: absolute(Path::new(&database.expect("checked above"))), | 153 | + database: absolute(dir, Path::new(&database)), |
| 120 | base_url, | 154 | base_url, |
| 121 | - dev, | 155 | + defaulted, |
| 122 | }) | 156 | }) |
| 123 | } | 157 | } |
| 124 | 158 | ||
| @@ -129,22 +163,26 @@ impl Config { | |||
| 129 | format!("base url {}", self.base_url), | 163 | format!("base url {}", self.base_url), |
| 130 | ]; | 164 | ]; |
| 131 | lines.push("mail the sender is in Settings, not here".to_string()); | 165 | lines.push("mail the sender is in Settings, not here".to_string()); |
| 132 | - if self.dev { | 166 | + if self.defaulted { |
| 133 | - lines.push("dev DIZEY_DEV=1, development defaults taken".to_string()); | 167 | + lines.push(format!( |
| 168 | + "dev {FILE_NAME} did not exist, development defaults written and taken" | ||
| 169 | + )); | ||
| 134 | } | 170 | } |
| 135 | lines | 171 | lines |
| 136 | } | 172 | } |
| 137 | } | 173 | } |
| 138 | 174 | ||
| 139 | - | ||
| 140 | /// An absolute path for a file that may not exist yet: the directory is | 175 | /// An absolute path for a file that may not exist yet: the directory is |
| 141 | -/// resolved, the file name is kept as written. | 176 | +/// resolved against `base` (the directory `dizey.toml` was read from), the |
| 142 | -fn absolute(path: &Path) -> PathBuf { | 177 | +/// file name is kept as written. |
| 178 | +fn absolute(base: &Path, path: &Path) -> PathBuf { | ||
| 143 | if path.is_absolute() { | 179 | if path.is_absolute() { |
| 144 | return path.to_path_buf(); | 180 | return path.to_path_buf(); |
| 145 | } | 181 | } |
| 146 | - let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")); | 182 | + let base = base |
| 147 | - let joined = cwd.join(path); | 183 | + .canonicalize() |
| 184 | + .unwrap_or_else(|_| std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))); | ||
| 185 | + let joined = base.join(path); | ||
| 148 | match (joined.parent(), joined.file_name()) { | 186 | match (joined.parent(), joined.file_name()) { |
| 149 | (Some(parent), Some(name)) => match parent.canonicalize() { | 187 | (Some(parent), Some(name)) => match parent.canonicalize() { |
| 150 | Ok(parent) => parent.join(name), | 188 | Ok(parent) => parent.join(name), |
| @@ -157,54 +195,83 @@ fn absolute(path: &Path) -> PathBuf { | |||
| 157 | #[cfg(test)] | 195 | #[cfg(test)] |
| 158 | mod tests { | 196 | mod tests { |
| 159 | use super::*; | 197 | use super::*; |
| 160 | - use std::collections::HashMap; | ||
| 161 | 198 | ||
| 162 | - /// An environment, without touching the process's own. | 199 | + /// A scratch directory, cleaned up when the test ends, so `load_from` can |
| 163 | - fn env(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> { | 200 | + /// be exercised without touching the process's own working directory. |
| 164 | - let map: HashMap<String, String> = pairs | 201 | + fn scratch() -> PathBuf { |
| 165 | - .iter() | 202 | + let dir = std::env::temp_dir().join(format!("dizey-config-test-{}", uuid_like())); |
| 166 | - .map(|(name, value)| (name.to_string(), value.to_string())) | 203 | + std::fs::create_dir_all(&dir).unwrap(); |
| 167 | - .collect(); | 204 | + dir |
| 168 | - move |name: &str| map.get(name).cloned() | 205 | + } |
| 206 | + | ||
| 207 | + /// Good enough uniqueness for a scratch directory name; no need for a | ||
| 208 | + /// real UUID dependency just for this. | ||
| 209 | + fn uuid_like() -> u128 { | ||
| 210 | + std::time::SystemTime::now() | ||
| 211 | + .duration_since(std::time::UNIX_EPOCH) | ||
| 212 | + .unwrap() | ||
| 213 | + .as_nanos() | ||
| 214 | + ^ (std::process::id() as u128) << 64 | ||
| 169 | } | 215 | } |
| 170 | 216 | ||
| 171 | #[test] | 217 | #[test] |
| 172 | - fn an_empty_environment_names_what_is_missing_rather_than_guessing() { | 218 | + fn an_absent_file_is_written_with_development_defaults_and_taken() { |
| 173 | - let problem = Config::read(env(&[])).unwrap_err(); | 219 | + let dir = scratch(); |
| 174 | - let ConfigError::Missing(names) = &problem else { | 220 | + let config = Config::load_from(&dir).unwrap(); |
| 175 | - panic!("expected the missing names, got {problem:?}"); | 221 | + assert!(config.defaulted); |
| 176 | - }; | 222 | + assert!(config.database.is_absolute(), "{:?}", config.database); |
| 177 | - assert_eq!(names, &["DIZEY_DATABASE", "DIZEY_BASE_URL"]); | 223 | + assert!(config.database.ends_with("dizey.db")); |
| 178 | - let said = problem.to_string(); | 224 | + assert_eq!(config.base_url, "http://127.0.0.1:3000"); |
| 179 | - assert!(said.contains("DIZEY_DATABASE"), "{said}"); | 225 | + |
| 180 | - assert!(said.contains("DIZEY_DEV=1"), "{said}"); | 226 | + let written = std::fs::read_to_string(dir.join(FILE_NAME)).unwrap(); |
| 227 | + assert_eq!(written, DEVELOPMENT_DEFAULTS); | ||
| 228 | + | ||
| 229 | + // A second load reads the file it just wrote, identically. | ||
| 230 | + let again = Config::load_from(&dir).unwrap(); | ||
| 231 | + assert!(!again.defaulted); | ||
| 232 | + assert_eq!(again, Config { defaulted: false, ..config }); | ||
| 181 | } | 233 | } |
| 182 | 234 | ||
| 183 | #[test] | 235 | #[test] |
| 184 | - fn a_blank_value_is_not_a_value() { | 236 | + fn an_empty_string_is_not_a_value() { |
| 185 | - let problem = Config::read(env(&[ | 237 | + let problem = Config::parse( |
| 186 | - ("DIZEY_DATABASE", " "), | 238 | + "database = \" \"\nbase_url = \"https://dizey.sh\"\n", |
| 187 | - ("DIZEY_BASE_URL", "https://dizey.sh"), | 239 | + Path::new("."), |
| 188 | - ])) | 240 | + false, |
| 241 | + ) | ||
| 189 | .unwrap_err(); | 242 | .unwrap_err(); |
| 190 | - assert_eq!(problem, ConfigError::Missing(vec!["DIZEY_DATABASE"])); | 243 | + assert_eq!(problem, ConfigError::Missing("database")); |
| 191 | } | 244 | } |
| 192 | 245 | ||
| 193 | #[test] | 246 | #[test] |
| 194 | - fn the_dev_flag_is_the_only_thing_that_brings_defaults_back() { | 247 | + fn a_missing_key_names_itself_rather_than_guessing() { |
| 195 | - let config = Config::read(env(&[("DIZEY_DEV", "1")])).unwrap(); | 248 | + let problem = Config::parse("base_url = \"https://dizey.sh\"\n", Path::new("."), false) |
| 196 | - assert!(config.dev); | 249 | + .unwrap_err(); |
| 197 | - assert!(config.database.is_absolute(), "{:?}", config.database); | 250 | + assert_eq!(problem, ConfigError::Missing("database")); |
| 198 | - assert!(config.database.ends_with("dizey.db")); | 251 | + let said = problem.to_string(); |
| 199 | - assert_eq!(config.base_url, "http://127.0.0.1:3000"); | 252 | + assert!(said.contains("database"), "{said}"); |
| 253 | + assert!(said.contains("dizey.toml"), "{said}"); | ||
| 254 | + } | ||
| 255 | + | ||
| 256 | + #[test] | ||
| 257 | + fn unparseable_toml_names_the_file_not_a_stack_trace() { | ||
| 258 | + let problem = Config::parse("this is not toml at all {{{", Path::new("."), false) | ||
| 259 | + .unwrap_err(); | ||
| 260 | + assert!( | ||
| 261 | + matches!(problem, ConfigError::Unparseable { .. }), | ||
| 262 | + "{problem:?}" | ||
| 263 | + ); | ||
| 264 | + assert!(problem.to_string().contains("dizey.toml")); | ||
| 200 | } | 265 | } |
| 201 | 266 | ||
| 202 | #[test] | 267 | #[test] |
| 203 | fn a_relative_database_is_reported_absolute() { | 268 | fn a_relative_database_is_reported_absolute() { |
| 204 | - let config = Config::read(env(&[ | 269 | + let dir = scratch(); |
| 205 | - ("DIZEY_DATABASE", "state/dizey.db"), | 270 | + let config = Config::parse( |
| 206 | - ("DIZEY_BASE_URL", "https://dizey.sh"), | 271 | + "database = \"state/dizey.db\"\nbase_url = \"https://dizey.sh\"\n", |
| 207 | - ])) | 272 | + &dir, |
| 273 | + false, | ||
| 274 | + ) | ||
| 208 | .unwrap(); | 275 | .unwrap(); |
| 209 | assert!(config.database.is_absolute(), "{:?}", config.database); | 276 | assert!(config.database.is_absolute(), "{:?}", config.database); |
| 210 | assert!(config.database.ends_with("dizey.db")); | 277 | assert!(config.database.ends_with("dizey.db")); |
| @@ -217,16 +284,17 @@ mod tests { | |||
| 217 | 284 | ||
| 218 | #[test] | 285 | #[test] |
| 219 | fn a_base_url_that_is_not_an_origin_stops_the_boot() { | 286 | fn a_base_url_that_is_not_an_origin_stops_the_boot() { |
| 220 | - let problem = Config::read(env(&[ | 287 | + let problem = Config::parse( |
| 221 | - ("DIZEY_DATABASE", "/srv/dizey.db"), | 288 | + "database = \"/srv/dizey.db\"\nbase_url = \"dizey.sh\"\n", |
| 222 | - ("DIZEY_BASE_URL", "dizey.sh"), | 289 | + Path::new("."), |
| 223 | - ])) | 290 | + false, |
| 291 | + ) | ||
| 224 | .unwrap_err(); | 292 | .unwrap_err(); |
| 225 | assert!( | 293 | assert!( |
| 226 | matches!( | 294 | matches!( |
| 227 | problem, | 295 | problem, |
| 228 | ConfigError::Invalid { | 296 | ConfigError::Invalid { |
| 229 | - variable: "DIZEY_BASE_URL", | 297 | + key: "base_url", |
| 230 | .. | 298 | .. |
| 231 | } | 299 | } |
| 232 | ), | 300 | ), |
| @@ -236,48 +304,37 @@ mod tests { | |||
| 236 | 304 | ||
| 237 | #[test] | 305 | #[test] |
| 238 | fn the_base_url_keeps_no_trailing_slash_so_links_are_built_once() { | 306 | fn the_base_url_keeps_no_trailing_slash_so_links_are_built_once() { |
| 239 | - let config = Config::read(env(&[ | 307 | + let config = Config::parse( |
| 240 | - ("DIZEY_DATABASE", "/srv/dizey.db"), | 308 | + "database = \"/srv/dizey.db\"\nbase_url = \"https://dizey.sh/\"\n", |
| 241 | - ("DIZEY_BASE_URL", "https://dizey.sh/"), | 309 | + Path::new("."), |
| 242 | - ])) | 310 | + false, |
| 311 | + ) | ||
| 243 | .unwrap(); | 312 | .unwrap(); |
| 244 | assert_eq!(config.base_url, "https://dizey.sh"); | 313 | assert_eq!(config.base_url, "https://dizey.sh"); |
| 245 | } | 314 | } |
| 246 | 315 | ||
| 247 | - /// The sender used to be five environment variables. It is workspace | 316 | + /// The sender used to be environment variables read directly. It is |
| 248 | - /// settings now, so a stale `DIZEY_SMTP_PASSWORD` left in a unit file or a | 317 | + /// workspace settings now, so unrelated keys left in the file must do |
| 249 | - /// shell must do nothing at all — not half-configure a sender, not stop the | 318 | + /// nothing at all — not half-configure a sender, not stop the boot, and |
| 250 | - /// boot, and above all not quietly send through an account the Settings | 319 | + /// above all not quietly send through an account the Settings screen |
| 251 | - /// screen does not show. | 320 | + /// does not show. |
| 252 | #[test] | 321 | #[test] |
| 253 | - fn leftover_sender_variables_are_ignored_entirely() { | 322 | + fn unrelated_keys_are_ignored_entirely() { |
| 254 | - let config = Config::read(env(&[ | 323 | + let config = Config::parse( |
| 255 | - ("DIZEY_DATABASE", "/srv/dizey.db"), | 324 | + "database = \"/srv/dizey.db\"\nbase_url = \"https://dizey.sh\"\nsmtp_password = \"hunter2-and-then-some\"\n", |
| 256 | - ("DIZEY_BASE_URL", "https://dizey.sh"), | 325 | + Path::new("."), |
| 257 | - ("DIZEY_SMTP_HOST", "smtp.example.net"), | 326 | + false, |
| 258 | - ("DIZEY_SMTP_PORT", "587"), | 327 | + ) |
| 259 | - ("DIZEY_SMTP_USERNAME", "dizey"), | 328 | + .expect("an unrelated key in the file is not an error"); |
| 260 | - ("DIZEY_SMTP_PASSWORD", "hunter2-and-then-some"), | ||
| 261 | - ("DIZEY_MAIL_FROM", "board@dizey.sh"), | ||
| 262 | - ])) | ||
| 263 | - .expect("a stale sender in the environment is not an error"); | ||
| 264 | 329 | ||
| 265 | let report = config.report().join("\n"); | 330 | let report = config.report().join("\n"); |
| 266 | assert!( | 331 | assert!( |
| 267 | - !report.contains("hunter2-and-then-some") && !report.contains("smtp.example.net"), | 332 | + !report.contains("hunter2-and-then-some"), |
| 268 | - "the report read a variable it no longer honours: {report}" | 333 | + "the report read a key it no longer honours: {report}" |
| 269 | ); | 334 | ); |
| 270 | assert!( | 335 | assert!( |
| 271 | report.contains("the sender is in Settings"), | 336 | report.contains("the sender is in Settings"), |
| 272 | "the report should say where the sender lives: {report}" | 337 | "the report should say where the sender lives: {report}" |
| 273 | ); | 338 | ); |
| 274 | } | 339 | } |
| 275 | - | ||
| 276 | - #[test] | ||
| 277 | - fn every_variable_the_app_reads_is_named_in_one_list() { | ||
| 278 | - assert_eq!(VARIABLES.len(), 3, "a variable was added without a line here"); | ||
| 279 | - assert!(VARIABLES.contains(&"DIZEY_DEV")); | ||
| 280 | - assert!(VARIABLES.contains(&"DIZEY_DATABASE")); | ||
| 281 | - assert!(VARIABLES.contains(&"DIZEY_BASE_URL")); | ||
| 282 | - } | ||
| 283 | } | 340 | } |
crates/dizey-web/src/main.rsmodified+6 -5
| @@ -6,11 +6,12 @@ async fn main() { | |||
| 6 | use leptos::prelude::*; | 6 | use leptos::prelude::*; |
| 7 | use std::sync::Arc; | 7 | use std::sync::Arc; |
| 8 | 8 | ||
| 9 | - // Every variable the app reads is resolved here, before anything is | 9 | + // dizey.toml is read here, before anything is opened, and written with |
| 10 | - // opened. A missing one stops the boot with its name in the message: the | 10 | + // development defaults if it is not there yet. A broken key stops the |
| 11 | - // failure this prevents is not an empty database, it is a second Dizey | 11 | + // boot with its name in the message: the failure this prevents is not an |
| 12 | - // writing a different file while everyone believes they share a board. | 12 | + // empty database, it is a second Dizey writing a different file while |
| 13 | - let config = match dizey_core::Config::from_env() { | 13 | + // everyone believes they share a board. |
| 14 | + let config = match dizey_core::Config::load() { | ||
| 14 | Ok(config) => config, | 15 | Ok(config) => config, |
| 15 | Err(problem) => { | 16 | Err(problem) => { |
| 16 | eprintln!("dizey: {problem}"); | 17 | eprintln!("dizey: {problem}"); |