feat: read meilisearch config

This commit is contained in:
Aravinth Manivannan 2024-07-17 20:16:21 +05:30
parent b4a76a36ad
commit 7103116c0f
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 66 additions and 0 deletions

View file

@ -29,3 +29,7 @@ pool = 4
url="smtps://username:password@smtp.example.com:465"
from="Vanikam Info <vanikam@example.com>"
reply_to="Vanikam Support <vanikam@example.com>"
[meili]
url = "http://localhost:7700"
#api_key = ""

58
src/settings/meili.rs Normal file
View file

@ -0,0 +1,58 @@
// 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 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<DefaultState>) -> ConfigBuilder<DefaultState> {
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
);
}
}

View file

@ -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)
}