Rename employee-tracking-backend to purenotify_backend and add OFFLINE
mode - Rename crate and update dependencies to newer versions - Add OFFLINE runtime mode for loopback-only server without DB - Refactor state handling with typed Axum routers and state injection - Rename mongodb module to mongo and fix imports accordingly - Update Cargo.lock with updated and removed dependencies - Remove no-auth feature and related code - Simplify health and user routes to generic state parameter Rename backend to purenotify_backend and add OFFLINE mode Use OFFLINE env var to run server without DB, binding to loopback only. Rename mongodb module to mongo and update dependencies. Update dependencies and fix router state handling.
This commit is contained in:
82
src/main.rs
82
src/main.rs
@@ -1,9 +1,7 @@
|
||||
// src/main.rs
|
||||
|
||||
use std::process::exit;
|
||||
use std::sync::Arc;
|
||||
use std::{process::exit, sync::Arc};
|
||||
|
||||
use ::mongodb::Database;
|
||||
use axum::Router;
|
||||
use dotenvy::dotenv;
|
||||
use tokio::signal;
|
||||
@@ -13,13 +11,14 @@ use tracing_subscriber::{EnvFilter, fmt, prelude::*};
|
||||
|
||||
mod config;
|
||||
mod handlers;
|
||||
mod mongodb;
|
||||
mod mongo; // local module wrapping the Mongo client
|
||||
mod routes;
|
||||
|
||||
use ::mongodb::Database; // external crate (absolute path avoids name clash)
|
||||
use config::Config;
|
||||
use mongodb::MongoDb;
|
||||
use mongo::MongoDb; // your wrapper
|
||||
|
||||
// Shared application state
|
||||
// Shared application state for online mode
|
||||
pub struct AppState {
|
||||
pub db: Database,
|
||||
pub config: Config,
|
||||
@@ -27,51 +26,86 @@ pub struct AppState {
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Load environment variables from .env file
|
||||
// Load .env early
|
||||
dotenv().ok();
|
||||
|
||||
// Initialize tracing
|
||||
// Tracing with a safe fallback if RUST_LOG is unset
|
||||
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
|
||||
tracing_subscriber::registry()
|
||||
.with(fmt::layer())
|
||||
.with(EnvFilter::from_env("RUST_LOG"))
|
||||
.with(env_filter)
|
||||
.init();
|
||||
|
||||
// Load config
|
||||
let config = match Config::from_env() {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
error!("Failed to load config: {}", e);
|
||||
error!("Failed to load config: {e}");
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
// Connect to MongoDB using the config
|
||||
let mongodb = match MongoDb::connect(&config).await {
|
||||
// Runtime OFFLINE switch: true if OFFLINE is 1/true/yes/on (case-insensitive)
|
||||
let offline = std::env::var("OFFLINE")
|
||||
.ok()
|
||||
.map(|v| matches!(v.to_ascii_lowercase().as_str(), "1" | "true" | "yes" | "on"))
|
||||
.unwrap_or(false);
|
||||
|
||||
if offline {
|
||||
// Enforce loopback binding while offline
|
||||
if !config.bind_address.ip().is_loopback() {
|
||||
error!(
|
||||
"OFFLINE=true requires binding to a loopback address (e.g., 127.0.0.1:<port> or [::1]:<port>), got {}",
|
||||
config.bind_address
|
||||
);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
info!("OFFLINE mode enabled — not connecting to MongoDB");
|
||||
info!("Server starting on {}", config.bind_address);
|
||||
|
||||
// Health-only, no state. Subrouter is typed to `()`.
|
||||
let app = Router::new()
|
||||
.nest("/health", routes::health::health::health_routes::<()>())
|
||||
.layer(TraceLayer::new_for_http());
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(config.bind_address)
|
||||
.await
|
||||
.unwrap();
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(shutdown_signal())
|
||||
.await
|
||||
.unwrap();
|
||||
return;
|
||||
}
|
||||
|
||||
// --- Online (DB-enabled) path ---
|
||||
let mongo = match MongoDb::connect(&config).await {
|
||||
Ok(db) => db,
|
||||
Err(e) => {
|
||||
error!("Failed to connect to MongoDB: {}", e);
|
||||
error!("Failed to connect to MongoDB: {e}");
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
// Create shared state
|
||||
let _shared_state = Arc::new(AppState {
|
||||
db: mongodb.database,
|
||||
let shared_state = Arc::new(AppState {
|
||||
db: mongo.database,
|
||||
config: config.clone(),
|
||||
});
|
||||
|
||||
#[cfg(feature = "no-auth")]
|
||||
info!("NO-AUTH MODE ENABLED");
|
||||
|
||||
info!("Server starting on {}", config.bind_address);
|
||||
|
||||
// Build the Axum router
|
||||
let app = Router::new()
|
||||
.nest("/health", routes::health::health::health_routes())
|
||||
.nest("/user", routes::user::user::user_routes())
|
||||
// Build subrouters typed with the same state as the root
|
||||
let health_router = routes::health::health::health_routes::<Arc<AppState>>();
|
||||
let user_router = routes::user::user::user_routes::<Arc<AppState>>();
|
||||
|
||||
// Root router typed with state; set state once on the root
|
||||
let app = Router::<Arc<AppState>>::new()
|
||||
.nest("/health", health_router)
|
||||
.nest("/user", user_router)
|
||||
.with_state(shared_state)
|
||||
.layer(TraceLayer::new_for_http());
|
||||
|
||||
// Run the server
|
||||
let listener = tokio::net::TcpListener::bind(config.bind_address)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Reference in New Issue
Block a user