54 lines
1.9 KiB
Rust
54 lines
1.9 KiB
Rust
use config::{Config, ConfigError};
|
|
use tracing::warn;
|
|
|
|
use super::{
|
|
FromConfig,
|
|
key::{DATABASE_MAX_CONNECTIONS_KEY, DATABASE_MIGRATE_ON_STARTUP_KEY},
|
|
};
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct DatabaseSettings {
|
|
pub url: String,
|
|
pub max_connections: u32,
|
|
pub migrate_on_startup: bool,
|
|
}
|
|
|
|
impl FromConfig for DatabaseSettings {
|
|
fn from_config(_config: &Config) -> Result<Self, String> {
|
|
Ok(DatabaseSettings {
|
|
url: _config
|
|
.get_string(super::key::DATABASE_URL_KEY)
|
|
.map_err(|op| match op {
|
|
ConfigError::NotFound(_) => "Database URL not found in configuration".into(),
|
|
err => {
|
|
format!("Failed to read Database URL from configuration {err}")
|
|
}
|
|
})?,
|
|
max_connections: _config
|
|
.get_int(DATABASE_MAX_CONNECTIONS_KEY)
|
|
.unwrap_or_else(|err| {
|
|
const DEFAULT_MAX_CONNECTIONS: i64 = 10;
|
|
warn!(
|
|
"{} not set or invalid in configuration, defaulting to {}. Error: {}",
|
|
DATABASE_MAX_CONNECTIONS_KEY, DEFAULT_MAX_CONNECTIONS, err
|
|
);
|
|
DEFAULT_MAX_CONNECTIONS
|
|
}) as u32,
|
|
migrate_on_startup: _config
|
|
.get_bool(DATABASE_MIGRATE_ON_STARTUP_KEY)
|
|
.unwrap_or_else(|err| {
|
|
const DEFAULT_MIGRATE_ON_STARTUP: bool = true;
|
|
warn!(
|
|
"{} not set or invalid in configuration, defaulting to {}. Error: {}",
|
|
DATABASE_MIGRATE_ON_STARTUP_KEY, DEFAULT_MIGRATE_ON_STARTUP, err
|
|
);
|
|
DEFAULT_MIGRATE_ON_STARTUP
|
|
}),
|
|
})
|
|
}
|
|
|
|
fn validate(&self) -> Result<(), String> {
|
|
Ok(())
|
|
}
|
|
}
|