implemented foundational API routes (/articles
, /summaries
) using Axum, added graceful shutdown handling, improved database initialization with connection pooling and directory creation, and integrated tracing
for logging
This commit is contained in:
@@ -5,35 +5,74 @@ mod models;
|
||||
mod services;
|
||||
|
||||
use crate::config::Config;
|
||||
use anyhow::Result;
|
||||
use axum::Router;
|
||||
use axum::routing::get;
|
||||
use sqlx::sqlite::SqliteConnectOptions;
|
||||
use std::str::FromStr;
|
||||
use tokio::signal;
|
||||
use tokio::signal::ctrl_c;
|
||||
use tokio::signal::unix::signal;
|
||||
use tracing::{error, info};
|
||||
use tracing_subscriber;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
async fn main() -> Result<()> {
|
||||
tracing_subscriber::fmt()
|
||||
.with_target(false)
|
||||
.compact()
|
||||
.init();
|
||||
|
||||
let config = Config::from_env();
|
||||
|
||||
match db::initialize_db(&config.db_path, &config.migration_path).await {
|
||||
Ok(_conn) => {
|
||||
println!("Database initialized successfully");
|
||||
let pool = db::initialize_db(&config).await?;
|
||||
|
||||
let options = SqliteConnectOptions::from_str(&config.database_url())
|
||||
.expect("Invalid database URL")
|
||||
.create_if_missing(true);
|
||||
let app = create_app(pool);
|
||||
|
||||
match db::create_pool(options).await {
|
||||
Ok(_pool) => {
|
||||
println!("Database pool created successfully")
|
||||
// App logic here
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error creating database pool: {:?}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error initializing database: {:?}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
let listener =
|
||||
tokio::net::TcpListener::bind(format!("{}:{}", config.host, config.port)).await?;
|
||||
info!("Server starting on {}:{}", config.host, config.port);
|
||||
|
||||
axum::serve(listener, app)
|
||||
.with_graceful_shutdown(shutdown_signal())
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_app(pool: sqlx::SqlitePool) -> Router {
|
||||
Router::new()
|
||||
.route("/health", get(health_check))
|
||||
.nest("/api", api::routes::routes())
|
||||
.with_state(pool)
|
||||
}
|
||||
|
||||
async fn health_check() -> &'static str {
|
||||
"OK"
|
||||
}
|
||||
|
||||
async fn shutdown_signal() {
|
||||
let ctrl_c = async {
|
||||
signal::ctrl_c()
|
||||
.await
|
||||
.expect("failed to install CTRL+C handler");
|
||||
};
|
||||
|
||||
#[cfg(unix)]
|
||||
let terminate = async {
|
||||
signal::unix::signal(signal::unix::SignalKind::terminate())
|
||||
.expect("failed to install terminate handler")
|
||||
.recv()
|
||||
.await;
|
||||
};
|
||||
|
||||
#[cfg(not(unix))]
|
||||
let terminate = std::future::pending::<()>();
|
||||
|
||||
tokio::select! {
|
||||
_ = ctrl_c => {},
|
||||
_ = terminate => {},
|
||||
}
|
||||
|
||||
info!("Signal received, shutting down");
|
||||
}
|
||||
|
Reference in New Issue
Block a user