common/src/settings.rs

67 lines
1.4 KiB
Rust

use serde_derive::Deserialize;
use crate::constants;
use crate::errors::EmgauwaError;
#[derive(Clone, Debug, Deserialize)]
#[serde(default)]
#[allow(unused)]
pub struct Server {
pub host: String,
pub port: u16,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(default)]
#[allow(unused)]
pub struct Logging {
pub level: String,
pub file: String,
}
#[derive(Clone, Debug, Deserialize, Default)]
#[serde(default)]
#[allow(unused)]
pub struct Permissions {
pub user: String,
pub group: String,
}
impl Default for Server {
fn default() -> Self {
Server {
host: String::from("127.0.0.1"),
port: constants::DEFAULT_PORT,
}
}
}
impl Default for Logging {
fn default() -> Self {
Logging {
level: String::from("info"),
file: String::from("stdout"),
}
}
}
pub fn load<T>(config_name: &str, env_prefix: &str) -> Result<T, EmgauwaError>
where
for<'de> T: serde::Deserialize<'de>,
{
let etc_file =
config::File::with_name(&format!("/etc/emgauwa/{}", config_name)).required(false);
let local_file = config::File::with_name(&format!("./emgauwa-{}", config_name)).required(false);
config::Config::builder()
.add_source(etc_file)
.add_source(local_file)
.add_source(
config::Environment::with_prefix(&format!("EMGAUWA_{}", env_prefix))
.prefix_separator("__")
.separator("__"),
)
.build()?
.try_deserialize::<T>()
.map_err(EmgauwaError::from)
}