diff options
author | tempest <git@ashen.earth> | 2025-04-15 00:08:12 -0600 |
---|---|---|
committer | tempest <git@ashen.earth> | 2025-04-15 00:08:12 -0600 |
commit | b856f12cf422b96c37c12df3d7829e4d15ef4453 (patch) | |
tree | 4547df3ec3d347715292a860a5a63207fe428de3 /src/data/config.rs | |
parent | 128cc42557c8d7da46c63a40ea4469ed0eb7f26d (diff) |
Can find content data
Diffstat (limited to 'src/data/config.rs')
-rw-r--r-- | src/data/config.rs | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/src/data/config.rs b/src/data/config.rs index 9a17077..11e10cc 100644 --- a/src/data/config.rs +++ b/src/data/config.rs @@ -13,34 +13,55 @@ struct ConfigFile { } pub struct Config { - site_title: String, - data_dir: PathBuf, - external_root: String, - listen_port: u16, - footer_copyright: Option<String> + pub site_title: String, + pub data_dir: PathBuf, + pub external_root: String, + pub listen_port: u16, + pub footer_copyright: Option<String>, } #[cfg(feature = "ssr")] impl Config { - pub fn read_from_file<P: AsRef<Path>>(path: P) -> Result<Self, String> { - let config_contents = fs::read_to_string(&path) + pub fn read_from_file() -> Result<Self, String> { + let config_path = Self::get_location()?; + let config_contents = fs::read_to_string(&config_path) .map_err(|_| "Unable to open config file".to_string())?; - let file : ConfigFile = toml::from_str(&config_contents) - .map_err(|err| err.to_string())?; + let file: ConfigFile = toml::from_str(&config_contents).map_err(|err| err.to_string())?; let port = file.listen_port.unwrap_or(3000); Ok(Self { - site_title: file.site_title + site_title: file + .site_title .unwrap_or("Untitled StormScribe Site".to_string()), - data_dir: file.data_dir.unwrap_or(path.as_ref() - .canonicalize().map_err(|_| "Cannot resolve config file location".to_string())? - .parent().ok_or("Cannot resolve data dir".to_string())?.to_path_buf()), - external_root: file.external_root + data_dir: file.data_dir.unwrap_or( + PathBuf::from(&config_path) + .canonicalize() + .map_err(|_| "Cannot resolve config file location".to_string())? + .parent() + .ok_or("Cannot resolve data dir".to_string())? + .to_path_buf(), + ), + external_root: file + .external_root .unwrap_or(format!("http://localhost:{port}/")), listen_port: port, footer_copyright: file.footer_copyright, }) } + + fn get_location() -> Result<String, String> { + Ok( + std::env::var("STORMSCRIBE_CONFIG_FILE").or_else(|_| -> Result<String, String> { + Ok(std::path::Path::join( + &std::env::current_dir() + .map_err(|_| "Could not read current directory".to_string())?, + "config.toml", + ) + .to_string_lossy() + .to_string()) + })?, + ) + } } |