use crate::{AppState, OAuthClientType}; use axum::extract::{FromRef, FromRequestParts}; use axum::http::StatusCode; use axum::http::request::Parts; use shared::cache::ConnectionPool; use shared::web_helpers::internal_error; use sqlx::PgPool; #[allow(dead_code)] struct DatabaseConnection(sqlx::pool::PoolConnection); impl FromRequestParts for DatabaseConnection where PgPool: FromRef, S: Send + Sync, { type Rejection = (StatusCode, String); async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result { let pool = PgPool::from_ref(state); let conn = pool.acquire().await.map_err(internal_error)?; Ok(Self(conn)) } } //Redis extractor // pub type AtProtoClient = OAuthClientType; #[allow(dead_code)] pub struct AtProtoClient(OAuthClientType); impl FromRequestParts for AtProtoClient where OAuthClientType: FromRef, S: Send + Sync, { type Rejection = (StatusCode, String); async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result { let client = OAuthClientType::from_ref(state); Ok(Self(client)) } } //From refs impl FromRef for PgPool { fn from_ref(app_state: &AppState) -> PgPool { app_state.postgres_pool.clone() } } impl FromRef for ConnectionPool { fn from_ref(app_state: &AppState) -> ConnectionPool { app_state.redis_pool.clone() } } impl FromRef for OAuthClientType { fn from_ref(app_state: &AppState) -> OAuthClientType { app_state.oauth_client.clone() } }