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:
aecsocket
2026-02-19 17:33:41 +00:00
committed by GitHub
parent b6b4bc21f1
commit ec81bcb13c
49 changed files with 636 additions and 661 deletions

View File

@@ -3,6 +3,7 @@ use super::{
environment::LocalService,
};
use crate::LabrinthConfig;
use crate::env::ENV;
use actix_web::{App, dev::ServiceResponse, test};
use async_trait::async_trait;
use std::rc::Rc;
@@ -46,10 +47,7 @@ impl Api for ApiV2 {
async fn reset_search_index(&self) -> ServiceResponse {
let req = actix_web::test::TestRequest::post()
.uri("/v2/admin/_force_reindex")
.append_header((
"Modrinth-Admin",
dotenvy::var("LABRINTH_ADMIN_KEY").unwrap(),
))
.append_header(("Modrinth-Admin", ENV.LABRINTH_ADMIN_KEY.clone()))
.to_request();
self.call(req).await
}

View File

@@ -3,6 +3,7 @@ use super::{
environment::LocalService,
};
use crate::LabrinthConfig;
use crate::env::ENV;
use actix_web::{App, dev::ServiceResponse, test};
use async_trait::async_trait;
use std::rc::Rc;
@@ -51,10 +52,7 @@ impl Api for ApiV3 {
async fn reset_search_index(&self) -> ServiceResponse {
let req = actix_web::test::TestRequest::post()
.uri("/_internal/admin/_force_reindex")
.append_header((
"Modrinth-Admin",
dotenvy::var("LABRINTH_ADMIN_KEY").unwrap(),
))
.append_header(("Modrinth-Admin", ENV.LABRINTH_ADMIN_KEY.clone()))
.to_request();
self.call(req).await
}

View File

@@ -1,6 +1,7 @@
use crate::database::PgPool;
use crate::database::redis::RedisPool;
use crate::database::{MIGRATOR, ReadOnlyPgPool};
use crate::env::ENV;
use crate::search;
use sqlx::postgres::PgPoolOptions;
use std::time::Duration;
@@ -57,15 +58,14 @@ impl TemporaryDatabase {
let temp_database_name = generate_random_name("labrinth_tests_db_");
println!("Creating temporary database: {}", &temp_database_name);
let database_url =
dotenvy::var("DATABASE_URL").expect("No database URL");
let database_url = &ENV.DATABASE_URL;
// Create the temporary (and template database, if needed)
Self::create_temporary(&database_url, &temp_database_name).await;
Self::create_temporary(database_url, &temp_database_name).await;
// Pool to the temporary database
let mut temporary_url =
Url::parse(&database_url).expect("Invalid database URL");
Url::parse(database_url).expect("Invalid database URL");
temporary_url.set_path(&format!("/{}", &temp_database_name));
let temp_db_url = temporary_url.to_string();
@@ -139,10 +139,8 @@ impl TemporaryDatabase {
}
// Switch to template
let url =
dotenvy::var("DATABASE_URL").expect("No database URL");
let mut template_url =
Url::parse(&url).expect("Invalid database URL");
let mut template_url = Url::parse(&ENV.DATABASE_URL)
.expect("Invalid database URL");
template_url.set_path(&format!("/{TEMPLATE_DATABASE_NAME}"));
let pool = sqlx::PgPool::connect(template_url.as_str())
@@ -234,11 +232,10 @@ impl TemporaryDatabase {
// If a temporary db is created, it must be cleaned up with cleanup.
// This means that dbs will only 'remain' if a test fails (for examination of the db), and will be cleaned up otherwise.
pub async fn cleanup(mut self) {
let database_url =
dotenvy::var("DATABASE_URL").expect("No database URL");
let database_url = &ENV.DATABASE_URL;
self.pool.close().await;
self.pool = sqlx::PgPool::connect(&database_url)
self.pool = sqlx::PgPool::connect(database_url)
.await
.map(PgPool::from)
.expect("Connection to main database failed");

View File

@@ -1,8 +1,9 @@
use crate::env::ENV;
use crate::queue::email::EmailQueue;
use crate::util::anrok;
use crate::util::gotenberg::GotenbergClient;
use crate::{LabrinthConfig, file_hosting};
use crate::{check_env_vars, clickhouse};
use crate::{clickhouse, env};
use std::sync::Arc;
pub mod api_common;
@@ -22,12 +23,8 @@ pub mod search;
// If making a test, you should probably use environment::TestEnvironment::build() (which calls this)
pub async fn setup(db: &database::TemporaryDatabase) -> LabrinthConfig {
println!("Setting up labrinth config");
dotenvy::dotenv().ok();
if check_env_vars() {
println!("Some environment variables are missing!");
}
env::init().expect("failed to initialize environment variables");
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
@@ -39,8 +36,7 @@ pub async fn setup(db: &database::TemporaryDatabase) -> LabrinthConfig {
Arc::new(file_hosting::MockHost::new());
let mut clickhouse = clickhouse::init_client().await.unwrap();
let stripe_client =
stripe::Client::new(dotenvy::var("STRIPE_API_KEY").unwrap());
let stripe_client = stripe::Client::new(ENV.STRIPE_API_KEY.clone());
let anrok_client = anrok::Client::from_env().unwrap();
let email_queue =