use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::error::Error as stdError;
use std::fs;
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub urls: Vec<HashMap<String, String>>,
pub hosts: Vec<HashMap<String, String>>,
pub api: Vec<HashMap<String, String>>,
pub authenticator: Vec<HashMap<String, HashMap<String, String>>>,
pub postgres_clients: Vec<HashMap<String, String>>,
#[serde(default)]
pub gateway: Vec<HashMap<String, String>>,
}
impl Config {
pub fn load() -> Result<Self, Box<dyn stdError>> {
Self::load_from("config.yaml")
}
pub fn load_from(path: &str) -> Result<Self, Box<dyn stdError>> {
let content = fs::read_to_string(path)?;
let config: Config = serde_yaml::from_str(&content)?;
Ok(config)
}
pub fn get_url(&self, service: &str) -> Option<&String> {
self.urls.iter().find_map(|map| map.get(service))
}
pub fn get_host(&self, service: &str) -> Option<&String> {
self.hosts.iter().find_map(|map| map.get(service))
}
pub fn get_api(&self) -> Option<&String> {
self.api.iter().find_map(|map| map.get("port"))
}
pub fn get_immortal_cache(&self) -> Option<&String> {
self.api.iter().find_map(|map| map.get("immortal_cache"))
}
pub fn get_cache_ttl(&self) -> Option<&String> {
self.api.iter().find_map(|map| map.get("cache_ttl"))
}
pub fn get_pool_idle_timeout(&self) -> Option<&String> {
self.api.iter().find_map(|map| map.get("pool_idle_timeout"))
}
pub fn get_http_keep_alive_secs(&self) -> Option<&String> {
self.api.iter().find_map(|map| map.get("keep_alive_secs"))
}
pub fn get_client_disconnect_timeout_secs(&self) -> Option<&String> {
self.api
.iter()
.find_map(|map| map.get("client_disconnect_timeout_secs"))
}
pub fn get_client_request_timeout_secs(&self) -> Option<&String> {
self.api
.iter()
.find_map(|map| map.get("client_request_timeout_secs"))
}
pub fn get_http_workers(&self) -> Option<&String> {
self.api.iter().find_map(|map| map.get("http_workers"))
}
pub fn get_http_max_connections(&self) -> Option<&String> {
self.api
.iter()
.find_map(|map| map.get("http_max_connections"))
}
pub fn get_http_backlog(&self) -> Option<&String> {
self.api.iter().find_map(|map| map.get("http_backlog"))
}
pub fn get_tcp_keepalive_secs(&self) -> Option<&String> {
self.api
.iter()
.find_map(|map| map.get("tcp_keepalive_secs"))
}
pub fn get_authenticator(&self, service: &str) -> Option<&HashMap<String, String>> {
self.authenticator.iter().find_map(|map| map.get(service))
}
pub fn get_postgres_uri(&self, client: &str) -> Option<&String> {
self.postgres_clients.iter().find_map(|map| map.get(client))
}
pub fn get_gateway_force_camel_case_to_snake_case(&self) -> bool {
self.gateway
.iter()
.find_map(|map| map.get("force_camel_case_to_snake_case"))
.and_then(|value| value.parse().ok())
.unwrap_or(false)
}
pub fn get_gateway_logging_client(&self) -> Option<&String> {
self.gateway
.iter()
.find_map(|map| map.get("logging_client"))
}
}