refactor: allow optional database in setup, use derived default

This commit is contained in:
Ryan Walters
2025-09-18 22:58:38 -05:00
parent e2c725cb95
commit cc06cd88a1
10 changed files with 95 additions and 59 deletions
+12 -2
View File
@@ -44,6 +44,16 @@ pub struct AppState {
impl AppState {
pub async fn new(config: Config, auth: AuthRegistry, db: PgPool, shutdown_notify: Arc<Notify>) -> Self {
Self::new_with_database(config, auth, db, shutdown_notify, true).await
}
pub async fn new_with_database(
config: Config,
auth: AuthRegistry,
db: PgPool,
shutdown_notify: Arc<Notify>,
use_database: bool,
) -> Self {
let jwt_secret = config.jwt_secret.clone();
// Initialize image storage
@@ -67,8 +77,8 @@ impl AppState {
healthchecker_task: Arc::new(RwLock::new(None)),
};
// Start the healthchecker task
{
// Start the healthchecker task only if database is being used
if use_database {
let health_state = app_state.health.clone();
let db_pool = app_state.db.clone();
let healthchecker_task = app_state.healthchecker_task.clone();
+1 -1
View File
@@ -51,7 +51,7 @@ impl DiscordProvider {
Arc::new(Self {
client,
http,
pkce: PkceManager::new(),
pkce: PkceManager::default(),
})
}
+1 -1
View File
@@ -64,7 +64,7 @@ impl GitHubProvider {
Arc::new(Self {
client,
http,
pkce: PkceManager::new(),
pkce: PkceManager::default(),
})
}
}
+1 -8
View File
@@ -10,6 +10,7 @@ pub struct PkceRecord {
pub created_at: Instant,
}
#[derive(Default)]
pub struct PkceManager {
pkce: DashMap<String, PkceRecord>,
last_purge_at_secs: AtomicU32,
@@ -17,14 +18,6 @@ pub struct PkceManager {
}
impl PkceManager {
pub fn new() -> Self {
Self {
pkce: DashMap::new(),
last_purge_at_secs: AtomicU32::new(0),
pkce_additions: AtomicU32::new(0),
}
}
pub fn generate_challenge(&self) -> (PkceCodeChallenge, String) {
let (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256();
trace!("PKCE challenge generated");
+12 -7
View File
@@ -3,14 +3,19 @@ use tracing::{info, warn};
pub type PgPool = Pool<Postgres>;
pub async fn create_pool(database_url: &str, max_connections: u32) -> PgPool {
info!("Connecting to PostgreSQL");
PgPoolOptions::new()
.max_connections(max_connections)
.connect(database_url)
.await
.unwrap_or_else(|e| {
pub async fn create_pool(immediate: bool, database_url: &str, max_connections: u32) -> PgPool {
info!(immediate, "Connecting to PostgreSQL");
let options = PgPoolOptions::new().max_connections(max_connections);
if immediate {
options.connect(database_url).await.unwrap_or_else(|e| {
warn!(error = %e, "Failed to connect to PostgreSQL");
panic!("database connect failed: {}", e);
})
} else {
options
.connect_lazy(database_url)
.expect("Failed to create lazy database pool")
}
}
+1
View File
@@ -35,6 +35,7 @@ pub async fn find_user_by_email(pool: &sqlx::PgPool, email: &str) -> Result<Opti
.await
}
#[allow(clippy::too_many_arguments)]
pub async fn link_oauth_account(
pool: &sqlx::PgPool,
user_id: i64,
+1 -1
View File
@@ -40,7 +40,7 @@ async fn main() {
let addr = std::net::SocketAddr::new(config.host, config.port);
let shutdown_timeout = std::time::Duration::from_secs(config.shutdown_timeout_seconds as u64);
let auth = AuthRegistry::new(&config).expect("auth initializer");
let db = data::pool::create_pool(&config.database_url, 10).await;
let db = data::pool::create_pool(true, &config.database_url, 10).await;
// Run database migrations at startup
if let Err(e) = sqlx::migrate!("./migrations").run(&db).await {
+1 -1
View File
@@ -377,7 +377,7 @@ pub async fn health_handler(
) -> axum::response::Response {
// Force health check in debug mode
#[cfg(debug_assertions)]
if params.get("force").is_some() {
if params.contains_key("force") {
app_state.check_health().await;
}