nomilo/src/config.rs

40 lines
1.1 KiB
Rust
Raw Normal View History

2021-03-20 02:31:41 +00:00
use std::net::SocketAddr;
use std::path::PathBuf;
use std::fs;
2021-03-27 17:23:19 +00:00
use serde::{Deserialize, Deserializer};
use chrono::Duration;
2021-03-20 02:31:41 +00:00
2021-03-27 17:23:19 +00:00
#[derive(Debug, Deserialize)]
2021-03-20 02:31:41 +00:00
pub struct Config {
2022-04-22 17:16:57 +00:00
pub dns: DnsClientConfig,
2021-03-27 17:23:19 +00:00
pub web_app: WebAppConfig,
2021-03-20 02:31:41 +00:00
}
2021-03-27 17:23:19 +00:00
#[derive(Debug, Deserialize)]
2022-04-22 17:16:57 +00:00
pub struct DnsClientConfig {
2021-03-27 17:23:19 +00:00
pub server: SocketAddr
}
#[derive(Debug, Deserialize)]
pub struct WebAppConfig {
2021-03-27 17:31:14 +00:00
pub secret: String,
2021-03-27 17:23:19 +00:00
#[serde(deserialize_with = "from_duration")]
pub token_duration: Duration,
}
fn from_duration<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where D: Deserializer<'de>
{
use serde::de::Error;
String::deserialize(deserializer)
.and_then(|string| humantime::parse_duration(&string).map_err(|err| Error::custom(err.to_string())))
.and_then(|duration| Duration::from_std(duration).map_err(|err| Error::custom(err.to_string())))
2021-03-20 02:31:41 +00:00
}
pub fn load(file_name: PathBuf) -> Config {
2021-03-27 17:23:19 +00:00
let file_content = fs::read_to_string(file_name).expect("could not read config file");
toml::from_str(&file_content).expect("could not parse config file")
2021-03-20 02:31:41 +00:00
}