feat: add MongoDB support with connection pooling and repository pattern

This commit is contained in:
2025-08-16 06:38:30 -04:00
parent 8a05f4edac
commit ed612bd717
12 changed files with 1226 additions and 49 deletions

View File

@@ -1,7 +1,9 @@
// src/main.rs
use std::process::exit;
use std::sync::Arc;
use ::mongodb::Database;
use axum::Router;
use dotenvy::dotenv;
use tokio::signal;
@@ -11,9 +13,17 @@ use tracing_subscriber::{EnvFilter, fmt, prelude::*};
mod config;
mod handlers;
mod mongodb;
mod routes;
use config::Config;
use mongodb::MongoDb;
// Shared application state
pub struct AppState {
pub db: Database,
pub config: Config,
}
#[tokio::main]
async fn main() {
@@ -35,6 +45,21 @@ async fn main() {
}
};
// Connect to MongoDB using the config
let mongodb = match MongoDb::connect(&config).await {
Ok(db) => db,
Err(e) => {
error!("Failed to connect to MongoDB: {}", e);
exit(1);
}
};
// Create shared state
let _shared_state = Arc::new(AppState {
db: mongodb.database,
config: config.clone(),
});
#[cfg(feature = "no-auth")]
info!("NO-AUTH MODE ENABLED");
@@ -42,7 +67,6 @@ async fn main() {
// Build the Axum router
let app = Router::new()
// .nest("/health", routes::health::health::health_routes())
.nest("/health", routes::health::health::health_routes())
.nest("/user", routes::user::user::user_routes())
.layer(TraceLayer::new_for_http());