No variable defaults to something silently
Browse files at this commit2fa2cc8009f70a666b3d2adbd7fc3064e5aff873Parent: 4f9d174- fix
- no variable defaults to something silently
3 changed files +503 -2
- crates/dizey-core/src/config.rs +481 -0
- crates/dizey-core/src/lib.rs +4 -0
- crates/dizey-web/src/main.rs +18 -2
crates/dizey-core/src/config.rsadded+481 -0
| @@ -0,0 +1,481 @@ | |||
| 1 | +//! Every environment variable Dizey reads, in one place. | ||
| 2 | +//! | ||
| 3 | +//! Nothing here has a silent default. A missing variable stops the boot and | ||
| 4 | +//! says which name is missing, because the alternative is worse than not | ||
| 5 | +//! starting: a wrong `DIZEY_DATABASE` does not mean "no data", it means a | ||
| 6 | +//! second Dizey quietly writing a different file while everyone believes they | ||
| 7 | +//! are looking at the same board — and Turso is single-writer, so the two are | ||
| 8 | +//! not even reconcilable afterwards. A wrong `DIZEY_BASE_URL` mails people a | ||
| 9 | +//! sign-in link pointing at a host that is not us. | ||
| 10 | +//! | ||
| 11 | +//! Development still needs to be one command, so `DIZEY_DEV=1` is the opt-in | ||
| 12 | +//! that turns the defaults back on. It is opt-in on purpose: a deployment that | ||
| 13 | +//! forgets it fails loudly, and a deployment that sets it did so deliberately. | ||
| 14 | +//! | ||
| 15 | +//! Whatever is finally resolved is printed once at startup — the database path | ||
| 16 | +//! absolute, the base URL as it will appear in mail — so "which file are we | ||
| 17 | +//! on" is answered by the log rather than by someone's memory. Passwords are | ||
| 18 | +//! never among the things printed. | ||
| 19 | + | ||
| 20 | +use std::fmt; | ||
| 21 | +use std::path::{Path, PathBuf}; | ||
| 22 | + | ||
| 23 | +/// Every variable the app reads, in the order the report prints them. | ||
| 24 | +pub const VARIABLES: &[&str] = &[ | ||
| 25 | + "DIZEY_DEV", | ||
| 26 | + "DIZEY_DATABASE", | ||
| 27 | + "DIZEY_BASE_URL", | ||
| 28 | + "DIZEY_SMTP_HOST", | ||
| 29 | + "DIZEY_SMTP_PORT", | ||
| 30 | + "DIZEY_SMTP_USERNAME", | ||
| 31 | + "DIZEY_SMTP_PASSWORD", | ||
| 32 | + "DIZEY_MAIL_FROM", | ||
| 33 | +]; | ||
| 34 | + | ||
| 35 | +/// The five variables that carry a sender. They are all-or-nothing: a host | ||
| 36 | +/// without a from-address is not a half-configured sender, it is a mistake | ||
| 37 | +/// that would be discovered by a mail that never arrives. | ||
| 38 | +const MAIL_VARIABLES: &[&str] = &[ | ||
| 39 | + "DIZEY_SMTP_HOST", | ||
| 40 | + "DIZEY_SMTP_PORT", | ||
| 41 | + "DIZEY_SMTP_USERNAME", | ||
| 42 | + "DIZEY_SMTP_PASSWORD", | ||
| 43 | + "DIZEY_MAIL_FROM", | ||
| 44 | +]; | ||
| 45 | + | ||
| 46 | +/// What the process needs to know before it opens a socket. | ||
| 47 | +#[derive(Clone, Debug, PartialEq, Eq)] | ||
| 48 | +pub struct Config { | ||
| 49 | + /// The database file, absolute. One process holds it. | ||
| 50 | + pub database: PathBuf, | ||
| 51 | + /// The origin links in mail point at, with no trailing slash. | ||
| 52 | + pub base_url: String, | ||
| 53 | + /// The sender, if one is configured. Mail stays quiet without it. | ||
| 54 | + pub mail: Option<MailConfig>, | ||
| 55 | + /// Whether the development defaults were taken. | ||
| 56 | + pub dev: bool, | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +/// Where mail goes out through. The password is in here and stays out of every | ||
| 60 | +/// rendering of it. | ||
| 61 | +#[derive(Clone, PartialEq, Eq)] | ||
| 62 | +pub struct MailConfig { | ||
| 63 | + pub host: String, | ||
| 64 | + pub port: u16, | ||
| 65 | + pub username: String, | ||
| 66 | + pub password: String, | ||
| 67 | + pub from: String, | ||
| 68 | +} | ||
| 69 | + | ||
| 70 | +impl fmt::Debug for MailConfig { | ||
| 71 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| 72 | + f.debug_struct("MailConfig") | ||
| 73 | + .field("host", &self.host) | ||
| 74 | + .field("port", &self.port) | ||
| 75 | + .field("username", &self.username) | ||
| 76 | + .field("password", &"…") | ||
| 77 | + .field("from", &self.from) | ||
| 78 | + .finish() | ||
| 79 | + } | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +/// Why the process is not starting. | ||
| 83 | +#[derive(Clone, Debug, PartialEq, Eq)] | ||
| 84 | +pub enum ConfigError { | ||
| 85 | + /// Named variables are unset and have no default outside development. | ||
| 86 | + Missing(Vec<&'static str>), | ||
| 87 | + /// A variable is set to something the app cannot use. | ||
| 88 | + Invalid { variable: &'static str, why: String }, | ||
| 89 | + /// Some of the sender's variables are set and some are not. | ||
| 90 | + PartialMail { | ||
| 91 | + set: Vec<&'static str>, | ||
| 92 | + missing: Vec<&'static str>, | ||
| 93 | + }, | ||
| 94 | +} | ||
| 95 | + | ||
| 96 | +impl fmt::Display for ConfigError { | ||
| 97 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| 98 | + match self { | ||
| 99 | + ConfigError::Missing(names) => { | ||
| 100 | + write!(f, "not starting: ")?; | ||
| 101 | + let them = if names.len() == 1 { | ||
| 102 | + write!(f, "{} is not set", names[0])?; | ||
| 103 | + "it" | ||
| 104 | + } else { | ||
| 105 | + write!(f, "{} are not set", names.join(", "))?; | ||
| 106 | + "them" | ||
| 107 | + }; | ||
| 108 | + write!( | ||
| 109 | + f, | ||
| 110 | + ". Set {them}, or set DIZEY_DEV=1 to take the development defaults." | ||
| 111 | + ) | ||
| 112 | + } | ||
| 113 | + ConfigError::Invalid { variable, why } => { | ||
| 114 | + write!( | ||
| 115 | + f, | ||
| 116 | + "not starting: {variable} is set to something unusable — {why}" | ||
| 117 | + ) | ||
| 118 | + } | ||
| 119 | + ConfigError::PartialMail { set, missing } => write!( | ||
| 120 | + f, | ||
| 121 | + "not starting: a mail sender is half configured — {} set, {} missing. \ | ||
| 122 | + Set all five or none; with none, Dizey sends nothing and says so.", | ||
| 123 | + set.join(", "), | ||
| 124 | + missing.join(", ") | ||
| 125 | + ), | ||
| 126 | + } | ||
| 127 | + } | ||
| 128 | +} | ||
| 129 | + | ||
| 130 | +impl std::error::Error for ConfigError {} | ||
| 131 | + | ||
| 132 | +impl Config { | ||
| 133 | + /// Reads the environment. Every failure names the variable behind it. | ||
| 134 | + pub fn from_env() -> Result<Config, ConfigError> { | ||
| 135 | + Config::read(|name| std::env::var(name).ok()) | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + /// The same reading, against any source — which is how it is tested | ||
| 139 | + /// without a test being able to disturb another test's environment. | ||
| 140 | + pub fn read(source: impl Fn(&str) -> Option<String>) -> Result<Config, ConfigError> { | ||
| 141 | + let value = |name: &str| { | ||
| 142 | + source(name).and_then(|raw| { | ||
| 143 | + let trimmed = raw.trim().to_string(); | ||
| 144 | + (!trimmed.is_empty()).then_some(trimmed) | ||
| 145 | + }) | ||
| 146 | + }; | ||
| 147 | + let dev = matches!(value("DIZEY_DEV").as_deref(), Some("1" | "true" | "yes")); | ||
| 148 | + | ||
| 149 | + let mut missing = Vec::new(); | ||
| 150 | + let database = value("DIZEY_DATABASE").or_else(|| dev.then(|| "dizey.db".to_string())); | ||
| 151 | + if database.is_none() { | ||
| 152 | + missing.push("DIZEY_DATABASE"); | ||
| 153 | + } | ||
| 154 | + let base_url = | ||
| 155 | + value("DIZEY_BASE_URL").or_else(|| dev.then(|| "http://127.0.0.1:3000".to_string())); | ||
| 156 | + if base_url.is_none() { | ||
| 157 | + missing.push("DIZEY_BASE_URL"); | ||
| 158 | + } | ||
| 159 | + if !missing.is_empty() { | ||
| 160 | + return Err(ConfigError::Missing(missing)); | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + let base_url = base_url.expect("checked above"); | ||
| 164 | + if !(base_url.starts_with("http://") || base_url.starts_with("https://")) { | ||
| 165 | + return Err(ConfigError::Invalid { | ||
| 166 | + variable: "DIZEY_BASE_URL", | ||
| 167 | + why: format!("{base_url:?} is not an http:// or https:// origin"), | ||
| 168 | + }); | ||
| 169 | + } | ||
| 170 | + let base_url = base_url.trim_end_matches('/').to_string(); | ||
| 171 | + | ||
| 172 | + let mail = mail_from(&value)?; | ||
| 173 | + | ||
| 174 | + Ok(Config { | ||
| 175 | + database: absolute(Path::new(&database.expect("checked above"))), | ||
| 176 | + base_url, | ||
| 177 | + mail, | ||
| 178 | + dev, | ||
| 179 | + }) | ||
| 180 | + } | ||
| 181 | + | ||
| 182 | + /// The lines to print once at startup. Nothing secret is among them. | ||
| 183 | + pub fn report(&self) -> Vec<String> { | ||
| 184 | + let mut lines = vec![ | ||
| 185 | + format!("database {}", self.database.display()), | ||
| 186 | + format!("base url {}", self.base_url), | ||
| 187 | + ]; | ||
| 188 | + lines.push(match &self.mail { | ||
| 189 | + Some(mail) => format!( | ||
| 190 | + "mail {} via {}:{} as {}", | ||
| 191 | + mail.from, mail.host, mail.port, mail.username | ||
| 192 | + ), | ||
| 193 | + None => "mail no sender configured — Dizey sends nothing".to_string(), | ||
| 194 | + }); | ||
| 195 | + if self.dev { | ||
| 196 | + lines.push("dev DIZEY_DEV=1, development defaults taken".to_string()); | ||
| 197 | + } | ||
| 198 | + lines | ||
| 199 | + } | ||
| 200 | +} | ||
| 201 | + | ||
| 202 | +fn mail_from(value: &impl Fn(&str) -> Option<String>) -> Result<Option<MailConfig>, ConfigError> { | ||
| 203 | + let read: Vec<(&'static str, Option<String>)> = MAIL_VARIABLES | ||
| 204 | + .iter() | ||
| 205 | + .map(|name| (*name, value(name))) | ||
| 206 | + .collect(); | ||
| 207 | + let set: Vec<&'static str> = read | ||
| 208 | + .iter() | ||
| 209 | + .filter(|(_, held)| held.is_some()) | ||
| 210 | + .map(|(name, _)| *name) | ||
| 211 | + .collect(); | ||
| 212 | + if set.is_empty() { | ||
| 213 | + return Ok(None); | ||
| 214 | + } | ||
| 215 | + let missing: Vec<&'static str> = read | ||
| 216 | + .iter() | ||
| 217 | + .filter(|(_, held)| held.is_none()) | ||
| 218 | + .map(|(name, _)| *name) | ||
| 219 | + .collect(); | ||
| 220 | + if !missing.is_empty() { | ||
| 221 | + return Err(ConfigError::PartialMail { set, missing }); | ||
| 222 | + } | ||
| 223 | + | ||
| 224 | + let held = |name: &str| { | ||
| 225 | + read.iter() | ||
| 226 | + .find(|(candidate, _)| *candidate == name) | ||
| 227 | + .and_then(|(_, held)| held.clone()) | ||
| 228 | + .expect("all five are set") | ||
| 229 | + }; | ||
| 230 | + let raw_port = held("DIZEY_SMTP_PORT"); | ||
| 231 | + let port = raw_port.parse::<u16>().map_err(|_| ConfigError::Invalid { | ||
| 232 | + variable: "DIZEY_SMTP_PORT", | ||
| 233 | + why: format!("{raw_port:?} is not a port number"), | ||
| 234 | + })?; | ||
| 235 | + let from = held("DIZEY_MAIL_FROM"); | ||
| 236 | + if !from.contains('@') { | ||
| 237 | + return Err(ConfigError::Invalid { | ||
| 238 | + variable: "DIZEY_MAIL_FROM", | ||
| 239 | + why: format!("{from:?} is not an address"), | ||
| 240 | + }); | ||
| 241 | + } | ||
| 242 | + | ||
| 243 | + Ok(Some(MailConfig { | ||
| 244 | + host: held("DIZEY_SMTP_HOST"), | ||
| 245 | + port, | ||
| 246 | + username: held("DIZEY_SMTP_USERNAME"), | ||
| 247 | + password: held("DIZEY_SMTP_PASSWORD"), | ||
| 248 | + from, | ||
| 249 | + })) | ||
| 250 | +} | ||
| 251 | + | ||
| 252 | +/// An absolute path for a file that may not exist yet: the directory is | ||
| 253 | +/// resolved, the file name is kept as written. | ||
| 254 | +fn absolute(path: &Path) -> PathBuf { | ||
| 255 | + if path.is_absolute() { | ||
| 256 | + return path.to_path_buf(); | ||
| 257 | + } | ||
| 258 | + let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")); | ||
| 259 | + let joined = cwd.join(path); | ||
| 260 | + match (joined.parent(), joined.file_name()) { | ||
| 261 | + (Some(parent), Some(name)) => match parent.canonicalize() { | ||
| 262 | + Ok(parent) => parent.join(name), | ||
| 263 | + Err(_) => joined.clone(), | ||
| 264 | + }, | ||
| 265 | + _ => joined, | ||
| 266 | + } | ||
| 267 | +} | ||
| 268 | + | ||
| 269 | +#[cfg(test)] | ||
| 270 | +mod tests { | ||
| 271 | + use super::*; | ||
| 272 | + use std::collections::HashMap; | ||
| 273 | + | ||
| 274 | + /// An environment, without touching the process's own. | ||
| 275 | + fn env(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> { | ||
| 276 | + let map: HashMap<String, String> = pairs | ||
| 277 | + .iter() | ||
| 278 | + .map(|(name, value)| (name.to_string(), value.to_string())) | ||
| 279 | + .collect(); | ||
| 280 | + move |name: &str| map.get(name).cloned() | ||
| 281 | + } | ||
| 282 | + | ||
| 283 | + #[test] | ||
| 284 | + fn an_empty_environment_names_what_is_missing_rather_than_guessing() { | ||
| 285 | + let problem = Config::read(env(&[])).unwrap_err(); | ||
| 286 | + let ConfigError::Missing(names) = &problem else { | ||
| 287 | + panic!("expected the missing names, got {problem:?}"); | ||
| 288 | + }; | ||
| 289 | + assert_eq!(names, &["DIZEY_DATABASE", "DIZEY_BASE_URL"]); | ||
| 290 | + let said = problem.to_string(); | ||
| 291 | + assert!(said.contains("DIZEY_DATABASE"), "{said}"); | ||
| 292 | + assert!(said.contains("DIZEY_DEV=1"), "{said}"); | ||
| 293 | + } | ||
| 294 | + | ||
| 295 | + #[test] | ||
| 296 | + fn a_blank_value_is_not_a_value() { | ||
| 297 | + let problem = Config::read(env(&[ | ||
| 298 | + ("DIZEY_DATABASE", " "), | ||
| 299 | + ("DIZEY_BASE_URL", "https://dizey.sh"), | ||
| 300 | + ])) | ||
| 301 | + .unwrap_err(); | ||
| 302 | + assert_eq!(problem, ConfigError::Missing(vec!["DIZEY_DATABASE"])); | ||
| 303 | + } | ||
| 304 | + | ||
| 305 | + #[test] | ||
| 306 | + fn the_dev_flag_is_the_only_thing_that_brings_defaults_back() { | ||
| 307 | + let config = Config::read(env(&[("DIZEY_DEV", "1")])).unwrap(); | ||
| 308 | + assert!(config.dev); | ||
| 309 | + assert!(config.database.is_absolute(), "{:?}", config.database); | ||
| 310 | + assert!(config.database.ends_with("dizey.db")); | ||
| 311 | + assert_eq!(config.base_url, "http://127.0.0.1:3000"); | ||
| 312 | + assert!(config.mail.is_none()); | ||
| 313 | + } | ||
| 314 | + | ||
| 315 | + #[test] | ||
| 316 | + fn a_relative_database_is_reported_absolute() { | ||
| 317 | + let config = Config::read(env(&[ | ||
| 318 | + ("DIZEY_DATABASE", "state/dizey.db"), | ||
| 319 | + ("DIZEY_BASE_URL", "https://dizey.sh"), | ||
| 320 | + ])) | ||
| 321 | + .unwrap(); | ||
| 322 | + assert!(config.database.is_absolute(), "{:?}", config.database); | ||
| 323 | + assert!(config.database.ends_with("dizey.db")); | ||
| 324 | + let report = config.report().join("\n"); | ||
| 325 | + assert!( | ||
| 326 | + report.contains(&config.database.display().to_string()), | ||
| 327 | + "{report}" | ||
| 328 | + ); | ||
| 329 | + } | ||
| 330 | + | ||
| 331 | + #[test] | ||
| 332 | + fn a_base_url_that_is_not_an_origin_stops_the_boot() { | ||
| 333 | + let problem = Config::read(env(&[ | ||
| 334 | + ("DIZEY_DATABASE", "/srv/dizey.db"), | ||
| 335 | + ("DIZEY_BASE_URL", "dizey.sh"), | ||
| 336 | + ])) | ||
| 337 | + .unwrap_err(); | ||
| 338 | + assert!( | ||
| 339 | + matches!( | ||
| 340 | + problem, | ||
| 341 | + ConfigError::Invalid { | ||
| 342 | + variable: "DIZEY_BASE_URL", | ||
| 343 | + .. | ||
| 344 | + } | ||
| 345 | + ), | ||
| 346 | + "{problem:?}" | ||
| 347 | + ); | ||
| 348 | + } | ||
| 349 | + | ||
| 350 | + #[test] | ||
| 351 | + fn the_base_url_keeps_no_trailing_slash_so_links_are_built_once() { | ||
| 352 | + let config = Config::read(env(&[ | ||
| 353 | + ("DIZEY_DATABASE", "/srv/dizey.db"), | ||
| 354 | + ("DIZEY_BASE_URL", "https://dizey.sh/"), | ||
| 355 | + ])) | ||
| 356 | + .unwrap(); | ||
| 357 | + assert_eq!(config.base_url, "https://dizey.sh"); | ||
| 358 | + } | ||
| 359 | + | ||
| 360 | + #[test] | ||
| 361 | + fn no_sender_is_a_configuration_not_an_error() { | ||
| 362 | + let config = Config::read(env(&[ | ||
| 363 | + ("DIZEY_DATABASE", "/srv/dizey.db"), | ||
| 364 | + ("DIZEY_BASE_URL", "https://dizey.sh"), | ||
| 365 | + ])) | ||
| 366 | + .unwrap(); | ||
| 367 | + assert!(config.mail.is_none()); | ||
| 368 | + assert!( | ||
| 369 | + config | ||
| 370 | + .report() | ||
| 371 | + .iter() | ||
| 372 | + .any(|line| line.contains("no sender configured")), | ||
| 373 | + "{:?}", | ||
| 374 | + config.report() | ||
| 375 | + ); | ||
| 376 | + } | ||
| 377 | + | ||
| 378 | + #[test] | ||
| 379 | + fn half_a_sender_stops_the_boot_and_names_both_halves() { | ||
| 380 | + let problem = Config::read(env(&[ | ||
| 381 | + ("DIZEY_DATABASE", "/srv/dizey.db"), | ||
| 382 | + ("DIZEY_BASE_URL", "https://dizey.sh"), | ||
| 383 | + ("DIZEY_SMTP_HOST", "smtp.example.net"), | ||
| 384 | + ("DIZEY_SMTP_PORT", "587"), | ||
| 385 | + ])) | ||
| 386 | + .unwrap_err(); | ||
| 387 | + let ConfigError::PartialMail { set, missing } = &problem else { | ||
| 388 | + panic!("expected the half-configured sender, got {problem:?}"); | ||
| 389 | + }; | ||
| 390 | + assert_eq!(set, &["DIZEY_SMTP_HOST", "DIZEY_SMTP_PORT"]); | ||
| 391 | + assert_eq!( | ||
| 392 | + missing, | ||
| 393 | + &[ | ||
| 394 | + "DIZEY_SMTP_USERNAME", | ||
| 395 | + "DIZEY_SMTP_PASSWORD", | ||
| 396 | + "DIZEY_MAIL_FROM" | ||
| 397 | + ] | ||
| 398 | + ); | ||
| 399 | + } | ||
| 400 | + | ||
| 401 | + #[test] | ||
| 402 | + fn a_whole_sender_is_read_and_its_password_is_never_printed() { | ||
| 403 | + let config = Config::read(env(&[ | ||
| 404 | + ("DIZEY_DATABASE", "/srv/dizey.db"), | ||
| 405 | + ("DIZEY_BASE_URL", "https://dizey.sh"), | ||
| 406 | + ("DIZEY_SMTP_HOST", "smtp.example.net"), | ||
| 407 | + ("DIZEY_SMTP_PORT", "587"), | ||
| 408 | + ("DIZEY_SMTP_USERNAME", "dizey"), | ||
| 409 | + ("DIZEY_SMTP_PASSWORD", "hunter2-and-then-some"), | ||
| 410 | + ("DIZEY_MAIL_FROM", "board@dizey.sh"), | ||
| 411 | + ])) | ||
| 412 | + .unwrap(); | ||
| 413 | + let mail = config.mail.clone().expect("a sender"); | ||
| 414 | + assert_eq!(mail.port, 587); | ||
| 415 | + assert_eq!(mail.from, "board@dizey.sh"); | ||
| 416 | + | ||
| 417 | + let report = config.report().join("\n"); | ||
| 418 | + assert!(report.contains("board@dizey.sh"), "{report}"); | ||
| 419 | + assert!(!report.contains("hunter2-and-then-some"), "{report}"); | ||
| 420 | + let debugged = format!("{mail:?}"); | ||
| 421 | + assert!(!debugged.contains("hunter2-and-then-some"), "{debugged}"); | ||
| 422 | + } | ||
| 423 | + | ||
| 424 | + #[test] | ||
| 425 | + fn a_port_that_is_not_a_port_stops_the_boot() { | ||
| 426 | + let problem = Config::read(env(&[ | ||
| 427 | + ("DIZEY_DATABASE", "/srv/dizey.db"), | ||
| 428 | + ("DIZEY_BASE_URL", "https://dizey.sh"), | ||
| 429 | + ("DIZEY_SMTP_HOST", "smtp.example.net"), | ||
| 430 | + ("DIZEY_SMTP_PORT", "not-a-port"), | ||
| 431 | + ("DIZEY_SMTP_USERNAME", "dizey"), | ||
| 432 | + ("DIZEY_SMTP_PASSWORD", "secret"), | ||
| 433 | + ("DIZEY_MAIL_FROM", "board@dizey.sh"), | ||
| 434 | + ])) | ||
| 435 | + .unwrap_err(); | ||
| 436 | + assert!( | ||
| 437 | + matches!( | ||
| 438 | + problem, | ||
| 439 | + ConfigError::Invalid { | ||
| 440 | + variable: "DIZEY_SMTP_PORT", | ||
| 441 | + .. | ||
| 442 | + } | ||
| 443 | + ), | ||
| 444 | + "{problem:?}" | ||
| 445 | + ); | ||
| 446 | + } | ||
| 447 | + | ||
| 448 | + #[test] | ||
| 449 | + fn a_from_address_without_an_at_sign_stops_the_boot() { | ||
| 450 | + let problem = Config::read(env(&[ | ||
| 451 | + ("DIZEY_DATABASE", "/srv/dizey.db"), | ||
| 452 | + ("DIZEY_BASE_URL", "https://dizey.sh"), | ||
| 453 | + ("DIZEY_SMTP_HOST", "smtp.example.net"), | ||
| 454 | + ("DIZEY_SMTP_PORT", "587"), | ||
| 455 | + ("DIZEY_SMTP_USERNAME", "dizey"), | ||
| 456 | + ("DIZEY_SMTP_PASSWORD", "secret"), | ||
| 457 | + ("DIZEY_MAIL_FROM", "board-at-dizey"), | ||
| 458 | + ])) | ||
| 459 | + .unwrap_err(); | ||
| 460 | + assert!( | ||
| 461 | + matches!( | ||
| 462 | + problem, | ||
| 463 | + ConfigError::Invalid { | ||
| 464 | + variable: "DIZEY_MAIL_FROM", | ||
| 465 | + .. | ||
| 466 | + } | ||
| 467 | + ), | ||
| 468 | + "{problem:?}" | ||
| 469 | + ); | ||
| 470 | + } | ||
| 471 | + | ||
| 472 | + #[test] | ||
| 473 | + fn every_variable_the_app_reads_is_named_in_one_list() { | ||
| 474 | + for name in MAIL_VARIABLES { | ||
| 475 | + assert!(VARIABLES.contains(name), "{name} is missing from VARIABLES"); | ||
| 476 | + } | ||
| 477 | + assert!(VARIABLES.contains(&"DIZEY_DEV")); | ||
| 478 | + assert!(VARIABLES.contains(&"DIZEY_DATABASE")); | ||
| 479 | + assert!(VARIABLES.contains(&"DIZEY_BASE_URL")); | ||
| 480 | + } | ||
| 481 | +} | ||
crates/dizey-core/src/lib.rsmodified+4 -0
| @@ -17,6 +17,8 @@ pub mod accounts; | |||
| 17 | #[cfg(feature = "server")] | 17 | #[cfg(feature = "server")] |
| 18 | pub mod auth; | 18 | pub mod auth; |
| 19 | #[cfg(feature = "server")] | 19 | #[cfg(feature = "server")] |
| 20 | +pub mod config; | ||
| 21 | +#[cfg(feature = "server")] | ||
| 20 | pub mod store; | 22 | pub mod store; |
| 21 | 23 | ||
| 22 | #[cfg(feature = "server")] | 24 | #[cfg(feature = "server")] |
| @@ -24,6 +26,8 @@ pub use accounts::{AccountError, Accounts}; | |||
| 24 | #[cfg(feature = "server")] | 26 | #[cfg(feature = "server")] |
| 25 | pub use board::{BoardReads, load as load_board}; | 27 | pub use board::{BoardReads, load as load_board}; |
| 26 | #[cfg(feature = "server")] | 28 | #[cfg(feature = "server")] |
| 29 | +pub use config::{Config, ConfigError}; | ||
| 30 | +#[cfg(feature = "server")] | ||
| 27 | pub use detail::{DetailReads, load as load_detail}; | 31 | pub use detail::{DetailReads, load as load_detail}; |
| 28 | #[cfg(feature = "server")] | 32 | #[cfg(feature = "server")] |
| 29 | pub use store::{Store, StoreError, TursoStore}; | 33 | pub use store::{Store, StoreError, TursoStore}; |
crates/dizey-web/src/main.rsmodified+18 -2
| @@ -6,11 +6,27 @@ 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 | ||
| 10 | + // opened. A missing one stops the boot with its name in the message: the | ||
| 11 | + // failure this prevents is not an empty database, it is a second Dizey | ||
| 12 | + // writing a different file while everyone believes they share a board. | ||
| 13 | + let config = match dizey_core::Config::from_env() { | ||
| 14 | + Ok(config) => config, | ||
| 15 | + Err(problem) => { | ||
| 16 | + eprintln!("dizey: {problem}"); | ||
| 17 | + std::process::exit(2); | ||
| 18 | + } | ||
| 19 | + }; | ||
| 20 | + // Said once, so the answer to "which file are we on" lives in the log. | ||
| 21 | + for line in config.report() { | ||
| 22 | + println!("dizey {line}"); | ||
| 23 | + } | ||
| 24 | + | ||
| 9 | // One process per database file: Turso is a single-writer engine and a | 25 | // One process per database file: Turso is a single-writer engine and a |
| 10 | // second process on the same file loses writes rather than queueing. | 26 | // second process on the same file loses writes rather than queueing. |
| 11 | - let database = std::env::var("DIZEY_DATABASE").unwrap_or_else(|_| "dizey.db".to_string()); | 27 | + // |
| 12 | // `open` applies any unapplied migration before it returns. | 28 | // `open` applies any unapplied migration before it returns. |
| 13 | - let store = TursoStore::open(&database) | 29 | + let store = TursoStore::open(&config.database.to_string_lossy()) |
| 14 | .await | 30 | .await |
| 15 | .expect("failed to open the database"); | 31 | .expect("failed to open the database"); |
| 16 | let accounts = Accounts::new(Arc::new(store)); | 32 | let accounts = Accounts::new(Arc::new(store)); |