use crate::AppResult;
use config::Config;
use serde::Deserialize;
use std::env;
#[derive(Debug, Clone, Default, Deserialize, PartialEq, Eq)]
pub struct AppConfig {
pub port: u16,
pub host: String,
}
impl AppConfig {
pub async fn from_config(path: &str) -> AppResult<AppConfig> {
let config = Config::builder()
.add_source(config::File::with_name(path).required(false))
.add_source(config::Environment::with_prefix("APP"))
.set_default(
"port",
env::var("PORT").unwrap_or_else(|_| "8080".to_owned()),
)?
.set_default(
"host",
env::var("HOST").unwrap_or_else(|_| "::1".to_owned()),
)?
.build()
.unwrap();
let appconfig: AppConfig = config.try_deserialize().unwrap();
Ok(appconfig)
}
}