pusher_rs/
config.rs

1use dotenv::dotenv;
2use std::env;
3use std::time::Duration;
4
5/// Config for the Pusher client. We are defaulting to .env. 
6/// TODO: look into .toml or .yaml
7#[derive(Clone, Debug)]
8pub struct PusherConfig {
9    /// The Pusher App ID.
10    pub app_id: String,
11
12    /// The Pusher App key.
13    pub app_key: String,
14
15    /// The Pusher App secret.
16    pub app_secret: String,
17
18    /// The cluster.
19    pub cluster: String,
20
21    /// Whether to use TLS for connections. Defaults to true.
22    pub use_tls: bool,
23
24    /// The host to connect to. If None, the default Pusher host will be used.
25    pub host: Option<String>,
26
27    /// The maximum number of reconnection attempts. Defaults to 6.
28    pub max_reconnection_attempts: u32,
29
30    /// The backoff interval for reconnection attempts. Defaults to 1 second.
31    pub backoff_interval: Duration,
32
33    /// The activity timeout. Defaults to 120 seconds.
34    pub activity_timeout: Duration,
35
36    /// The pong timeout. Defaults to 30 seconds.
37    pub pong_timeout: Duration,
38}
39
40impl Default for PusherConfig {
41    fn default() -> Self {
42        Self {
43            app_id: String::new(),
44            app_key: String::new(),
45            app_secret: String::new(),
46            cluster: String::new(),
47            use_tls: false,
48            host: None,
49            max_reconnection_attempts: 6,
50            backoff_interval: Duration::from_secs(1),
51            activity_timeout: Duration::from_secs(120),
52            pong_timeout: Duration::from_secs(30),
53        }
54    }
55}
56
57impl PusherConfig {
58    pub fn from_env() -> Result<Self, env::VarError> {
59        dotenv().ok(); // This line loads the .env file
60        let cluster = env::var("PUSHER_CLUSTER").unwrap_or_else(|_| "mt1".to_string()); //Default to mt1.
61        let host = env::var("PUSHER_HOST")
62            .ok()
63            .unwrap_or_else(|| format!("ws-{}.pusher.com", cluster)); // let's read <> build the host from the env variable
64
65        Ok(Self {
66            app_id: env::var("PUSHER_APP_ID")?,
67            app_key: env::var("PUSHER_KEY")?,
68            app_secret: env::var("PUSHER_SECRET")?,
69            cluster,
70            use_tls: env::var("PUSHER_USE_TLS")
71                .map(|v| v.to_lowercase() == "true")
72                .unwrap_or(true),
73            host: Some(host),
74            max_reconnection_attempts: env::var("PUSHER_MAX_RECONNECTION_ATTEMPTS")
75                .ok()
76                .and_then(|v| v.parse().ok())
77                .unwrap_or(6),
78            backoff_interval: Duration::from_secs(
79                env::var("PUSHER_BACKOFF_INTERVAL")
80                    .ok()
81                    .and_then(|v| v.parse().ok())
82                    .unwrap_or(1),
83            ),
84            activity_timeout: Duration::from_secs(
85                env::var("PUSHER_ACTIVITY_TIMEOUT")
86                    .ok()
87                    .and_then(|v| v.parse().ok())
88                    .unwrap_or(120),
89            ),
90            pong_timeout: Duration::from_secs(
91                env::var("PUSHER_PONG_TIMEOUT")
92                    .ok()
93                    .and_then(|v| v.parse().ok())
94                    .unwrap_or(30),
95            ),
96        })
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    #[test]
105    fn test_default_config() {
106        let config = PusherConfig::default();
107        // assert!(config.use_tls);
108        assert_eq!(config.max_reconnection_attempts, 6);
109        assert_eq!(config.backoff_interval, Duration::from_secs(1));
110        assert_eq!(config.activity_timeout, Duration::from_secs(120));
111        assert_eq!(config.pong_timeout, Duration::from_secs(30));
112    }
113
114    #[test]
115    #[ignore]
116    fn test_new_config() {
117        let config =
118            PusherConfig::from_env().expect("Failed to load Pusher configuration from environment");
119        assert_eq!(config.app_id, "app_id");
120        assert_eq!(config.app_key, "app_key");
121        assert_eq!(config.app_secret, "app_secret");
122        assert_eq!(config.cluster, "eu");
123    }
124}