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

View file

@ -11,7 +11,7 @@ use axum::{
use rusqlite::Connection;
use tower_http::trace::TraceLayer;
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 _};
#[derive(Template)]
@ -40,7 +40,7 @@ struct Food {
}
#[tokio::main]
async fn main() {
async fn main() -> Result<(), std::io::Error> {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
@ -57,8 +57,12 @@ async fn main() {
.init();
let db_connecion_str = "./foods.db".to_string();
let conn = Connection::open(db_connecion_str).unwrap();
conn.execute(include_str!("create_tables.sql"), ()).unwrap();
debug!(db_connecion_str, "opening database");
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 app = Router::new()
@ -88,9 +92,17 @@ async fn main() {
.layer(RequestIdLayer)
.with_state(conn);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3001").await.unwrap();
info!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
let address = "0.0.0.0:3001";
let listener = tokio::net::TcpListener::bind(address)
.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> {