Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/labrinth/.env.docker-compose
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,6 @@ ARCHON_URL=none
MURALPAY_API_URL=https://api.muralpay.com
MURALPAY_API_KEY=none
MURALPAY_TRANSFER_API_KEY=none
MURALPAY_SOURCE_ACCOUNT_ID=none
MURALPAY_SOURCE_ACCOUNT_ID=00000000-0000-0000-0000-000000000000

DEFAULT_AFFILIATE_REVENUE_SPLIT=0.1
2 changes: 1 addition & 1 deletion apps/labrinth/.env.local
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,6 @@ ARCHON_URL=none
MURALPAY_API_URL=https://api-staging.muralpay.com
MURALPAY_API_KEY=none
MURALPAY_TRANSFER_API_KEY=none
MURALPAY_SOURCE_ACCOUNT_ID=none
MURALPAY_SOURCE_ACCOUNT_ID=00000000-0000-0000-0000-000000000000

DEFAULT_AFFILIATE_REVENUE_SPLIT=0.1
3 changes: 2 additions & 1 deletion apps/labrinth/src/auth/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::AuthProvider;
use crate::auth::AuthenticationError;
use crate::database::models::{DBUser, user_item};
use crate::database::redis::RedisPool;
use crate::env::ENV;
use crate::models::pats::Scopes;
use crate::models::users::User;
use crate::queue::session::AuthQueue;
Expand Down Expand Up @@ -146,7 +147,7 @@ where
user_item::DBUser::get_id(session.user_id, executor, redis)
.await?;

let rate_limit_ignore = dotenvy::var("RATE_LIMIT_IGNORE_KEY")?;
let rate_limit_ignore = &ENV.RATE_LIMIT_IGNORE_KEY;
if req
.headers()
.get("x-ratelimit-key")
Expand Down
14 changes: 7 additions & 7 deletions apps/labrinth/src/clickhouse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ mod fetch;

pub use fetch::*;

use crate::env::ENV;

pub async fn init_client() -> clickhouse::error::Result<clickhouse::Client> {
init_client_with_database(&dotenvy::var("CLICKHOUSE_DATABASE").unwrap())
.await
init_client_with_database(&ENV.CLICKHOUSE_DATABASE).await
}

pub async fn init_client_with_database(
Expand All @@ -24,9 +25,9 @@ pub async fn init_client_with_database(
.build(https_connector);

clickhouse::Client::with_http_client(hyper_client)
.with_url(dotenvy::var("CLICKHOUSE_URL").unwrap())
.with_user(dotenvy::var("CLICKHOUSE_USER").unwrap())
.with_password(dotenvy::var("CLICKHOUSE_PASSWORD").unwrap())
.with_url(&ENV.CLICKHOUSE_URL)
.with_user(&ENV.CLICKHOUSE_USER)
.with_password(&ENV.CLICKHOUSE_PASSWORD)
.with_validation(false)
};

Expand All @@ -35,8 +36,7 @@ pub async fn init_client_with_database(
.execute()
.await?;

let clickhouse_replicated =
dotenvy::var("CLICKHOUSE_REPLICATED").unwrap() == "true";
let clickhouse_replicated = ENV.CLICKHOUSE_REPLICATED;
let cluster_line = if clickhouse_replicated {
"ON cluster '{cluster}'"
} else {
Expand Down
53 changes: 12 additions & 41 deletions apps/labrinth/src/database/postgres_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down
32 changes: 8 additions & 24 deletions apps/labrinth/src/database/redis/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::env::ENV;

use super::models::DatabaseError;
use ariadne::ids::base62_impl::{parse_base62, to_base62};
use chrono::{TimeZone, Utc};
Expand Down Expand Up @@ -42,44 +44,26 @@ impl RedisPool {
// testing pool uses a hashmap to mimic redis behaviour for very small data sizes (ie: tests)
// PANICS: production pool will panic if redis url is not set
pub fn new(meta_namespace: impl Into<Arc<str>>) -> Self {
let wait_timeout =
dotenvy::var("REDIS_WAIT_TIMEOUT_MS").ok().map_or_else(
|| Duration::from_millis(15000),
|x| {
Duration::from_millis(
x.parse::<u64>().expect(
"REDIS_WAIT_TIMEOUT_MS must be a valid u64",
),
)
},
);

let url = dotenvy::var("REDIS_URL").expect("Redis URL not set");
let wait_timeout = Duration::from_millis(ENV.REDIS_WAIT_TIMEOUT_MS);

let url = &ENV.REDIS_URL;
let pool = Config::from_url(url.clone())
.builder()
.expect("Error building Redis pool")
.max_size(
dotenvy::var("REDIS_MAX_CONNECTIONS")
.ok()
.and_then(|x| x.parse().ok())
.unwrap_or(10000),
)
.max_size(ENV.REDIS_MAX_CONNECTIONS as usize)
.wait_timeout(Some(wait_timeout))
.runtime(Runtime::Tokio1)
.build()
.expect("Redis connection failed");

let pool = RedisPool {
url,
url: url.clone(),
pool,
cache_list: Arc::new(DashMap::with_capacity(2048)),
meta_namespace: meta_namespace.into(),
};

let redis_min_connections = dotenvy::var("REDIS_MIN_CONNECTIONS")
.ok()
.and_then(|x| x.parse::<usize>().ok())
.unwrap_or(0);
let redis_min_connections = ENV.REDIS_MIN_CONNECTIONS;
let spawn_min_connections = (0..redis_min_connections)
.map(|_| {
let pool = pool.clone();
Expand Down
Loading