add example .env, don't unwrap postgres error but log it
This commit is contained in:
parent
b050eaac9b
commit
937c6e8451
3 changed files with 13 additions and 4 deletions
2
.env.example
Normal file
2
.env.example
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
DATABASE=postgresql://postgres:postgres@localhost/postgres
|
||||||
|
PORT=3000
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
/target
|
/target
|
||||||
.vscode
|
.vscode
|
||||||
|
.env
|
||||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -9,7 +9,7 @@ use axum::{routing::get, Router};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use sqlx::postgres::PgPoolOptions;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
use tracing::debug;
|
use tracing::{debug, error, info};
|
||||||
|
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::state::AppState;
|
use crate::state::AppState;
|
||||||
|
@ -26,19 +26,25 @@ async fn main() {
|
||||||
|
|
||||||
let config = Config::parse();
|
let config = Config::parse();
|
||||||
|
|
||||||
let pool = PgPoolOptions::new()
|
let pool = match PgPoolOptions::new()
|
||||||
.max_connections(500)
|
.max_connections(500)
|
||||||
.connect(&config.database)
|
.connect(&config.database)
|
||||||
.await
|
.await {
|
||||||
.unwrap();
|
Ok(pool) => pool,
|
||||||
|
Err(err) => return error!("Initializing database: {}", err)
|
||||||
|
};
|
||||||
|
|
||||||
let state = Arc::new(AppState { pool });
|
let state = Arc::new(AppState { pool });
|
||||||
|
|
||||||
|
debug!("Building router");
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", get(|| async { "Hello, World!" }))
|
.route("/", get(|| async { "Hello, World!" }))
|
||||||
.layer(TraceLayer::new_for_http())
|
.layer(TraceLayer::new_for_http())
|
||||||
.with_state(state);
|
.with_state(state);
|
||||||
|
|
||||||
|
info!("Listening on port {}", config.port);
|
||||||
|
|
||||||
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), config.port);
|
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), config.port);
|
||||||
axum::Server::bind(&socket)
|
axum::Server::bind(&socket)
|
||||||
.serve(app.into_make_service())
|
.serve(app.into_make_service())
|
||||||
|
|
Loading…
Reference in a new issue