use std::collections::HashMap;
use parking_lot::RwLock;
pub struct Environment {
config: RwLock<HashMap<String, String>>,
secrets: RwLock<HashMap<String, String>>,
}
impl Environment {
pub fn new() -> Self {
Self {
config: RwLock::new(HashMap::new()),
secrets: RwLock::new(HashMap::new()),
}
}
pub fn set_config(&self, key: impl Into<String>, value: impl Into<String>) {
self.config.write().insert(key.into(), value.into());
}
pub fn get_config(&self, key: &str) -> Option<String> {
self.config.read().get(key).cloned()
}
pub fn set_secret(&self, key: impl Into<String>, value: impl Into<String>) {
self.secrets.write().insert(key.into(), value.into());
}
pub fn get_secret(&self, key: &str) -> Option<String> {
self.secrets.read().get(key).cloned()
}
pub fn has_secret(&self, key: &str) -> bool {
self.secrets.read().contains_key(key)
}
pub fn load_from_env(&self, prefix: &str) {
for (key, value) in std::env::vars() {
if key.starts_with(prefix) {
let config_key = key.strip_prefix(prefix).unwrap_or(&key).to_lowercase();
self.set_config(config_key, value);
}
}
}
}
impl Default for Environment {
fn default() -> Self {
Self::new()
}
}