Skip to main content

btcnode_metrics/
config.rs

1use serde::Deserialize;
2use std::path::Path;
3
4use crate::Error;
5
6#[derive(Debug, Deserialize)]
7pub struct AppConfig {
8    pub node: NodeConfig,
9    pub server: ServerConfig,
10}
11
12#[derive(Debug, Deserialize)]
13pub struct NodeConfig {
14    pub rpc_url: String,
15    pub rpc_user: String,
16    pub rpc_password: String,
17}
18
19#[derive(Debug, Deserialize)]
20pub struct ServerConfig {
21    pub listen_addr: String,
22}
23
24impl AppConfig {
25    pub fn load(path: &Path) -> Result<Self, Error> {
26        let contents = std::fs::read_to_string(path)
27            .map_err(|e| Error::Config(format!("failed to read config file: {e}")))?;
28
29        let mut config: AppConfig = toml::from_str(&contents)
30            .map_err(|e| Error::Config(format!("failed to parse config: {e}")))?;
31
32        // Environment variable overrides
33        if let Ok(val) = std::env::var("BTC_METRICS_RPC_URL") {
34            config.node.rpc_url = val;
35        }
36        if let Ok(val) = std::env::var("BTC_METRICS_RPC_USER") {
37            config.node.rpc_user = val;
38        }
39        if let Ok(val) = std::env::var("BTC_METRICS_RPC_PASSWORD") {
40            config.node.rpc_password = val;
41        }
42        if let Ok(val) = std::env::var("BTC_METRICS_LISTEN_ADDR") {
43            config.server.listen_addr = val;
44        }
45
46        Ok(config)
47    }
48}