diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..7302f23 --- /dev/null +++ b/config.toml @@ -0,0 +1,6 @@ +listen = "127.0.0.1:8123" +data_dir = "./data" +site_title = "ily.rs" +site_url = "https://ily.rs" +telegram_bot_token = "REPLACE_ME" +telegram_chat_id = 0 diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..0446feb --- /dev/null +++ b/src/config.rs @@ -0,0 +1,43 @@ +use serde::Deserialize; +use std::path::PathBuf; + +#[derive(Debug, Deserialize)] +pub struct Config { + pub listen: String, + pub data_dir: PathBuf, + pub site_title: String, + pub site_url: String, + pub telegram_bot_token: String, + pub telegram_chat_id: i64, +} + +impl Config { + pub fn load(path: &str) -> Result> { + let content = std::fs::read_to_string(path)?; + let config: Config = toml::from_str(&content)?; + Ok(config) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_config() { + let toml_str = r#" +listen = "127.0.0.1:8123" +data_dir = "/var/lib/guestbook" +site_title = "ily.rs" +site_url = "https://ily.rs" +telegram_bot_token = "123:ABC" +telegram_chat_id = 12345 +"#; + let config: Config = toml::from_str(toml_str).unwrap(); + assert_eq!(config.listen, "127.0.0.1:8123"); + assert_eq!(config.data_dir, PathBuf::from("/var/lib/guestbook")); + assert_eq!(config.site_title, "ily.rs"); + assert_eq!(config.site_url, "https://ily.rs"); + assert_eq!(config.telegram_chat_id, 12345); + } +} diff --git a/src/main.rs b/src/main.rs index 46d4aa3..48fd603 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +mod config; + fn main() { - println!("guestbook"); + let config = config::Config::load("config.toml").expect("failed to load config.toml"); + println!("listening on {}", config.listen); }