fix: Use println! instead of stderr for reliable Docker logging
This commit is contained in:
parent
fce388bdf7
commit
69d8fd611e
1 changed files with 19 additions and 39 deletions
|
|
@ -16,31 +16,23 @@ use tower_http::{
|
|||
trace::TraceLayer,
|
||||
};
|
||||
use config::Config;
|
||||
use std::io::Write;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
// Force flush immediately to ensure we see output
|
||||
let mut stderr = std::io::stderr();
|
||||
writeln!(stderr, "=== NORMOGEN BACKEND STARTING ===")?;
|
||||
stderr.flush()?;
|
||||
|
||||
writeln!(stderr, "[STARTUP] Loading environment variables...")?;
|
||||
stderr.flush()?;
|
||||
// Use println! which is more reliable in Docker
|
||||
println!("=== NORMOGEN BACKEND STARTING ===");
|
||||
println!("[STARTUP] Loading environment variables...");
|
||||
|
||||
match dotenv::dotenv() {
|
||||
Ok(path) => {
|
||||
writeln!(stderr, "[STARTUP] Loaded .env from: {:?}", path)?;
|
||||
stderr.flush()?;
|
||||
println!("[STARTUP] Loaded .env from: {:?}", path);
|
||||
}
|
||||
Err(e) => {
|
||||
writeln!(stderr, "[STARTUP] No .env file found (this is OK in Docker): {}", e)?;
|
||||
stderr.flush()?;
|
||||
println!("[STARTUP] No .env file found (this is OK in Docker): {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
writeln!(stderr, "[STARTUP] Initializing logging...")?;
|
||||
stderr.flush()?;
|
||||
println!("[STARTUP] Initializing logging...");
|
||||
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
|
|
@ -49,49 +41,40 @@ async fn main() -> anyhow::Result<()> {
|
|||
)
|
||||
.init();
|
||||
|
||||
writeln!(stderr, "[STARTUP] Loading configuration...")?;
|
||||
stderr.flush()?;
|
||||
println!("[STARTUP] Loading configuration...");
|
||||
|
||||
let config = match Config::from_env() {
|
||||
Ok(cfg) => {
|
||||
writeln!(stderr, "[STARTUP] Config loaded: DB={}, Port={}", cfg.database.database, cfg.server.port)?;
|
||||
stderr.flush()?;
|
||||
println!("[STARTUP] Config loaded: DB={}, Port={}", cfg.database.database, cfg.server.port);
|
||||
cfg
|
||||
}
|
||||
Err(e) => {
|
||||
writeln!(stderr, "[FATAL] Failed to load configuration: {}", e)?;
|
||||
stderr.flush()?;
|
||||
println!("[FATAL] Failed to load configuration: {}", e);
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
|
||||
writeln!(stderr, "[STARTUP] Connecting to MongoDB at {}...", config.database.uri)?;
|
||||
stderr.flush()?;
|
||||
println!("[STARTUP] Connecting to MongoDB at {}...", config.database.uri);
|
||||
|
||||
let db = match db::MongoDb::new(&config.database.uri, &config.database.database).await {
|
||||
Ok(db) => {
|
||||
writeln!(stderr, "[STARTUP] MongoDB connection successful!")?;
|
||||
stderr.flush()?;
|
||||
println!("[STARTUP] MongoDB connection successful!");
|
||||
db
|
||||
}
|
||||
Err(e) => {
|
||||
writeln!(stderr, "[FATAL] Failed to connect to MongoDB: {}", e)?;
|
||||
stderr.flush()?;
|
||||
println!("[FATAL] Failed to connect to MongoDB: {}", e);
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
|
||||
writeln!(stderr, "[STARTUP] Performing health check...")?;
|
||||
stderr.flush()?;
|
||||
println!("[STARTUP] Performing health check...");
|
||||
|
||||
match db.health_check().await {
|
||||
Ok(health) => {
|
||||
writeln!(stderr, "[STARTUP] MongoDB health check: {}", health)?;
|
||||
stderr.flush()?;
|
||||
println!("[STARTUP] MongoDB health check: {}", health);
|
||||
}
|
||||
Err(e) => {
|
||||
writeln!(stderr, "[FATAL] MongoDB health check failed: {}", e)?;
|
||||
stderr.flush()?;
|
||||
println!("[FATAL] MongoDB health check failed: {}", e);
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
|
|
@ -104,8 +87,7 @@ async fn main() -> anyhow::Result<()> {
|
|||
config: config.clone(),
|
||||
};
|
||||
|
||||
writeln!(stderr, "[STARTUP] Building router...")?;
|
||||
stderr.flush()?;
|
||||
println!("[STARTUP] Building router...");
|
||||
|
||||
let public_routes = Router::new()
|
||||
.route("/health", get(handlers::health_check))
|
||||
|
|
@ -148,15 +130,13 @@ async fn main() -> anyhow::Result<()> {
|
|||
|
||||
let app = public_routes.merge(protected_routes).with_state(app_state);
|
||||
|
||||
writeln!(stderr, "[STARTUP] Binding to {}:{}...", config.server.host, config.server.port)?;
|
||||
stderr.flush()?;
|
||||
println!("[STARTUP] Binding to {}:{}...", config.server.host, config.server.port);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(&format!("{}:{}", config.server.host, config.server.port))
|
||||
.await?;
|
||||
|
||||
writeln!(stderr, "[STARTUP] Server listening on http://{}:{}", config.server.host, config.server.port)?;
|
||||
writeln!(stderr, "[STARTUP] Server ready to accept connections!")?;
|
||||
stderr.flush()?;
|
||||
println!("[STARTUP] Server listening on http://{}:{}", config.server.host, config.server.port);
|
||||
println!("[STARTUP] Server ready to accept connections!");
|
||||
|
||||
tracing::info!("Server listening on {}:{}", config.server.host, config.server.port);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue