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())