controller/emgauwa-controller/src/settings.rs

69 lines
1.3 KiB
Rust
Raw Normal View History

use config::Config;
2023-11-23 02:36:14 +00:00
use emgauwa_lib::constants;
use serde_derive::Deserialize;
2023-11-23 02:36:14 +00:00
#[derive(Clone, Debug, Deserialize)]
#[serde(default)]
#[allow(unused)]
pub struct Core {
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)]
#[serde(default)]
#[allow(unused)]
pub struct Settings {
pub database: String,
pub logging: Logging,
}
impl Default for Settings {
fn default() -> Self {
Settings {
database: String::from("sqlite://emgauwa-controller.sqlite"),
logging: Logging::default(),
}
}
}
2023-11-23 02:36:14 +00:00
impl Default for Core {
fn default() -> Self {
Core {
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 init() -> Settings {
Config::builder()
.add_source(config::File::with_name("emgauwa-controller"))
.add_source(
config::Environment::with_prefix("EMGAUWA_CONTROLLER")
.prefix_separator("_")
.separator("__"),
)
.build()
.unwrap()
.try_deserialize::<Settings>()
.unwrap_or_else(|_| panic!("Error reading settings."))
}