You've already forked AstralRinth
Improve environment variable handling and reading (#5389)
* wip: better env var reading * move most env vars to env.rs * migrate more env vars * more migration * more migrations * More migration * 🦀 dotenvy is gone (almost) * 🦀 dotenvy is gone 🦀 * Fix mural source account env var handling * Remove defaults from admin key vars * dummy commit to update github pr * fix ci
This commit is contained in:
@@ -13,6 +13,8 @@ pub type PgTransaction<'c> = sqlx_tracing::Transaction<'c, Postgres>;
|
||||
pub use sqlx_tracing::Acquire;
|
||||
pub use sqlx_tracing::Executor;
|
||||
|
||||
use crate::env::ENV;
|
||||
|
||||
// pub type PgPool = sqlx::PgPool;
|
||||
// pub type PgTransaction<'c> = sqlx::Transaction<'c, Postgres>;
|
||||
// pub use sqlx::Acquire;
|
||||
@@ -50,57 +52,27 @@ impl DerefMut for ReadOnlyPgPool {
|
||||
|
||||
pub async fn connect_all() -> Result<(PgPool, ReadOnlyPgPool), sqlx::Error> {
|
||||
info!("Initializing database connection");
|
||||
let database_url =
|
||||
dotenvy::var("DATABASE_URL").expect("`DATABASE_URL` not in .env");
|
||||
let database_url = &ENV.DATABASE_URL;
|
||||
|
||||
let acquire_timeout =
|
||||
dotenvy::var("DATABASE_ACQUIRE_TIMEOUT_MS")
|
||||
.ok()
|
||||
.map_or_else(
|
||||
|| Duration::from_millis(30000),
|
||||
|x| {
|
||||
Duration::from_millis(x.parse::<u64>().expect(
|
||||
"DATABASE_ACQUIRE_TIMEOUT_MS must be a valid u64",
|
||||
))
|
||||
},
|
||||
);
|
||||
Duration::from_millis(ENV.DATABASE_ACQUIRE_TIMEOUT_MS);
|
||||
|
||||
let pool = PgPoolOptions::new()
|
||||
.acquire_timeout(acquire_timeout)
|
||||
.min_connections(
|
||||
dotenvy::var("DATABASE_MIN_CONNECTIONS")
|
||||
.ok()
|
||||
.and_then(|x| x.parse().ok())
|
||||
.unwrap_or(0),
|
||||
)
|
||||
.max_connections(
|
||||
dotenvy::var("DATABASE_MAX_CONNECTIONS")
|
||||
.ok()
|
||||
.and_then(|x| x.parse().ok())
|
||||
.unwrap_or(16),
|
||||
)
|
||||
.min_connections(ENV.DATABASE_MIN_CONNECTIONS)
|
||||
.max_connections(ENV.DATABASE_MAX_CONNECTIONS)
|
||||
.max_lifetime(Some(Duration::from_secs(60 * 60)))
|
||||
.connect(&database_url)
|
||||
.connect(database_url)
|
||||
.await?;
|
||||
let pool = PgPool::from(pool);
|
||||
|
||||
if let Ok(url) = dotenvy::var("READONLY_DATABASE_URL") {
|
||||
if !ENV.READONLY_DATABASE_URL.is_empty() {
|
||||
let ro_pool = PgPoolOptions::new()
|
||||
.acquire_timeout(acquire_timeout)
|
||||
.min_connections(
|
||||
dotenvy::var("READONLY_DATABASE_MIN_CONNECTIONS")
|
||||
.ok()
|
||||
.and_then(|x| x.parse().ok())
|
||||
.unwrap_or(0),
|
||||
)
|
||||
.max_connections(
|
||||
dotenvy::var("READONLY_DATABASE_MAX_CONNECTIONS")
|
||||
.ok()
|
||||
.and_then(|x| x.parse().ok())
|
||||
.unwrap_or(1),
|
||||
)
|
||||
.min_connections(ENV.READONLY_DATABASE_MIN_CONNECTIONS)
|
||||
.max_connections(ENV.READONLY_DATABASE_MAX_CONNECTIONS)
|
||||
.max_lifetime(Some(Duration::from_secs(60 * 60)))
|
||||
.connect(&url)
|
||||
.connect(&ENV.READONLY_DATABASE_URL)
|
||||
.await?;
|
||||
let ro_pool = PgPool::from(ro_pool);
|
||||
|
||||
@@ -112,8 +84,7 @@ pub async fn connect_all() -> Result<(PgPool, ReadOnlyPgPool), sqlx::Error> {
|
||||
}
|
||||
|
||||
pub async fn check_for_migrations() -> eyre::Result<()> {
|
||||
let uri =
|
||||
dotenvy::var("DATABASE_URL").wrap_err("`DATABASE_URL` not in .env")?;
|
||||
let uri = &ENV.DATABASE_URL;
|
||||
let uri = uri.as_str();
|
||||
if !Postgres::database_exists(uri)
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user