2024-09-22 21:38:35 -05:00
|
|
|
use axum::{
|
|
|
|
routing::{get, post},
|
|
|
|
Router,
|
|
|
|
};
|
|
|
|
use sqlx::postgres::PgPoolOptions;
|
|
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
|
|
|
|
|
|
|
use std::time::Duration;
|
|
|
|
use tokio::net::TcpListener;
|
|
|
|
|
|
|
|
mod datasources;
|
|
|
|
mod routes;
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let _ = dotenvy::dotenv();
|
|
|
|
tracing_subscriber::registry()
|
|
|
|
.with(
|
|
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
|
|
|
.unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
|
|
|
|
)
|
|
|
|
.with(tracing_subscriber::fmt::layer())
|
|
|
|
.init();
|
|
|
|
|
|
|
|
let db_connection_str = std::env::var("DATABASE_URL")
|
|
|
|
.unwrap_or_else(|_| "postgres://postgres:password@localhost".to_string());
|
|
|
|
|
|
|
|
// set up connection pool
|
|
|
|
let pool = PgPoolOptions::new()
|
|
|
|
.max_connections(10)
|
|
|
|
.acquire_timeout(Duration::from_secs(3))
|
|
|
|
.connect(&db_connection_str)
|
|
|
|
.await
|
|
|
|
.expect("can't connect to database");
|
|
|
|
|
|
|
|
// build our application with some routes
|
|
|
|
let app = Router::new()
|
|
|
|
.route("/", get(routes::root::RootRoute::root))
|
|
|
|
.fallback(routes::root::RootRoute::not_found)
|
|
|
|
.with_state(pool);
|
|
|
|
|
|
|
|
// run it with hyper
|
|
|
|
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
|
|
|
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
|
|
|
axum::serve(listener, app).await.unwrap();
|
2024-09-03 00:17:19 -05:00
|
|
|
}
|