Skip to main content

server_middleware/extract/
db.rs

1#![allow(unused_variables)] //允许未使用的变量
2#![allow(dead_code)] //允许未使用的代码
3#![allow(unused_must_use)]
4
5use std::sync::Arc;
6
7use axum::extract::FromRequestParts;
8use axum::http::request::Parts;
9use server_common::error::db::DbError;
10use sqlx::PgPool;
11
12pub struct DbPool(pub Arc<PgPool>);
13/// > 自定义提取器,提取数据库连接池对象
14///
15impl<S> FromRequestParts<S> for DbPool
16where
17    S: Send + Sync,
18{
19    type Rejection = DbError;
20
21    async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
22        let pool = parts
23            .extensions
24            .get::<Arc<PgPool>>()
25            .ok_or(DbError::PoolIsNotExistError)?;
26        Ok(DbPool(pool.clone()))
27    }
28}