From 8970f9859cd7e410eb2f7f13eac30bdcb916db7c Mon Sep 17 00:00:00 2001 From: YahKazo Date: Thu, 19 Feb 2026 15:30:50 +0100 Subject: [PATCH 1/2] feat: fixed all the issues in the code --- src/main.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 997409f..0b39fba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,14 +3,16 @@ mod db; mod handlers; use axum::{Router, extract::State, routing::get}; -use sqlx::migrate::Migrator; // for Migrator -use std::net::SocketAddr; // for SocketAddr -use std::path::Path; // for Path -use tokio::net::TcpListener; // for TcpListener +use sqlx::migrate::Migrator; +use std::net::SocketAddr; +use std::path::Path; +use std::time::Duration; +use tokio::net::TcpListener; +use tokio::signal; use tracing_subscriber::prelude::*; -use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; // for .with() on registry +use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; -#[derive(Clone)] // <-- Add Clone +#[derive(Clone)] pub struct AppState { db: sqlx::PgPool, } @@ -36,7 +38,7 @@ async fn main() -> anyhow::Result<()> { tracing::info!("Database migrations completed"); // Build router with state - let app_state = AppState { db: pool }; + let app_state = AppState { db: pool.clone() }; let app = Router::new() .route("/health", get(handlers::health)) .with_state(app_state); @@ -45,8 +47,45 @@ async fn main() -> anyhow::Result<()> { tracing::info!("listening on {}", addr); let listener = TcpListener::bind(addr).await?; - axum::serve(listener, app).await?; + + // Start server with graceful shutdown + axum::serve(listener, app) + .with_graceful_shutdown(shutdown_signal()) + .await?; + + // Cleanup after server shuts down + tracing::info!("Server shutdown complete, closing database connections..."); + pool.close().await; + tracing::info!("Database connections closed. Shutdown complete."); Ok(()) } +/// Waits for SIGTERM (Unix) or SIGINT (Ctrl+C) signals +async fn shutdown_signal() { + let ctrl_c = async { + signal::ctrl_c() + .await + .expect("failed to install Ctrl+C handler"); + tracing::info!("Received Ctrl+C (SIGINT), initiating graceful shutdown"); + }; + + #[cfg(unix)] + let terminate = async { + signal::unix::signal(signal::unix::SignalKind::terminate()) + .expect("failed to install SIGTERM handler") + .recv() + .await; + tracing::info!("Received SIGTERM, initiating graceful shutdown"); + }; + + #[cfg(not(unix))] + let terminate = std::future::pending::<()>(); + + tokio::select! { + _ = ctrl_c => {}, + _ = terminate => {}, + } + + tracing::info!("Shutdown signal received, waiting for pending requests to complete..."); +} \ No newline at end of file From 7ddbe0360567b48cd5048d0c813e67f5e6571708 Mon Sep 17 00:00:00 2001 From: YahKazo Date: Thu, 19 Feb 2026 15:56:34 +0100 Subject: [PATCH 2/2] feat: fix and updated the error --- src/main.rs | 56 +++++++++++------------------------------------------ 1 file changed, 11 insertions(+), 45 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0b39fba..494ae52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,16 +3,15 @@ mod db; mod handlers; use axum::{Router, extract::State, routing::get}; -use sqlx::migrate::Migrator; -use std::net::SocketAddr; -use std::path::Path; -use std::time::Duration; -use tokio::net::TcpListener; -use tokio::signal; +use sqlx::migrate::Migrator; // for Migrator +use std::net::SocketAddr; // for SocketAddr +use std::path::Path; // for Path +use tokio::net::TcpListener; // for TcpListener +use tokio::signal; // for ctrl_c use tracing_subscriber::prelude::*; -use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; +use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; // for .with() on registry -#[derive(Clone)] +#[derive(Clone)] // <-- Add Clone pub struct AppState { db: sqlx::PgPool, } @@ -38,7 +37,7 @@ async fn main() -> anyhow::Result<()> { tracing::info!("Database migrations completed"); // Build router with state - let app_state = AppState { db: pool.clone() }; + let app_state = AppState { db: pool }; let app = Router::new() .route("/health", get(handlers::health)) .with_state(app_state); @@ -46,46 +45,13 @@ async fn main() -> anyhow::Result<()> { let addr = SocketAddr::from(([0, 0, 0, 0], config.server_port)); tracing::info!("listening on {}", addr); - let listener = TcpListener::bind(addr).await?; + let shutdown_signal = signal::ctrl_c(); - // Start server with graceful shutdown + let listener = TcpListener::bind(addr).await?; axum::serve(listener, app) - .with_graceful_shutdown(shutdown_signal()) + .with_graceful_shutdown(shutdown_signal) .await?; - // Cleanup after server shuts down - tracing::info!("Server shutdown complete, closing database connections..."); - pool.close().await; - tracing::info!("Database connections closed. Shutdown complete."); - Ok(()) } -/// Waits for SIGTERM (Unix) or SIGINT (Ctrl+C) signals -async fn shutdown_signal() { - let ctrl_c = async { - signal::ctrl_c() - .await - .expect("failed to install Ctrl+C handler"); - tracing::info!("Received Ctrl+C (SIGINT), initiating graceful shutdown"); - }; - - #[cfg(unix)] - let terminate = async { - signal::unix::signal(signal::unix::SignalKind::terminate()) - .expect("failed to install SIGTERM handler") - .recv() - .await; - tracing::info!("Received SIGTERM, initiating graceful shutdown"); - }; - - #[cfg(not(unix))] - let terminate = std::future::pending::<()>(); - - tokio::select! { - _ = ctrl_c => {}, - _ = terminate => {}, - } - - tracing::info!("Shutdown signal received, waiting for pending requests to complete..."); -} \ No newline at end of file