forked from
oppi.li/at-advent
this repo has no description
1use crate::{AppState, OAuthClientType};
2use axum::extract::{FromRef, FromRequestParts};
3use axum::http::StatusCode;
4use axum::http::request::Parts;
5use shared::cache::ConnectionPool;
6use shared::web_helpers::internal_error;
7use sqlx::PgPool;
8
9#[allow(dead_code)]
10struct DatabaseConnection(sqlx::pool::PoolConnection<sqlx::Postgres>);
11
12impl<S> FromRequestParts<S> for DatabaseConnection
13where
14 PgPool: FromRef<S>,
15 S: Send + Sync,
16{
17 type Rejection = (StatusCode, String);
18
19 async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
20 let pool = PgPool::from_ref(state);
21
22 let conn = pool.acquire().await.map_err(internal_error)?;
23
24 Ok(Self(conn))
25 }
26}
27
28//Redis extractor
29
30// pub type AtProtoClient = OAuthClientType;
31
32#[allow(dead_code)]
33pub struct AtProtoClient(OAuthClientType);
34
35impl<S> FromRequestParts<S> for AtProtoClient
36where
37 OAuthClientType: FromRef<S>,
38 S: Send + Sync,
39{
40 type Rejection = (StatusCode, String);
41
42 async fn from_request_parts(_parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
43 let client = OAuthClientType::from_ref(state);
44 Ok(Self(client))
45 }
46}
47
48//From refs
49
50impl FromRef<AppState> for PgPool {
51 fn from_ref(app_state: &AppState) -> PgPool {
52 app_state.postgres_pool.clone()
53 }
54}
55
56impl FromRef<AppState> for ConnectionPool {
57 fn from_ref(app_state: &AppState) -> ConnectionPool {
58 app_state.redis_pool.clone()
59 }
60}
61
62impl FromRef<AppState> for OAuthClientType {
63 fn from_ref(app_state: &AppState) -> OAuthClientType {
64 app_state.oauth_client.clone()
65 }
66}