Skip to main content

dbctl_core/db/
redis.rs

1use super::traits::Database;
2
3#[derive(Clone)]
4pub struct Redis {
5    pub name: String,
6    pub password: Option<String>,
7    pub port: u16,
8    pub host: String,
9}
10
11impl Database for Redis {
12    fn name(&self) -> &str {
13        &self.name
14    }
15
16    fn image(&self) -> &str {
17        "redis:7"
18    }
19
20    fn port(&self) -> u16 {
21        self.port
22    }
23
24    fn env_vars(&self) -> Vec<(String, String)> {
25        let mut env_vars = Vec::new();
26        
27        if let Some(password) = &self.password {
28            env_vars.push(("REDIS_PASSWORD".into(), password.clone()));
29        }
30        
31        env_vars
32    }
33
34    fn connection_url(&self) -> String {
35        match &self.password {
36            Some(password) => format!(
37                "redis://default:{}@{}:{}",
38                password, self.host, self.port
39            ),
40            None => format!(
41                "redis://{}:{}",
42                self.host, self.port
43            ),
44        }
45    }
46
47    fn default() -> Self {
48        Redis {
49            name: "redis-default".to_string(),
50            password: Some("redispassword".to_string()),
51            port: 6379,
52            host: "localhost".to_string(),
53        }
54    }
55}