backbone_core/config/
database_config.rs1use serde::{Deserialize, Serialize};
6use std::time::Duration;
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct DatabaseConfig {
11 pub url: String,
13 #[serde(default = "default_max_connections")]
15 pub max_connections: u32,
16 #[serde(default = "default_min_connections")]
18 pub min_connections: u32,
19 #[serde(default = "default_connect_timeout")]
21 pub connect_timeout: u32,
22 #[serde(default = "default_idle_timeout")]
24 pub idle_timeout: u32,
25 #[serde(default = "default_max_lifetime")]
27 pub max_lifetime: u32,
28 #[serde(default = "default_ssl_mode")]
30 pub ssl_mode: Option<String>,
31}
32
33fn default_max_connections() -> u32 { 20 }
34fn default_min_connections() -> u32 { 5 }
35fn default_connect_timeout() -> u32 { 30 }
36fn default_idle_timeout() -> u32 { 600 }
37fn default_max_lifetime() -> u32 { 1800 }
38fn default_ssl_mode() -> Option<String> { Some("prefer".to_string()) }
39
40impl Default for DatabaseConfig {
41 fn default() -> Self {
42 Self {
43 url: "postgresql://postgres:password@localhost:5432/bersihirdb".to_string(),
44 max_connections: default_max_connections(),
45 min_connections: default_min_connections(),
46 connect_timeout: default_connect_timeout(),
47 idle_timeout: default_idle_timeout(),
48 max_lifetime: default_max_lifetime(),
49 ssl_mode: default_ssl_mode(),
50 }
51 }
52}
53
54impl DatabaseConfig {
55 pub fn connect_timeout_duration(&self) -> Duration {
57 Duration::from_secs(self.connect_timeout as u64)
58 }
59
60 pub fn idle_timeout_duration(&self) -> Duration {
62 Duration::from_secs(self.idle_timeout as u64)
63 }
64
65 pub fn max_lifetime_duration(&self) -> Duration {
67 Duration::from_secs(self.max_lifetime as u64)
68 }
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct CacheConfig {
74 #[serde(default = "default_cache_driver")]
76 pub driver: String,
77 #[serde(default = "default_cache_url")]
79 pub url: String,
80 #[serde(default = "default_cache_max_connections")]
82 pub max_connections: Option<u32>,
83 #[serde(default = "default_cache_ttl")]
85 pub default_ttl: Option<u64>,
86}
87
88fn default_cache_driver() -> String { "redis".to_string() }
89fn default_cache_url() -> String { "redis://localhost:6379".to_string() }
90fn default_cache_max_connections() -> Option<u32> { Some(10) }
91fn default_cache_ttl() -> Option<u64> { Some(3600) }
92
93impl Default for CacheConfig {
94 fn default() -> Self {
95 Self {
96 driver: default_cache_driver(),
97 url: default_cache_url(),
98 max_connections: default_cache_max_connections(),
99 default_ttl: default_cache_ttl(),
100 }
101 }
102}
103
104impl CacheConfig {
105 pub fn ttl_duration(&self) -> Option<Duration> {
107 self.default_ttl.map(Duration::from_secs)
108 }
109}