diff --git a/config/default.toml b/config/default.toml index b7b7cfe..c2c2036 100644 --- a/config/default.toml +++ b/config/default.toml @@ -29,3 +29,7 @@ pool = 4 url="smtps://username:password@smtp.example.com:465" from="Vanikam Info " reply_to="Vanikam Support " + +[meili] +url = "http://localhost:7700" +#api_key = "" diff --git a/src/settings/meili.rs b/src/settings/meili.rs new file mode 100644 index 0000000..24dd03d --- /dev/null +++ b/src/settings/meili.rs @@ -0,0 +1,58 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use std::env; + +use config::{builder::DefaultState, ConfigBuilder}; +use serde::Deserialize; +use validator::Validate; + +#[derive(Debug, Clone, Validate, Deserialize, Eq, PartialEq)] +pub struct Meili { + #[validate(url)] + pub url: String, + pub api_key: String, +} + +impl Meili { + pub fn env_override(mut s: ConfigBuilder) -> ConfigBuilder { + for (parameter, env_var_name) in [ + ("meili.url", "VANIKAM_meili_URL"), + ("meili.api_key", "VANIKAM_meili_API_KEY"), + ] + .iter() + { + if let Ok(val) = env::var(env_var_name) { + log::debug!("Overriding [{parameter}] with environment variable {env_var_name}"); + s = s.set_override(parameter, val).unwrap(); + } + } + + s + } +} + +#[cfg(test)] +mod tests { + use crate::env_helper; + + use super::*; + + #[test] + fn test_meili_env_override() { + let init_settings = crate::settings::Settings::new().unwrap(); + env_helper!( + init_settings, + "VANIKAM_meili_URL", + "https://test_meili_env_override.org", + meili.url + ); + env_helper!( + init_settings, + "VANIKAM_meili_API_KEY", + "apitestketkere", + meili.api_key + ); + } +} diff --git a/src/settings/mod.rs b/src/settings/mod.rs index c4d45c1..8945e65 100644 --- a/src/settings/mod.rs +++ b/src/settings/mod.rs @@ -13,10 +13,12 @@ use validator::Validate; pub mod database; pub mod email; +pub mod meili; pub mod server; use database::{DBType, Database}; use email::Email; +use meili::Meili; use server::Server; #[derive(Debug, Clone, Validate, Deserialize, Eq, PartialEq)] @@ -29,6 +31,7 @@ pub struct Settings { pub database: Database, pub server: Server, pub email: Email, + pub meili: Meili, } impl Settings { @@ -48,6 +51,7 @@ impl Settings { s = Database::env_override(s); s = Email::env_override(s); + s = Meili::env_override(s); Server::env_override(s) }