morethantext-web/src/settings.rs

52 lines
1.2 KiB
Rust
Raw Normal View History

use config::{Config, ConfigError};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
//#[allow(unused)]
pub struct Settings {
pub address: String,
pub port: u16,
}
impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let s = Config::builder()
.set_default("port", 9090)?
.set_default("address", "127.0.0.1")?
2022-06-19 00:42:44 -04:00
.add_source(config::File::with_name("morethantext").required(false))
.add_source(
config::Environment::with_prefix("MTT")
.try_parsing(true)
.separator("_")
.list_separator(" "),
)
.build()?;
s.try_deserialize()
}
}
#[cfg(test)]
mod settings_tests {
use super::*;
use serial_test::serial;
#[test]
#[serial]
fn defaults() {
let set = Settings::new().unwrap();
println!("{:?}", set);
assert_eq!(set.port, 9090);
assert_eq!(set.address, "127.0.0.1");
}
#[test]
#[serial]
fn port_env() {
std::env::set_var("MTT_PORT", "7825");
let set = Settings::new().unwrap();
println!("{:?}", set);
assert_eq!(set.port, 7825);
std::env::remove_var("MTT_PORT");
}
}