debug: Add file logging to diagnose startup issue
This commit is contained in:
parent
c82160ca11
commit
ff68ce2646
1 changed files with 38 additions and 30 deletions
|
|
@ -16,36 +16,44 @@ use tower_http::{
|
|||
trace::TraceLayer,
|
||||
};
|
||||
use config::Config;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
|
||||
// Set up panic hook to catch panics before main
|
||||
fn setup_panic_hook() {
|
||||
std::panic::set_hook(Box::new(|panic_info| {
|
||||
let msg = panic_info.to_string();
|
||||
println!("[PANIC] {}", msg);
|
||||
let location = panic_info.location().unwrap_or_else(|| std::panic::Location::caller());
|
||||
println!("[PANIC] Location: {}:{}:{}", location.file(), location.line(), location.column());
|
||||
}));
|
||||
// Helper function to log to both file and stdout
|
||||
fn log(msg: &str) {
|
||||
let timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S%.3f");
|
||||
let full_msg = format!("[{}] {}\n", timestamp, msg);
|
||||
|
||||
// Try to write to file
|
||||
if let Ok(mut file) = OpenOptions::new()
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open("/tmp/normogen-startup.log")
|
||||
{
|
||||
let _ = file.write_all(full_msg.as_bytes());
|
||||
let _ = file.flush();
|
||||
}
|
||||
|
||||
// Also print to stdout
|
||||
print!("{}", full_msg);
|
||||
let _ = std::io::stdout().flush();
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
// Install panic hook FIRST
|
||||
setup_panic_hook();
|
||||
|
||||
// IMMEDIATE output - this MUST show up
|
||||
println!("=== NORMOGEN STARTING ===");
|
||||
println!("[1/7] Loading environment variables...");
|
||||
log("=== NORMOGEN STARTING ===");
|
||||
log("[1/7] Loading environment variables...");
|
||||
|
||||
match dotenv::dotenv() {
|
||||
Ok(path) => {
|
||||
println!("[1/7] Loaded .env from: {:?}", path);
|
||||
log(&format!("[1/7] Loaded .env from: {:?}", path));
|
||||
}
|
||||
Err(_) => {
|
||||
println!("[1/7] No .env file found (OK in Docker)");
|
||||
log("[1/7] No .env file found (OK in Docker)");
|
||||
}
|
||||
}
|
||||
|
||||
println!("[2/7] Initializing logging...");
|
||||
log("[2/7] Initializing logging...");
|
||||
tracing_subscriber::fmt()
|
||||
.with_env_filter(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
|
|
@ -53,42 +61,42 @@ async fn main() -> anyhow::Result<()> {
|
|||
)
|
||||
.init();
|
||||
|
||||
println!("[3/7] Loading configuration...");
|
||||
log("[3/7] Loading configuration...");
|
||||
let config = match Config::from_env() {
|
||||
Ok(cfg) => {
|
||||
println!("[3/7] Config loaded: DB={}, Port={}", cfg.database.database, cfg.server.port);
|
||||
log(&format!("[3/7] Config loaded: DB={}, Port={}", cfg.database.database, cfg.server.port));
|
||||
cfg
|
||||
}
|
||||
Err(e) => {
|
||||
println!("[FATAL] Failed to load configuration: {}", e);
|
||||
log(&format!("[FATAL] Failed to load configuration: {}", e));
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
|
||||
println!("[4/7] Connecting to MongoDB at {}...", config.database.uri);
|
||||
log(&format!("[4/7] Connecting to MongoDB at {}...", config.database.uri));
|
||||
let db = match db::MongoDb::new(&config.database.uri, &config.database.database).await {
|
||||
Ok(db) => {
|
||||
println!("[4/7] MongoDB connection successful!");
|
||||
log("[4/7] MongoDB connection successful!");
|
||||
db
|
||||
}
|
||||
Err(e) => {
|
||||
println!("[FATAL] Failed to connect to MongoDB: {}", e);
|
||||
log(&format!("[FATAL] Failed to connect to MongoDB: {}", e));
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
|
||||
println!("[5/7] Performing health check...");
|
||||
log("[5/7] Performing health check...");
|
||||
match db.health_check().await {
|
||||
Ok(_) => {
|
||||
println!("[5/7] MongoDB health check: OK");
|
||||
log("[5/7] MongoDB health check: OK");
|
||||
}
|
||||
Err(e) => {
|
||||
println!("[FATAL] MongoDB health check failed: {}", e);
|
||||
log(&format!("[FATAL] MongoDB health check failed: {}", e));
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
|
||||
println!("[6/7] Building application...");
|
||||
log("[6/7] Building application...");
|
||||
let jwt_service = auth::JwtService::new(config.jwt.clone());
|
||||
|
||||
let app_state = config::AppState {
|
||||
|
|
@ -134,12 +142,12 @@ async fn main() -> anyhow::Result<()> {
|
|||
|
||||
let app = public_routes.merge(protected_routes).with_state(app_state);
|
||||
|
||||
println!("[7/7] Starting server on {}:{}...", config.server.host, config.server.port);
|
||||
log(&format!("[7/7] Starting server on {}:{}...", config.server.host, config.server.port));
|
||||
let listener = tokio::net::TcpListener::bind(&format!("{}:{}", config.server.host, config.server.port))
|
||||
.await?;
|
||||
|
||||
println!("=== SERVER READY ===");
|
||||
println!("Listening on http://{}:{}", config.server.host, config.server.port);
|
||||
log("=== SERVER READY ===");
|
||||
log(&format!("Listening on http://{}:{}", config.server.host, config.server.port));
|
||||
|
||||
tracing::info!("Server listening on {}:{}", config.server.host, config.server.port);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue