remove some unwraps

This commit is contained in:
Khaïs COLIN 2025-10-19 17:32:25 +02:00
parent a9ecc56990
commit 957e15d0d8
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 19 additions and 7 deletions

BIN
foods.db-journal Normal file

Binary file not shown.

View file

@ -11,7 +11,7 @@ use axum::{
use rusqlite::Connection; use rusqlite::Connection;
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use tower_request_id::{RequestId, RequestIdLayer}; use tower_request_id::{RequestId, RequestIdLayer};
use tracing::{debug, info, info_span}; use tracing::{debug, error, info, info_span};
use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _}; use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _};
#[derive(Template)] #[derive(Template)]
@ -40,7 +40,7 @@ struct Food {
} }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() -> Result<(), std::io::Error> {
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| { tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
@ -57,8 +57,12 @@ async fn main() {
.init(); .init();
let db_connecion_str = "./foods.db".to_string(); let db_connecion_str = "./foods.db".to_string();
let conn = Connection::open(db_connecion_str).unwrap(); debug!(db_connecion_str, "opening database");
conn.execute(include_str!("create_tables.sql"), ()).unwrap(); let conn = Connection::open(db_connecion_str).expect("failed to open database");
if let Err(e) = conn.execute(include_str!("create_tables.sql"), ()) {
error!(?e, "failed to create tables");
panic!("failed to create tables: {:#?}", e);
}
let conn = Arc::new(Mutex::new(conn)); let conn = Arc::new(Mutex::new(conn));
let app = Router::new() let app = Router::new()
@ -88,9 +92,17 @@ async fn main() {
.layer(RequestIdLayer) .layer(RequestIdLayer)
.with_state(conn); .with_state(conn);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3001").await.unwrap(); let address = "0.0.0.0:3001";
info!("listening on {}", listener.local_addr().unwrap()); let listener = tokio::net::TcpListener::bind(address)
axum::serve(listener, app).await.unwrap(); .await
.expect("failed to bind to address");
info!(
"listening on {}",
listener
.local_addr()
.expect("failed to get local listening address")
);
axum::serve(listener, app).await
} }
fn get_foods(conn: &Arc<Mutex<Connection>>) -> Vec<Food> { fn get_foods(conn: &Arc<Mutex<Connection>>) -> Vec<Food> {