Skip to content

Commit

Permalink
env_vars settings
Browse files Browse the repository at this point in the history
  • Loading branch information
lollerfirst committed Dec 16, 2024
1 parent 5efad2c commit ebf55b9
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions crates/cdk-mintd/src/env_vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use anyhow::{anyhow, bail, Result};
use cdk::nuts::CurrencyUnit;

use crate::config::{
Cln, Database, DatabaseEngine, FakeWallet, Info, LNbits, Ln, LnBackend, Lnd, MintInfo,
Phoenixd, Settings, Strike,
Cln, Database, DatabaseEngine, ElasticSearchLogger, FakeWallet, Info, LNbits, Ln, LnBackend, Lnd, MintInfo, Phoenixd, Settings, Strike
};

pub const DATABASE_ENV_VAR: &str = "CDK_MINTD_DATABASE";
Expand Down Expand Up @@ -68,6 +67,10 @@ pub const ENV_FAKE_WALLET_FEE_PERCENT: &str = "CDK_MINTD_FAKE_WALLET_FEE_PERCENT
pub const ENV_FAKE_WALLET_RESERVE_FEE_MIN: &str = "CDK_MINTD_FAKE_WALLET_RESERVE_FEE_MIN";
pub const ENV_FAKE_WALLET_MIN_DELAY: &str = "CDK_MINTD_FAKE_WALLET_MIN_DELAY";
pub const ENV_FAKE_WALLET_MAX_DELAY: &str = "CDK_MINTD_FAKE_WALLET_MAX_DELAY";
// Elastic Search
pub const ENV_ELASTIC_SEARCH_URL: &str = "CDK_MINTD_ELASTIC_SEARCH_URL";
pub const ENV_ELASTIC_SEARCH_INDEX: &str = "CDK_MINTD_ELASTIC_SEARCH_INDEX";
pub const ENV_ELASTIC_SEARCH_API_KEY_BASE64: &str = "CDK_MINTD_ELASTIC_SEARCH_API_KEY_BASE64";

impl Settings {
pub fn from_env(&mut self) -> Result<Self> {
Expand All @@ -79,6 +82,8 @@ impl Settings {
self.info = self.info.clone().from_env();
self.mint_info = self.mint_info.clone().from_env();
self.ln = self.ln.clone().from_env();

self.external_loggers_from_env();

match self.ln.ln_backend {
LnBackend::Cln => {
Expand All @@ -104,6 +109,20 @@ impl Settings {

Ok(self.clone())
}

fn external_loggers_from_env(&mut self) {
// ElasticSearch
if env::var(ENV_ELASTIC_SEARCH_URL).is_ok() {
if self.elasticsearch.is_none() {
let extlogger = ElasticSearchLogger {
elasticsearch_url: "".to_string(), api_key: None, index: "".to_string()
};
self.elasticsearch = Some(extlogger.from_env());
} else {
self.elasticsearch = Some(self.elasticsearch.clone().expect("Expected ElasticSearchLogger").from_env());
}
}
}
}

impl Info {
Expand Down Expand Up @@ -433,3 +452,24 @@ impl FakeWallet {
self
}
}


impl ElasticSearchLogger {
pub fn from_env(mut self) -> Self {
if let Ok(elasticsearch_url) = env::var(ENV_ELASTIC_SEARCH_URL) {
self.elasticsearch_url = elasticsearch_url;
}

if let Ok(api_key) = env::var(ENV_ELASTIC_SEARCH_API_KEY_BASE64) {
self.api_key = Some(api_key);
} else {
self.api_key = None;
}

if let Ok(index) = env::var(ENV_ELASTIC_SEARCH_INDEX) {
self.index = index;
}

self
}
}

0 comments on commit ebf55b9

Please sign in to comment.