2025-02-26 13:10:48 -08:00
|
|
|
use axum::{
|
|
|
|
extract::{FromRef, FromRequestParts},
|
|
|
|
http::request::Parts,
|
|
|
|
};
|
|
|
|
use deadpool_diesel::postgres::{Connection, Pool};
|
2025-02-26 13:10:45 -08:00
|
|
|
use lettre::SmtpTransport;
|
2025-02-26 13:10:50 -08:00
|
|
|
use oauth2::basic::BasicClient;
|
|
|
|
|
2025-02-26 13:10:48 -08:00
|
|
|
use crate::{app_error::AppError, sessions::PgStore, settings::Settings};
|
2025-02-26 13:10:50 -08:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2025-02-26 13:10:45 -08:00
|
|
|
pub struct AppState {
|
2025-02-26 13:10:50 -08:00
|
|
|
pub db_pool: Pool,
|
2025-02-26 13:10:45 -08:00
|
|
|
pub mailer: Mailer,
|
2025-02-26 13:10:45 -08:00
|
|
|
pub reqwest_client: reqwest::Client,
|
2025-02-26 13:10:50 -08:00
|
|
|
pub oauth_client: BasicClient,
|
|
|
|
pub session_store: PgStore,
|
|
|
|
pub settings: Settings,
|
|
|
|
}
|
|
|
|
|
2025-02-26 13:10:45 -08:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Mailer(pub SmtpTransport);
|
|
|
|
|
|
|
|
impl FromRef<AppState> for Mailer {
|
|
|
|
fn from_ref(state: &AppState) -> Self {
|
|
|
|
state.mailer.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-26 13:10:45 -08:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct ReqwestClient(pub reqwest::Client);
|
|
|
|
|
|
|
|
impl FromRef<AppState> for ReqwestClient {
|
2025-02-26 13:10:50 -08:00
|
|
|
fn from_ref(state: &AppState) -> Self {
|
2025-02-26 13:10:45 -08:00
|
|
|
ReqwestClient(state.reqwest_client.clone())
|
2025-02-26 13:10:50 -08:00
|
|
|
}
|
|
|
|
}
|
2025-02-26 13:10:48 -08:00
|
|
|
|
|
|
|
pub struct DbConn(pub Connection);
|
|
|
|
|
|
|
|
impl FromRequestParts<AppState> for DbConn {
|
|
|
|
type Rejection = AppError;
|
|
|
|
|
|
|
|
async fn from_request_parts(_: &mut Parts, state: &AppState) -> Result<Self, Self::Rejection> {
|
|
|
|
let conn = state.db_pool.get().await?;
|
|
|
|
Ok(Self(conn))
|
|
|
|
}
|
|
|
|
}
|