feat: init
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
use crate::config::AppConfig;
|
||||
use crate::error::{AppError, AppResult};
|
||||
|
||||
impl AppConfig {
|
||||
pub fn database_url(&self) -> AppResult<String> {
|
||||
if let Some(url) = self.get_env::<String>("APP_DATABASE_URL")? {
|
||||
return Ok(url);
|
||||
}
|
||||
if let Some(url) = self.get_env::<String>("DATABASE_URL")? {
|
||||
return Ok(url);
|
||||
}
|
||||
Err(AppError::Config(
|
||||
"Neither APP_DATABASE_URL nor DATABASE_URL is set".into(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn database_max_connections(&self) -> AppResult<u32> {
|
||||
self.get_env_or("APP_DATABASE_MAX_CONNECTIONS", 10)
|
||||
}
|
||||
|
||||
pub fn database_min_connections(&self) -> AppResult<u32> {
|
||||
self.get_env_or("APP_DATABASE_MIN_CONNECTIONS", 2)
|
||||
}
|
||||
|
||||
pub fn database_idle_timeout(&self) -> AppResult<u64> {
|
||||
self.get_env_or("APP_DATABASE_IDLE_TIMEOUT", 600)
|
||||
}
|
||||
|
||||
pub fn database_max_lifetime(&self) -> AppResult<u64> {
|
||||
self.get_env_or("APP_DATABASE_MAX_LIFETIME", 3600)
|
||||
}
|
||||
|
||||
pub fn database_connection_timeout(&self) -> AppResult<u64> {
|
||||
self.get_env_or("APP_DATABASE_CONNECTION_TIMEOUT", 8)
|
||||
}
|
||||
|
||||
pub fn database_schema_search_path(&self) -> AppResult<String> {
|
||||
self.get_env_or("APP_DATABASE_SCHEMA_SEARCH_PATH", "public".to_string())
|
||||
}
|
||||
|
||||
pub fn database_read_write_split(&self) -> AppResult<bool> {
|
||||
self.get_env_or("APP_DATABASE_READ_WRITE_SPLIT", false)
|
||||
}
|
||||
|
||||
pub fn database_read_replicas(&self) -> AppResult<Vec<String>> {
|
||||
match self.get_env::<String>("APP_DATABASE_REPLICAS")? {
|
||||
Some(s) if !s.is_empty() => Ok(s
|
||||
.split(',')
|
||||
.map(|u| u.trim().to_string())
|
||||
.filter(|u| !u.is_empty())
|
||||
.collect()),
|
||||
_ => Ok(Vec::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn database_replica_max_connections(&self) -> AppResult<u32> {
|
||||
self.get_env_or("APP_DATABASE_REPLICA_MAX_CONNECTIONS", 10)
|
||||
}
|
||||
|
||||
pub fn database_replica_min_connections(&self) -> AppResult<u32> {
|
||||
self.get_env_or("APP_DATABASE_REPLICA_MIN_CONNECTIONS", 2)
|
||||
}
|
||||
|
||||
pub fn database_replica_idle_timeout(&self) -> AppResult<u64> {
|
||||
self.get_env_or("APP_DATABASE_REPLICA_IDLE_TIMEOUT", 600)
|
||||
}
|
||||
|
||||
pub fn database_replica_max_lifetime(&self) -> AppResult<u64> {
|
||||
self.get_env_or("APP_DATABASE_REPLICA_MAX_LIFETIME", 3600)
|
||||
}
|
||||
|
||||
pub fn database_replica_connection_timeout(&self) -> AppResult<u64> {
|
||||
self.get_env_or("APP_DATABASE_REPLICA_CONNECTION_TIMEOUT", 8)
|
||||
}
|
||||
|
||||
pub fn database_health_check_interval(&self) -> AppResult<u64> {
|
||||
self.get_env_or("APP_DATABASE_HEALTH_CHECK_INTERVAL", 30)
|
||||
}
|
||||
|
||||
pub fn database_retry_attempts(&self) -> AppResult<u32> {
|
||||
self.get_env_or("APP_DATABASE_RETRY_ATTEMPTS", 3)
|
||||
}
|
||||
|
||||
pub fn database_retry_delay(&self) -> AppResult<u64> {
|
||||
self.get_env_or("APP_DATABASE_RETRY_DELAY", 5)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user