feat: load twilio settings

This commit is contained in:
Aravinth Manivannan 2025-01-09 20:59:03 +05:30
parent 4b0df692f1
commit 0d5540771c
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 69 additions and 0 deletions

View file

@ -33,3 +33,8 @@ reply_to="Vanikam Support <vanikam@example.com>"
[meili]
#url = "http://localhost:7700"
#api_key = ""
[phone]
twilio_account_id = ""
twilio_auth_token = ""

View file

@ -14,11 +14,13 @@ use validator::Validate;
pub mod database;
pub mod email;
pub mod meili;
pub mod phone;
pub mod server;
use database::{DBType, Database};
use email::Email;
use meili::Meili;
use phone::Phone;
use server::Server;
#[derive(Debug, Clone, Validate, Deserialize, Eq, PartialEq)]
@ -32,6 +34,7 @@ pub struct Settings {
pub server: Server,
pub email: Email,
pub meili: Meili,
pub phone: Phone,
}
impl Settings {
@ -52,6 +55,7 @@ impl Settings {
s = Database::env_override(s);
s = Email::env_override(s);
s = Meili::env_override(s);
s = Phone::env_override(s);
Server::env_override(s)
}

60
src/settings/phone.rs Normal file
View file

@ -0,0 +1,60 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::env;
use config::{builder::DefaultState, ConfigBuilder};
use lettre::message::Mailbox;
use serde::{Deserialize, Serialize};
use url::Url;
use validator::{Validate, ValidationError};
#[derive(Debug, Clone, Serialize, Deserialize, Validate, Eq, PartialEq)]
pub struct Phone {
pub twilio_account_id: String,
pub twilio_auth_token: String,
}
impl Phone {
pub fn env_override(mut s: ConfigBuilder<DefaultState>) -> ConfigBuilder<DefaultState> {
for (parameter, env_var_name) in [
("phone.twilio_account_id", "VANIKAM_phone_TWILIO_ACCOUNT_ID"),
("phone.twilio_auth_token", "VANIKAM_phone_TWILIO_AUTH_TOKEN"),
]
.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_phone_env_override() {
let init_settings = crate::settings::Settings::new().unwrap();
env_helper!(
init_settings,
"VANIKAM_phone_TWILIO_AUTH_TOKEN",
"foo".to_string(),
phone.twilio_auth_token
);
env_helper!(
init_settings,
"VANIKAM_phone_TWILIO_ACCOUNT_ID",
"foo".to_string(),
phone.twilio_account_id
);
}
}