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,
|
trace::TraceLayer,
|
||||||
};
|
};
|
||||||
use config::Config;
|
use config::Config;
|
||||||
|
use std::fs::OpenOptions;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
// Set up panic hook to catch panics before main
|
// Helper function to log to both file and stdout
|
||||||
fn setup_panic_hook() {
|
fn log(msg: &str) {
|
||||||
std::panic::set_hook(Box::new(|panic_info| {
|
let timestamp = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S%.3f");
|
||||||
let msg = panic_info.to_string();
|
let full_msg = format!("[{}] {}\n", timestamp, msg);
|
||||||
println!("[PANIC] {}", msg);
|
|
||||||
let location = panic_info.location().unwrap_or_else(|| std::panic::Location::caller());
|
// Try to write to file
|
||||||
println!("[PANIC] Location: {}:{}:{}", location.file(), location.line(), location.column());
|
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]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
// Install panic hook FIRST
|
log("=== NORMOGEN STARTING ===");
|
||||||
setup_panic_hook();
|
log("[1/7] Loading environment variables...");
|
||||||
|
|
||||||
// IMMEDIATE output - this MUST show up
|
|
||||||
println!("=== NORMOGEN STARTING ===");
|
|
||||||
println!("[1/7] Loading environment variables...");
|
|
||||||
|
|
||||||
match dotenv::dotenv() {
|
match dotenv::dotenv() {
|
||||||
Ok(path) => {
|
Ok(path) => {
|
||||||
println!("[1/7] Loaded .env from: {:?}", path);
|
log(&format!("[1/7] Loaded .env from: {:?}", path));
|
||||||
}
|
}
|
||||||
Err(_) => {
|
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()
|
tracing_subscriber::fmt()
|
||||||
.with_env_filter(
|
.with_env_filter(
|
||||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||||
|
|
@ -53,42 +61,42 @@ async fn main() -> anyhow::Result<()> {
|
||||||
)
|
)
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
println!("[3/7] Loading configuration...");
|
log("[3/7] Loading configuration...");
|
||||||
let config = match Config::from_env() {
|
let config = match Config::from_env() {
|
||||||
Ok(cfg) => {
|
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
|
cfg
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("[FATAL] Failed to load configuration: {}", e);
|
log(&format!("[FATAL] Failed to load configuration: {}", e));
|
||||||
return Err(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 {
|
let db = match db::MongoDb::new(&config.database.uri, &config.database.database).await {
|
||||||
Ok(db) => {
|
Ok(db) => {
|
||||||
println!("[4/7] MongoDB connection successful!");
|
log("[4/7] MongoDB connection successful!");
|
||||||
db
|
db
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("[FATAL] Failed to connect to MongoDB: {}", e);
|
log(&format!("[FATAL] Failed to connect to MongoDB: {}", e));
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("[5/7] Performing health check...");
|
log("[5/7] Performing health check...");
|
||||||
match db.health_check().await {
|
match db.health_check().await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
println!("[5/7] MongoDB health check: OK");
|
log("[5/7] MongoDB health check: OK");
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("[FATAL] MongoDB health check failed: {}", e);
|
log(&format!("[FATAL] MongoDB health check failed: {}", e));
|
||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("[6/7] Building application...");
|
log("[6/7] Building application...");
|
||||||
let jwt_service = auth::JwtService::new(config.jwt.clone());
|
let jwt_service = auth::JwtService::new(config.jwt.clone());
|
||||||
|
|
||||||
let app_state = config::AppState {
|
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);
|
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))
|
let listener = tokio::net::TcpListener::bind(&format!("{}:{}", config.server.host, config.server.port))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
println!("=== SERVER READY ===");
|
log("=== SERVER READY ===");
|
||||||
println!("Listening on http://{}:{}", config.server.host, config.server.port);
|
log(&format!("Listening on http://{}:{}", config.server.host, config.server.port));
|
||||||
|
|
||||||
tracing::info!("Server listening on {}:{}", 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