feat: add MongoDB support with connection pooling and repository pattern
This commit is contained in:
26
src/main.rs
26
src/main.rs
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user