Skip to main content

cdk_mintd/env_vars/
info.rs

1//! Info environment variables
2
3use std::env;
4use std::str::FromStr;
5
6use cdk_common::common::QuoteTTL;
7
8use super::common::*;
9use crate::config::{Info, LoggingOutput};
10
11impl Info {
12    pub fn from_env(mut self) -> Self {
13        // Required fields
14        if let Ok(url) = env::var(ENV_URL) {
15            self.url = url;
16        }
17
18        if let Ok(host) = env::var(ENV_LISTEN_HOST) {
19            self.listen_host = host;
20        }
21
22        if let Ok(port_str) = env::var(ENV_LISTEN_PORT) {
23            if let Ok(port) = port_str.parse() {
24                self.listen_port = port;
25            }
26        }
27
28        if let Ok(signatory_url) = env::var(ENV_SIGNATORY_URL) {
29            self.signatory_url = Some(signatory_url);
30        }
31
32        if let Ok(signatory_certs) = env::var(ENV_SIGNATORY_CERTS) {
33            self.signatory_certs = Some(signatory_certs);
34        }
35
36        if let Ok(seed) = env::var(ENV_SEED) {
37            self.seed = Some(seed);
38        }
39
40        if let Ok(mnemonic) = env::var(ENV_MNEMONIC) {
41            self.mnemonic = Some(mnemonic);
42        }
43
44        if let Ok(cache_seconds_str) = env::var(ENV_CACHE_SECONDS) {
45            if let Ok(seconds) = cache_seconds_str.parse() {
46                self.http_cache.ttl = Some(seconds);
47            }
48        }
49
50        if let Ok(extend_cache_str) = env::var(ENV_EXTEND_CACHE_SECONDS) {
51            if let Ok(seconds) = extend_cache_str.parse() {
52                self.http_cache.tti = Some(seconds);
53            }
54        }
55
56        if let Ok(fee_str) = env::var(ENV_INPUT_FEE_PPK) {
57            if let Ok(fee) = fee_str.parse() {
58                self.input_fee_ppk = Some(fee);
59            }
60        }
61
62        if let Ok(swagger_str) = env::var(ENV_ENABLE_SWAGGER) {
63            if let Ok(enable) = swagger_str.parse() {
64                self.enable_swagger_ui = Some(enable);
65            }
66        }
67
68        if let Ok(info_page_str) = env::var(ENV_ENABLE_INFO_PAGE) {
69            if let Ok(enable) = info_page_str.parse() {
70                self.enable_info_page = Some(enable);
71            }
72        }
73
74        if let Ok(use_keyset_v2_str) = env::var(ENV_USE_KEYSET_V2) {
75            if let Ok(use_keyset_v2) = use_keyset_v2_str.parse() {
76                self.use_keyset_v2 = Some(use_keyset_v2);
77            }
78        }
79
80        // Logging configuration
81        if let Ok(output_str) = env::var(ENV_LOGGING_OUTPUT) {
82            if let Ok(output) = LoggingOutput::from_str(&output_str) {
83                self.logging.output = output;
84            } else {
85                tracing::warn!(
86                    "Invalid logging output '{}' in environment variable. Valid options: stdout, file, both",
87                    output_str
88                );
89            }
90        }
91
92        if let Ok(console_level) = env::var(ENV_LOGGING_CONSOLE_LEVEL) {
93            self.logging.console_level = Some(console_level);
94        }
95
96        if let Ok(file_level) = env::var(ENV_LOGGING_FILE_LEVEL) {
97            self.logging.file_level = Some(file_level);
98        }
99
100        self.http_cache = self.http_cache.from_env();
101
102        // Quote TTL from env
103        let mut mint_ttl_env: Option<u64> = None;
104        let mut melt_ttl_env: Option<u64> = None;
105        if let Ok(mint_ttl_str) = env::var(ENV_QUOTE_TTL_MINT) {
106            if let Ok(v) = mint_ttl_str.parse::<u64>() {
107                mint_ttl_env = Some(v);
108            }
109        }
110        if let Ok(melt_ttl_str) = env::var(ENV_QUOTE_TTL_MELT) {
111            if let Ok(v) = melt_ttl_str.parse::<u64>() {
112                melt_ttl_env = Some(v);
113            }
114        }
115        if mint_ttl_env.is_some() || melt_ttl_env.is_some() {
116            let current = self.quote_ttl.unwrap_or_default();
117            self.quote_ttl = Some(QuoteTTL {
118                mint_ttl: mint_ttl_env.unwrap_or(current.mint_ttl),
119                melt_ttl: melt_ttl_env.unwrap_or(current.melt_ttl),
120            });
121        }
122
123        self
124    }
125}