From 937c6e8451d4433b8c3ecf3e3db57a6714f1cca7 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 18 Oct 2023 17:22:25 +0200 Subject: [PATCH] add example .env, don't unwrap postgres error but log it --- .env.example | 2 ++ .gitignore | 1 + src/main.rs | 14 ++++++++++---- 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..960939c --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +DATABASE=postgresql://postgres:postgres@localhost/postgres +PORT=3000 diff --git a/.gitignore b/.gitignore index 9026c77..9442b11 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target .vscode +.env diff --git a/src/main.rs b/src/main.rs index f3d90c3..74471d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use axum::{routing::get, Router}; use clap::Parser; use sqlx::postgres::PgPoolOptions; use tower_http::trace::TraceLayer; -use tracing::debug; +use tracing::{debug, error, info}; use crate::config::Config; use crate::state::AppState; @@ -26,19 +26,25 @@ async fn main() { let config = Config::parse(); - let pool = PgPoolOptions::new() + let pool = match PgPoolOptions::new() .max_connections(500) .connect(&config.database) - .await - .unwrap(); + .await { + Ok(pool) => pool, + Err(err) => return error!("Initializing database: {}", err) + }; let state = Arc::new(AppState { pool }); + debug!("Building router"); + let app = Router::new() .route("/", get(|| async { "Hello, World!" })) .layer(TraceLayer::new_for_http()) .with_state(state); + info!("Listening on port {}", config.port); + let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), config.port); axum::Server::bind(&socket) .serve(app.into_make_service())