Skip to main content

gaia_client/
config.rs

1use std::path::{Path, PathBuf};
2
3/// Configuration for the Gaia client.
4#[derive(Debug, Clone)]
5pub struct GaiaClientConfig {
6    /// The address of the Gaia daemon (e.g., "localhost:50051").
7    pub server_address: String,
8
9    /// Path to the CA certificate file.
10    pub ca_cert_path: PathBuf,
11
12    /// Path to the client certificate file.
13    pub client_cert_path: PathBuf,
14
15    /// Path to the client private key file.
16    pub client_key_path: PathBuf,
17
18    /// Optional domain name for TLS verification (defaults to "gaia").
19    pub domain_name: String,
20}
21
22impl GaiaClientConfig {
23    /// Creates a new Gaia client configuration.
24    ///
25    /// # Arguments
26    ///
27    /// * `server_address` - The address of the Gaia daemon (e.g., "localhost:50051")
28    /// * `ca_cert_path` - Path to the CA certificate file
29    /// * `client_cert_path` - Path to the client certificate file
30    /// * `client_key_path` - Path to the client private key file
31    ///
32    /// # Example
33    ///
34    /// ```
35    /// use gaia_client::GaiaClientConfig;
36    ///
37    /// let config = GaiaClientConfig::new(
38    ///     "localhost:50051",
39    ///     "/etc/gaia/certs/ca.crt",
40    ///     "/etc/gaia/certs/client.crt",
41    ///     "/etc/gaia/certs/client.key",
42    /// );
43    /// ```
44    pub fn new<S, P>(
45        server_address: S,
46        ca_cert_path: P,
47        client_cert_path: P,
48        client_key_path: P,
49    ) -> Self
50    where
51        S: Into<String>,
52        P: AsRef<Path>,
53    {
54        Self {
55            server_address: server_address.into(),
56            ca_cert_path: ca_cert_path.as_ref().to_path_buf(),
57            client_cert_path: client_cert_path.as_ref().to_path_buf(),
58            client_key_path: client_key_path.as_ref().to_path_buf(),
59            domain_name: "gaia".to_string(),
60        }
61    }
62
63    /// Sets a custom domain name for TLS verification.
64    pub fn with_domain_name<S: Into<String>>(mut self, domain_name: S) -> Self {
65        self.domain_name = domain_name.into();
66        self
67    }
68
69    /// Creates a configuration from environment variables.
70    ///
71    /// Expects the following environment variables:
72    /// - `GAIA_SERVER_ADDRESS` (defaults to "localhost:50051")
73    /// - `GAIA_CA_CERT` (defaults to "/etc/gaia/certs/ca.crt")
74    /// - `GAIA_CLIENT_CERT` (defaults to "/etc/gaia/certs/client.crt")
75    /// - `GAIA_CLIENT_KEY` (defaults to "/etc/gaia/certs/client.key")
76    ///
77    /// # Example
78    ///
79    /// ```
80    /// use gaia_client::GaiaClientConfig;
81    ///
82    /// let config = GaiaClientConfig::from_env();
83    /// ```
84    pub fn from_env() -> Self {
85        let server_address =
86            std::env::var("GAIA_SERVER_ADDRESS").unwrap_or_else(|_| "localhost:50051".to_string());
87        let ca_cert_path =
88            std::env::var("GAIA_CA_CERT").unwrap_or_else(|_| "/etc/gaia/certs/ca.crt".to_string());
89        let client_cert_path = std::env::var("GAIA_CLIENT_CERT")
90            .unwrap_or_else(|_| "/etc/gaia/certs/client.crt".to_string());
91        let client_key_path = std::env::var("GAIA_CLIENT_KEY")
92            .unwrap_or_else(|_| "/etc/gaia/certs/client.key".to_string());
93
94        Self::new(
95            server_address,
96            ca_cert_path,
97            client_cert_path,
98            client_key_path,
99        )
100    }
101}