admin_config/
redis_config.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub struct RedisConfig {
5 pub host: String,
7 pub port: u16,
9 pub username: Option<String>,
11 pub password: Option<String>,
13 pub database: u8,
15 pub max_pool_size: u32,
17 pub connect_timeout: u64,
19}
20
21impl Default for RedisConfig {
22 fn default() -> Self {
23 Self {
24 host: "localhost".to_string(),
25 port: 6379,
26 username: Some("".to_string()),
27 password: Some("".to_string()),
28 database: 0,
29 max_pool_size: 20,
30 connect_timeout: 5,
31 }
32 }
33}
34
35impl RedisConfig {
36 pub fn connection_string(&self) -> String {
37 match (&self.username, &self.password) {
38 (Some(username), Some(password)) => {
39 format!(
40 "redis://{}:{}@{}:{}/{}",
41 username, password, self.host, self.port, self.database
42 )
43 }
44 (None, Some(password)) => {
45 format!("redis://:{}@{}:{}/{}", password, self.host, self.port, self.database)
46 }
47 _ => {
48 format!("redis://{}:{}/{}", self.host, self.port, self.database)
49 }
50 }
51 }
52}