3dd7293feat: ship Steid as an installable binary7d | 1 | use std::{fmt, path::PathBuf}; |
0d12f7dfeat: config from env and SQLite pool in app context1mo | 2 | |
| 3 | use serde::Deserialize; |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | #[derive(Debug, Clone, Deserialize)] |
| 9 | pub struct AppConfig { |
| 10 | |
| 11 | #[serde(default = "default_database_url")] |
| 12 | pub database_url: String, |
| 13 | |
02eb2e4feat: GitStorage port and DiskGitStorage24d | 14 | |
| 15 | |
0d12f7dfeat: config from env and SQLite pool in app context1mo | 16 | |
02eb2e4feat: GitStorage port and DiskGitStorage24d | 17 | |
| 18 | |
0d12f7dfeat: config from env and SQLite pool in app context1mo | 19 | #[serde(default = "default_data_dir")] |
| 20 | pub data_dir: PathBuf, |
264c436fix: session cookie survives plain-HTTP localhost in dev1mo | 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | #[serde(default)] |
| 28 | pub insecure_cookies: bool, |
3dd7293feat: ship Steid as an installable binary7d | 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | #[serde(default)] |
| 47 | pub setup_token: Option<Secret>, |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | #[derive(Clone, Deserialize)] |
| 55 | pub struct Secret(String); |
| 56 | |
| 57 | impl Secret { |
| 58 | |
| 59 | #[must_use] |
| 60 | pub fn expose(&self) -> &str { |
| 61 | &self.0 |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl fmt::Debug for Secret { |
| 66 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 67 | f.write_str("Secret(redacted)") |
| 68 | } |
0d12f7dfeat: config from env and SQLite pool in app context1mo | 69 | } |
| 70 | |
| 71 | fn default_database_url() -> String { |
| 72 | "sqlite:steid.db?mode=rwc".to_owned() |
| 73 | } |
| 74 | |
| 75 | fn default_data_dir() -> PathBuf { |
| 76 | PathBuf::from("./data") |
| 77 | } |
| 78 | |
| 79 | impl AppConfig { |
| 80 | |
| 81 | pub fn from_env() -> Result<Self, envy::Error> { |
| 82 | envy::prefixed("STEID_").from_env() |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #[cfg(test)] |
| 87 | mod tests { |
| 88 | use super::*; |
| 89 | |
| 90 | |
| 91 | |
| 92 | fn from_pairs(pairs: &[(&str, &str)]) -> Result<AppConfig, envy::Error> { |
1e1ae56feat: identity domain model1mo | 93 | envy::prefixed("STEID_").from_iter( |
| 94 | pairs |
| 95 | .iter() |
| 96 | .map(|(k, v)| ((*k).to_owned(), (*v).to_owned())), |
| 97 | ) |
0d12f7dfeat: config from env and SQLite pool in app context1mo | 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | fn applies_defaults_when_environment_is_empty() { |
| 102 | let config = from_pairs(&[]).expect("defaults should satisfy every field"); |
| 103 | |
| 104 | assert_eq!(config.database_url, "sqlite:steid.db?mode=rwc"); |
| 105 | assert_eq!(config.data_dir, PathBuf::from("./data")); |
| 106 | } |
| 107 | |
| 108 | #[test] |
| 109 | fn reads_prefixed_variables() { |
| 110 | let config = from_pairs(&[ |
| 111 | ("STEID_DATABASE_URL", "sqlite:custom.db?mode=rwc"), |
| 112 | ("STEID_DATA_DIR", "/srv/steid/repos"), |
| 113 | ]) |
| 114 | .expect("explicit values should parse"); |
| 115 | |
| 116 | assert_eq!(config.database_url, "sqlite:custom.db?mode=rwc"); |
| 117 | assert_eq!(config.data_dir, PathBuf::from("/srv/steid/repos")); |
| 118 | } |
| 119 | |
3dd7293feat: ship Steid as an installable binary7d | 120 | #[test] |
| 121 | fn a_setup_token_is_absent_unless_configured() { |
| 122 | let config = from_pairs(&[]).expect("defaults should satisfy every field"); |
| 123 | |
| 124 | assert!(config.setup_token.is_none()); |
| 125 | } |
| 126 | |
| 127 | #[test] |
| 128 | fn reads_the_setup_token_from_the_environment() { |
| 129 | let config = from_pairs(&[("STEID_SETUP_TOKEN", "b2a9c17e4d5f80316a7c9e2b4d8f0135")]) |
| 130 | .expect("an explicit token should parse"); |
| 131 | |
| 132 | assert_eq!( |
| 133 | config.setup_token.as_ref().map(Secret::expose), |
| 134 | Some("b2a9c17e4d5f80316a7c9e2b4d8f0135") |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | #[test] |
| 139 | fn a_configured_setup_token_is_redacted_in_debug_output() { |
| 140 | let secret = "b2a9c17e4d5f80316a7c9e2b4d8f0135"; |
| 141 | let config = from_pairs(&[("STEID_SETUP_TOKEN", secret)]).expect("an explicit token"); |
| 142 | |
| 143 | let rendered = format!("{config:?}"); |
| 144 | |
| 145 | assert!(!rendered.contains(secret), "{rendered}"); |
| 146 | assert!(rendered.contains("Secret(redacted)"), "{rendered}"); |
| 147 | } |
| 148 | |
0d12f7dfeat: config from env and SQLite pool in app context1mo | 149 | #[test] |
| 150 | fn ignores_unprefixed_variables() { |
| 151 | let config = from_pairs(&[("DATA_DIR", "/should/be/ignored")]) |
| 152 | .expect("unprefixed keys should not interfere"); |
| 153 | |
| 154 | assert_eq!(config.data_dir, PathBuf::from("./data")); |
| 155 | } |
| 156 | } |