Make database connections configurable by environment variables (#768)
* Make env DB_MAX_SIZE and DB_MIN_IDLE recognizable * Make database max size and minimum idle configurable * Restore file permission * Fail fast
This commit is contained in:
parent
dabe904642
commit
3be842c653
@ -11,6 +11,8 @@ pub struct Config {
|
|||||||
pub base_url: String,
|
pub base_url: String,
|
||||||
pub database_url: String,
|
pub database_url: String,
|
||||||
pub db_name: &'static str,
|
pub db_name: &'static str,
|
||||||
|
pub db_max_size: Option<u32>,
|
||||||
|
pub db_min_idle: Option<u32>,
|
||||||
pub search_index: String,
|
pub search_index: String,
|
||||||
pub rocket: Result<RocketConfig, RocketError>,
|
pub rocket: Result<RocketConfig, RocketError>,
|
||||||
pub logo: LogoConfig,
|
pub logo: LogoConfig,
|
||||||
@ -193,6 +195,14 @@ lazy_static! {
|
|||||||
var("ROCKET_PORT").unwrap_or_else(|_| "7878".to_owned())
|
var("ROCKET_PORT").unwrap_or_else(|_| "7878".to_owned())
|
||||||
)),
|
)),
|
||||||
db_name: DB_NAME,
|
db_name: DB_NAME,
|
||||||
|
db_max_size: var("DB_MAX_SIZE").map_or(None, |s| Some(
|
||||||
|
s.parse::<u32>()
|
||||||
|
.expect("Couldn't parse DB_MAX_SIZE into u32")
|
||||||
|
)),
|
||||||
|
db_min_idle: var("DB_MIN_IDLE").map_or(None, |s| Some(
|
||||||
|
s.parse::<u32>()
|
||||||
|
.expect("Couldn't parse DB_MIN_IDLE into u32")
|
||||||
|
)),
|
||||||
#[cfg(feature = "postgres")]
|
#[cfg(feature = "postgres")]
|
||||||
database_url: var("DATABASE_URL")
|
database_url: var("DATABASE_URL")
|
||||||
.unwrap_or_else(|_| format!("postgres://plume:plume@localhost/{}", DB_NAME)),
|
.unwrap_or_else(|_| format!("postgres://plume:plume@localhost/{}", DB_NAME)),
|
||||||
|
@ -55,10 +55,13 @@ fn init_pool() -> Option<DbPool> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let manager = ConnectionManager::<Connection>::new(CONFIG.database_url.as_str());
|
let manager = ConnectionManager::<Connection>::new(CONFIG.database_url.as_str());
|
||||||
let pool = DbPool::builder()
|
let mut builder = DbPool::builder()
|
||||||
.connection_customizer(Box::new(PragmaForeignKey))
|
.connection_customizer(Box::new(PragmaForeignKey))
|
||||||
.build(manager)
|
.min_idle(CONFIG.db_min_idle);
|
||||||
.ok()?;
|
if let Some(max_size) = CONFIG.db_max_size {
|
||||||
|
builder = builder.max_size(max_size);
|
||||||
|
};
|
||||||
|
let pool = builder.build(manager).ok()?;
|
||||||
Instance::cache_local(&pool.get().unwrap());
|
Instance::cache_local(&pool.get().unwrap());
|
||||||
Some(pool)
|
Some(pool)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user