//! get static and parsed environment variables //! //! USER_DID is parsed into a jacquard Did //! DATABASE_URL is from DATABASE_URL or POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_HOST, and POSTGRES_DATABASE //! USER_PDS_URL is resolved from USER_DID if ommited //! USER_EXPORT_URL falls back to USER_PDS_URL //! USER_SUBSCRIBE_URL falls back to USER_PDS_URL use jacquard::prelude::IdentityResolver; use jacquard::types::string::Did; use std::env; pub const DB_MAX_REQ: usize = 65535; mod config_value; use crate::config::config_value::ConfigValue; // this should be loaded before the program starts any threads // if this panics threads that access it will be poisoned pub static USER_DID: ConfigValue> = ConfigValue::new_sync(|| { let Ok(env) = env::var("USER_DID") else { panic!("USER_DID not set"); }; let Ok(did) = Did::new_owned(env) else { panic!("USER_DID was not a valid did") }; did }); pub static DATABASE_URL: ConfigValue = ConfigValue::new_sync(|| { if let Ok(url) = env::var("DATABASE_URL") { return url; } let user = env::var("POSTGRES_USER"); let db = env::var("POSTGRES_DATABASE").or_else(|_| user.clone()); let password = env::var("POSTGRES_PASSWORD"); let host = env::var("POSTGRES_HOST"); if let Ok(user) = user.clone() && let Ok(db) = db.clone() && let Ok(password) = password.clone() && let Ok(host) = host.clone() { format!("postgres://{}:{}@{}/{}", user, password, host, db) } else { let missing = [ (user, "USER"), (db, "DATABASE"), (password, "PASSWORD"), (host, "HOST"), ] .iter() .filter_map(|x| { if x.0.is_err() { Some(String::from("POSTGRES_") + x.1) } else { None } }) .collect::>() .join(", "); panic!( "Could not generate database url. Missing environment variables {}. Set DATABASE_URL to define the postgres url manually", missing ); } }); pub static USER_PDS_URL: ConfigValue = ConfigValue::new_async(|| { Box::pin(async { if let Ok(url) = env::var("USER_PDS_URL") { url } else { let resolver = jacquard::identity::PublicResolver::default(); resolver .pds_for_did(&self::USER_DID) .await .unwrap() .domain() .unwrap() .to_string() } }) }); pub static USER_EXPORT_URL: ConfigValue = ConfigValue::new_sync(|| env::var("USER_EXPORT_URL").unwrap_or(USER_PDS_URL.clone())); pub static USER_SUBSCRIBE_URL: ConfigValue = ConfigValue::new_sync(|| env::var("USER_SUBSCRIBE_URL").unwrap_or(USER_PDS_URL.clone()));