The config file lives in config/, tracked once as a placeholder
Browse files at this commit5e2a21acc5fb62f665c3699d7a385ee8f503f7feParent: 37a3769- feat
- the config file lives in config/, tracked once as a placeholder
4 changed files +25 -15
- .gitignore +1 -1
- config/izlek.toml +4 -0
- crates/izlek-core/src/config.rs +19 -13
- crates/izlek-web/src/main.rs +1 -1
.gitignoremodified+1 -1
| @@ -5,5 +5,5 @@ | |||
| 5 | *.db-* | 5 | *.db-* |
| 6 | .env | 6 | .env |
| 7 | izlek.key | 7 | izlek.key |
| 8 | -izlek.toml | 8 | +/config/izlek.toml |
| 9 | CLAUDE.md | 9 | CLAUDE.md |
config/izlek.tomladded+4 -0
| @@ -0,0 +1,4 @@ | |||
| 1 | +# Where the one database file lives. One process holds it. | ||
| 2 | +database = "izlek.db" | ||
| 3 | +# The origin sign-in links in mail point at. | ||
| 4 | +base_url = "http://127.0.0.1:3000" | ||
crates/izlek-core/src/config.rsmodified+19 -13
| @@ -1,4 +1,4 @@ | |||
| 1 | -//! Everything Izlek reads from `izlek.toml`, in one place. | 1 | +//! Everything Izlek reads from `config/izlek.toml`, in one place. |
| 2 | //! | 2 | //! |
| 3 | //! Nothing here has a silent default once the file exists. A key that is | 3 | //! Nothing here has a silent default once the file exists. A key that is |
| 4 | //! missing, empty or unusable stops the boot and says which key and which | 4 | //! missing, empty or unusable stops the boot and says which key and which |
| @@ -10,8 +10,8 @@ | |||
| 10 | //! pointing at a host that is not us. | 10 | //! pointing at a host that is not us. |
| 11 | //! | 11 | //! |
| 12 | //! Development still needs to be one command, so the *absence* of | 12 | //! Development still needs to be one command, so the *absence* of |
| 13 | -//! `izlek.toml` is the opt-in that takes the development defaults: the app | 13 | +//! `config/izlek.toml` is the opt-in that takes the development defaults: the |
| 14 | -//! writes the file itself, with those defaults in it and comments saying | 14 | +//! app 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 | 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 | 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 | 17 | //! first time and reads it like any other. A real deployment is handed the |
| @@ -31,10 +31,11 @@ use serde::Deserialize; | |||
| 31 | use std::fmt; | 31 | use std::fmt; |
| 32 | use std::path::{Path, PathBuf}; | 32 | use std::path::{Path, PathBuf}; |
| 33 | 33 | ||
| 34 | -/// The name of the file `Config::load` reads and, failing that, writes. | 34 | +/// The path, relative to the working directory, of the file `Config::load` |
| 35 | -const FILE_NAME: &str = "izlek.toml"; | 35 | +/// reads and, failing that, writes. |
| 36 | +const FILE_NAME: &str = "config/izlek.toml"; | ||
| 36 | 37 | ||
| 37 | -/// What a freshly written `izlek.toml` says, comments included. Development | 38 | +/// What a freshly written `config/izlek.toml` says, comments included. Development |
| 38 | /// defaults, so that a plain `izlek` in an empty directory is one command. | 39 | /// defaults, so that a plain `izlek` in an empty directory is one command. |
| 39 | const DEVELOPMENT_DEFAULTS: &str = r#"# Where the one database file lives. One process holds it. | 40 | const DEVELOPMENT_DEFAULTS: &str = r#"# Where the one database file lives. One process holds it. |
| 40 | database = "izlek.db" | 41 | database = "izlek.db" |
| @@ -42,7 +43,7 @@ database = "izlek.db" | |||
| 42 | base_url = "http://127.0.0.1:3000" | 43 | base_url = "http://127.0.0.1:3000" |
| 43 | "#; | 44 | "#; |
| 44 | 45 | ||
| 45 | -/// The shape of `izlek.toml`, before the values are checked. | 46 | +/// The shape of `config/izlek.toml`, before the values are checked. |
| 46 | #[derive(Deserialize)] | 47 | #[derive(Deserialize)] |
| 47 | struct Toml { | 48 | struct Toml { |
| 48 | database: Option<String>, | 49 | database: Option<String>, |
| @@ -56,7 +57,7 @@ pub struct Config { | |||
| 56 | pub database: PathBuf, | 57 | pub database: PathBuf, |
| 57 | /// The origin links in mail point at, with no trailing slash. | 58 | /// The origin links in mail point at, with no trailing slash. |
| 58 | pub base_url: String, | 59 | pub base_url: String, |
| 59 | - /// Whether `izlek.toml` did not exist and was just written with the | 60 | + /// Whether `config/izlek.toml` did not exist and was just written with the |
| 60 | /// development defaults this boot. | 61 | /// development defaults this boot. |
| 61 | pub defaulted: bool, | 62 | pub defaulted: bool, |
| 62 | } | 63 | } |
| @@ -101,20 +102,25 @@ impl fmt::Display for ConfigError { | |||
| 101 | impl std::error::Error for ConfigError {} | 102 | impl std::error::Error for ConfigError {} |
| 102 | 103 | ||
| 103 | impl Config { | 104 | impl Config { |
| 104 | - /// Reads `izlek.toml` from the current directory, writing it with the | 105 | + /// Reads `config/izlek.toml` from the current directory, writing it with |
| 105 | - /// development defaults first if it is not there. | 106 | + /// the development defaults first if it is not there. |
| 106 | pub fn load() -> Result<Config, ConfigError> { | 107 | pub fn load() -> Result<Config, ConfigError> { |
| 107 | Config::load_from(Path::new(".")) | 108 | Config::load_from(Path::new(".")) |
| 108 | } | 109 | } |
| 109 | 110 | ||
| 110 | /// The same reading, against any directory — which is how it is tested | 111 | /// The same reading, against any directory — which is how it is tested |
| 111 | /// without a test being able to disturb another test's working | 112 | /// without a test being able to disturb another test's working |
| 112 | - /// directory, or another test's `izlek.toml`. | 113 | + /// directory, or another test's `config/izlek.toml`. |
| 113 | pub fn load_from(dir: &Path) -> Result<Config, ConfigError> { | 114 | pub fn load_from(dir: &Path) -> Result<Config, ConfigError> { |
| 114 | let path = dir.join(FILE_NAME); | 115 | let path = dir.join(FILE_NAME); |
| 115 | match std::fs::read_to_string(&path) { | 116 | match std::fs::read_to_string(&path) { |
| 116 | Ok(text) => Config::parse(&text, dir, false), | 117 | Ok(text) => Config::parse(&text, dir, false), |
| 117 | Err(err) if err.kind() == std::io::ErrorKind::NotFound => { | 118 | Err(err) if err.kind() == std::io::ErrorKind::NotFound => { |
| 119 | + if let Some(parent) = path.parent() { | ||
| 120 | + std::fs::create_dir_all(parent).map_err(|err| { | ||
| 121 | + ConfigError::Io(format!("could not create {}: {err}", parent.display())) | ||
| 122 | + })?; | ||
| 123 | + } | ||
| 118 | std::fs::write(&path, DEVELOPMENT_DEFAULTS).map_err(|err| { | 124 | std::fs::write(&path, DEVELOPMENT_DEFAULTS).map_err(|err| { |
| 119 | ConfigError::Io(format!("could not write {}: {err}", path.display())) | 125 | ConfigError::Io(format!("could not write {}: {err}", path.display())) |
| 120 | })?; | 126 | })?; |
| @@ -173,8 +179,8 @@ impl Config { | |||
| 173 | } | 179 | } |
| 174 | 180 | ||
| 175 | /// An absolute path for a file that may not exist yet: the directory is | 181 | /// An absolute path for a file that may not exist yet: the directory is |
| 176 | -/// resolved against `base` (the directory `izlek.toml` was read from), the | 182 | +/// resolved against `base` (the directory `config/izlek.toml` was read |
| 177 | -/// file name is kept as written. | 183 | +/// from), the file name is kept as written. |
| 178 | fn absolute(base: &Path, path: &Path) -> PathBuf { | 184 | fn absolute(base: &Path, path: &Path) -> PathBuf { |
| 179 | if path.is_absolute() { | 185 | if path.is_absolute() { |
| 180 | return path.to_path_buf(); | 186 | return path.to_path_buf(); |
crates/izlek-web/src/main.rsmodified+1 -1
| @@ -6,7 +6,7 @@ async fn main() { | |||
| 6 | use leptos::prelude::*; | 6 | use leptos::prelude::*; |
| 7 | use std::sync::Arc; | 7 | use std::sync::Arc; |
| 8 | 8 | ||
| 9 | - // izlek.toml is read here, before anything is opened, and written with | 9 | + // config/izlek.toml is read here, before anything is opened, and written with |
| 10 | // development defaults if it is not there yet. A broken key stops the | 10 | // development defaults if it is not there yet. A broken key stops the |
| 11 | // boot with its name in the message: the failure this prevents is not an | 11 | // boot with its name in the message: the failure this prevents is not an |
| 12 | // empty database, it is a second Izlek writing a different file while | 12 | // empty database, it is a second Izlek writing a different file while |