gitdataai/libs/config/database.rs
2026-04-14 19:02:01 +08:00

71 lines
2.6 KiB
Rust

use crate::AppConfig;
impl AppConfig {
pub fn database_url(&self) -> anyhow::Result<String> {
if let Some(url) = self.env.get("APP_DATABASE_URL") {
return Ok(url.to_string());
}
Err(anyhow::anyhow!("APP_DATABASE_URL not found"))
}
pub fn database_max_connections(&self) -> anyhow::Result<u32> {
if let Some(max_connections) = self.env.get("APP_DATABASE_MAX_CONNECTIONS") {
return Ok(max_connections.parse::<u32>()?);
}
Ok(10)
}
pub fn database_min_connections(&self) -> anyhow::Result<u32> {
if let Some(min_connections) = self.env.get("APP_DATABASE_MIN_CONNECTIONS") {
return Ok(min_connections.parse::<u32>()?);
}
Ok(2)
}
pub fn database_idle_timeout(&self) -> anyhow::Result<u64> {
if let Some(idle_timeout) = self.env.get("APP_DATABASE_IDLE_TIMEOUT") {
return Ok(idle_timeout.parse::<u64>()?);
}
Ok(60000)
}
pub fn database_max_lifetime(&self) -> anyhow::Result<u64> {
if let Some(max_lifetime) = self.env.get("APP_DATABASE_MAX_LIFETIME") {
return Ok(max_lifetime.parse::<u64>()?);
}
Ok(300000)
}
pub fn database_connection_timeout(&self) -> anyhow::Result<u64> {
if let Some(connection_timeout) = self.env.get("APP_DATABASE_CONNECTION_TIMEOUT") {
return Ok(connection_timeout.parse::<u64>()?);
}
Ok(5000)
}
pub fn database_schema_search_path(&self) -> anyhow::Result<String> {
if let Some(schema_search_path) = self.env.get("APP_DATABASE_SCHEMA_SEARCH_PATH") {
return Ok(schema_search_path.to_string());
}
Ok("public".to_string())
}
pub fn database_read_replicas(&self) -> anyhow::Result<Vec<String>> {
if let Some(replicas) = self.env.get("APP_DATABASE_REPLICAS") {
return Ok(replicas.split(',').map(|s| s.to_string()).collect());
}
Ok(vec![])
}
pub fn database_health_check_interval(&self) -> anyhow::Result<u64> {
if let Some(interval) = self.env.get("APP_DATABASE_HEALTH_CHECK_INTERVAL") {
return Ok(interval.parse::<u64>()?);
}
Ok(30)
}
pub fn database_retry_attempts(&self) -> anyhow::Result<u32> {
if let Some(attempts) = self.env.get("APP_DATABASE_RETRY_ATTEMPTS") {
return Ok(attempts.parse::<u32>()?);
}
Ok(3)
}
pub fn database_retry_delay(&self) -> anyhow::Result<u64> {
if let Some(delay) = self.env.get("APP_DATABASE_RETRY_DELAY") {
return Ok(delay.parse::<u64>()?);
}
Ok(5)
}
}