use std::fmt::Debug; use axum::{ async_trait, extract::{FromRef, FromRequestParts}, http::request::Parts, }; use sqlx::{Pool, Postgres}; use crate::Error; #[derive(Debug, Clone)] pub struct AppState { pub pool: Pool, } impl AppState { #[tracing::instrument] pub async fn new(uri: String) -> Result { tracing::debug!("Attempting to connect to database..."); let pool = sqlx::postgres::PgPoolOptions::new() .max_connections(10) .connect(&uri) .await?; tracing::info!("Connected to database"); sqlx::migrate!().run(&pool).await?; Ok(Self { pool }) } } #[async_trait] impl FromRequestParts for AppState where Self: FromRef, S: Send + Sync + Debug, { type Rejection = Error; async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result { Ok(Self::from_ref(state)) } }