Skip to main content

rustack_core/
config.rs

1//! Configuration management for Rustack services.
2//!
3//! All configuration is driven by environment variables, matching LocalStack conventions.
4
5use crate::types::AwsRegion;
6
7/// Global configuration for Rustack.
8#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct RustackConfig {
11    /// Bind address for the gateway.
12    pub gateway_listen: String,
13    /// Default AWS region.
14    pub default_region: AwsRegion,
15    /// Log level.
16    pub log_level: String,
17    /// Whether persistence is enabled.
18    pub persistence: bool,
19    /// Data directory for persistence.
20    pub data_dir: String,
21}
22
23impl Default for RustackConfig {
24    fn default() -> Self {
25        Self {
26            gateway_listen: "0.0.0.0:4566".to_owned(),
27            default_region: AwsRegion::default(),
28            log_level: "info".to_owned(),
29            persistence: false,
30            data_dir: "/var/lib/localstack".to_owned(),
31        }
32    }
33}
34
35impl RustackConfig {
36    /// Load configuration from environment variables.
37    #[must_use]
38    pub fn from_env() -> Self {
39        let mut config = Self::default();
40
41        if let Ok(v) = std::env::var("GATEWAY_LISTEN") {
42            config.gateway_listen = v;
43        }
44        if let Ok(v) = std::env::var("DEFAULT_REGION") {
45            config.default_region = AwsRegion::new(v);
46        }
47        if let Ok(v) = std::env::var("LOG_LEVEL") {
48            config.log_level = v;
49        }
50        if let Ok(v) = std::env::var("PERSISTENCE") {
51            config.persistence = v == "1" || v.eq_ignore_ascii_case("true");
52        }
53        if let Ok(v) = std::env::var("DATA_DIR") {
54            config.data_dir = v;
55        }
56
57        config
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_should_create_default_config() {
67        let config = RustackConfig::default();
68        assert_eq!(config.gateway_listen, "0.0.0.0:4566");
69        assert_eq!(config.default_region.as_str(), "us-east-1");
70        assert!(!config.persistence);
71    }
72}