use secrecy::SecretString;
use std::sync::OnceLock;
static ENV_CONFIG: OnceLock<EnvConfig> = OnceLock::new();
#[derive(Debug, Clone)]
pub struct EnvConfig {
pub arcgis_api_key: Option<SecretString>,
pub arcgis_public_key: Option<SecretString>,
pub arcgis_location_key: Option<SecretString>,
pub arcgis_content_key: Option<SecretString>,
pub arcgis_features_key: Option<SecretString>,
pub arcgis_client_id: Option<SecretString>,
pub arcgis_client_secret: Option<SecretString>,
pub arcgis_enterprise_portal: Option<String>,
pub arcgis_enterprise_key: Option<SecretString>,
pub arcgis_feature_url: Option<String>,
}
impl EnvConfig {
fn load() -> Self {
let _ = dotenvy::dotenv();
tracing::debug!("Loading environment configuration");
let config = Self {
arcgis_api_key: std::env::var("ARCGIS_API_KEY").ok().map(|s| {
tracing::debug!("ARCGIS_API_KEY loaded from environment");
SecretString::new(s.into())
}),
arcgis_public_key: std::env::var("ARCGIS_PUBLIC_KEY").ok().map(|s| {
tracing::debug!("ARCGIS_PUBLIC_KEY loaded from environment");
SecretString::new(s.into())
}),
arcgis_location_key: std::env::var("ARCGIS_LOCATION_KEY").ok().map(|s| {
tracing::debug!("ARCGIS_LOCATION_KEY loaded from environment");
SecretString::new(s.into())
}),
arcgis_content_key: std::env::var("ARCGIS_CONTENT_KEY").ok().map(|s| {
tracing::debug!("ARCGIS_CONTENT_KEY loaded from environment");
SecretString::new(s.into())
}),
arcgis_features_key: std::env::var("ARCGIS_FEATURES_KEY").ok().map(|s| {
tracing::debug!("ARCGIS_FEATURES_KEY loaded from environment");
SecretString::new(s.into())
}),
arcgis_client_id: std::env::var("ARCGIS_CLIENT_ID").ok().map(|s| {
tracing::debug!("ARCGIS_CLIENT_ID loaded from environment");
SecretString::new(s.into())
}),
arcgis_client_secret: std::env::var("ARCGIS_CLIENT_SECRET").ok().map(|s| {
tracing::debug!("ARCGIS_CLIENT_SECRET loaded from environment");
SecretString::new(s.into())
}),
arcgis_enterprise_portal: std::env::var("ARCGIS_ENTERPRISE_PORTAL").ok().inspect(
|_| {
tracing::debug!("ARCGIS_ENTERPRISE_PORTAL loaded from environment");
},
),
arcgis_enterprise_key: std::env::var("ARCGIS_ENTERPRISE_KEY").ok().map(|s| {
tracing::debug!("ARCGIS_ENTERPRISE_KEY loaded from environment");
SecretString::new(s.into())
}),
arcgis_feature_url: std::env::var("ARCGIS_FEATURE_URL").ok().inspect(|_| {
tracing::debug!("ARCGIS_FEATURE_URL loaded from environment");
}),
};
tracing::debug!("Environment configuration loaded");
config
}
pub fn global() -> &'static Self {
ENV_CONFIG.get_or_init(Self::load)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_env_config_loads_without_panic() {
let config = EnvConfig::global();
let _ = &config.arcgis_api_key;
}
}