2024-02-18 18:50:22 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2024-04-24 23:41:53 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Default)]
|
2024-02-18 18:50:22 +00:00
|
|
|
#[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>,
|
|
|
|
{
|
2024-04-29 02:19:02 +00:00
|
|
|
let etc_file =
|
|
|
|
config::File::with_name(&format!("/etc/emgauwa/{}", config_name)).required(false);
|
2024-02-18 18:50:22 +00:00
|
|
|
let local_file = config::File::with_name(&format!("./emgauwa-{}", config_name)).required(false);
|
|
|
|
|
|
|
|
config::Config::builder()
|
2024-04-29 02:19:02 +00:00
|
|
|
.add_source(etc_file)
|
2024-02-18 18:50:22 +00:00
|
|
|
.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)
|
|
|
|
}
|