This commit is contained in:
dal 2025-08-26 14:01:15 -06:00
parent 0c91de708d
commit 7c9fbc65b0
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
1 changed files with 36 additions and 4 deletions

View File

@ -28,9 +28,13 @@ pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
#[tokio::main] #[tokio::main]
#[allow(unused)] #[allow(unused)]
async fn main() -> Result<(), anyhow::Error> { async fn main() -> Result<(), anyhow::Error> {
println!("Starting API server initialization...");
dotenv().ok(); dotenv().ok();
println!("Environment variables loaded from .env file");
// Initialize Infisical and load all secrets // Initialize Infisical and load all secrets
println!("Attempting to connect to Infisical...");
match init_secrets().await { match init_secrets().await {
Ok(_) => { Ok(_) => {
// Don't log here yet - tracing subscriber isn't initialized // Don't log here yet - tracing subscriber isn't initialized
@ -51,31 +55,40 @@ async fn main() -> Result<(), anyhow::Error> {
let environment = get_secret_or_default("ENVIRONMENT", "development").await; let environment = get_secret_or_default("ENVIRONMENT", "development").await;
let is_development = environment == "development"; let is_development = environment == "development";
println!("Environment: {}", environment);
ring::default_provider() ring::default_provider()
.install_default() .install_default()
.expect("Failed to install default crypto provider"); .expect("Failed to install default crypto provider");
println!("Crypto provider initialized");
// Initialize Sentry using our middleware helper // Initialize Sentry using our middleware helper
let _guard = init_sentry( let _guard = init_sentry(
"https://a417fbed1de30d2714a8afbe38d5bc1b@o4505360096428032.ingest.us.sentry.io/4507360721043456" "https://a417fbed1de30d2714a8afbe38d5bc1b@o4505360096428032.ingest.us.sentry.io/4507360721043456"
); );
println!("Sentry initialized");
// Set up the tracing subscriber with conditional Sentry integration // Set up the tracing subscriber with conditional Sentry integration
let log_level = get_secret_or_default("LOG_LEVEL", "warn").await.to_uppercase(); let log_level = get_secret_or_default("LOG_LEVEL", "warn").await.to_uppercase();
println!("Log level set to: {}", log_level);
let env_filter = let env_filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(log_level)); EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(log_level));
// Re-initialize the tracing subscriber with Sentry integration using our middleware helper // Re-initialize the tracing subscriber with Sentry integration using our middleware helper
init_tracing_subscriber(env_filter); init_tracing_subscriber(env_filter);
println!("Tracing subscriber initialized");
info!("Successfully initialized with secrets from Infisical"); info!("Successfully initialized with secrets from Infisical");
info!("Initializing database connection pools...");
if let Err(e) = init_pools().await { if let Err(e) = init_pools().await {
tracing::error!("Failed to initialize database pools: {}", e); tracing::error!("Failed to initialize database pools: {}", e);
eprintln!("CRITICAL: Database connection failed - {}", e);
eprintln!("Check DATABASE_URL and database availability");
return Ok(()); return Ok(());
} }
info!("Database pools initialized successfully");
// --- Start Stored Values Sync Job Scheduler --- // --- Start Stored Values Sync Job Scheduler ---
let scheduler = JobScheduler::new().await?; // Using `?` assuming main returns Result let scheduler = JobScheduler::new().await?; // Using `?` assuming main returns Result
@ -97,13 +110,19 @@ async fn main() -> Result<(), anyhow::Error> {
info!("Stored values sync job scheduler started."); info!("Stored values sync job scheduler started.");
// --- End Stored Values Sync Job Scheduler --- // --- End Stored Values Sync Job Scheduler ---
info!("Building API routes...");
let protected_router = Router::new().nest("/api/v1", routes::protected_router()); let protected_router = Router::new().nest("/api/v1", routes::protected_router());
let public_router = Router::new().route("/health", axum::routing::get(|| async { "OK" })); let public_router = Router::new().route("/health", axum::routing::get(|| async {
info!("Health check endpoint hit");
"OK"
}));
info!("Routes configured");
let (shutdown_tx, _) = broadcast::channel::<()>(1); let (shutdown_tx, _) = broadcast::channel::<()>(1);
let shutdown_tx = Arc::new(shutdown_tx); let shutdown_tx = Arc::new(shutdown_tx);
// Base router configuration // Base router configuration
info!("Configuring middleware layers...");
let app = Router::new() let app = Router::new()
.merge(protected_router) .merge(protected_router)
.merge(public_router) .merge(public_router)
@ -111,6 +130,7 @@ async fn main() -> Result<(), anyhow::Error> {
.layer(cors()) .layer(cors())
.layer(CompressionLayer::new()) .layer(CompressionLayer::new())
.layer(Extension(shutdown_tx.clone())); .layer(Extension(shutdown_tx.clone()));
info!("Middleware configured");
// Add Sentry layers if not in development using our middleware helper // Add Sentry layers if not in development using our middleware helper
let app = if !is_development { let app = if !is_development {
@ -124,23 +144,34 @@ async fn main() -> Result<(), anyhow::Error> {
.parse() .parse()
.expect("Invalid SERVER_PORT value"); .expect("Invalid SERVER_PORT value");
info!("Starting server on port {}", port_number); info!("Attempting to bind to port {}...", port_number);
let listener = match tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port_number)).await { let listener = match tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port_number)).await {
Ok(listener) => listener, Ok(listener) => {
info!("Successfully bound to port {}", port_number);
listener
},
Err(e) => { Err(e) => {
tracing::error!("Failed to bind to port {}: {}", port_number, e); tracing::error!("Failed to bind to port {}: {}", port_number, e);
eprintln!("CRITICAL: Cannot bind to port {} - {}", port_number, e);
eprintln!("Check if another process is using this port: lsof -i :{}", port_number);
return Ok(()); return Ok(());
} }
}; };
info!("Server is running on http://0.0.0.0:{}", port_number); info!("🚀 Server is running on http://0.0.0.0:{}", port_number);
info!("Health check available at: http://0.0.0.0:{}/health", port_number);
let server = axum::serve(listener, app); let server = axum::serve(listener, app);
info!("Server ready to accept connections");
tokio::select! { tokio::select! {
res = server => { res = server => {
if let Err(e) = res { if let Err(e) = res {
error!("Axum server error: {}", e); error!("Axum server error: {}", e);
eprintln!("CRITICAL: Server crashed with error: {}", e);
} else {
info!("Server shut down normally");
} }
}, },
_ = tokio::signal::ctrl_c() => { _ = tokio::signal::ctrl_c() => {
@ -149,5 +180,6 @@ async fn main() -> Result<(), anyhow::Error> {
} }
} }
info!("Server shutdown complete");
Ok(()) Ok(())
} }