Skip to main content

server_middleware/extract/
config.rs

1use std::sync::Arc;
2
3use axum::{extract::FromRequestParts, http::request::Parts};
4use server_common::error::config::ConfigError;
5use server_config::app::AppConfig;
6
7pub struct Config(pub Arc<AppConfig>);
8/// > 自定义提取器,提取配置文件对象
9///
10impl<S> FromRequestParts<S> for Config
11where
12    S: Send + Sync,
13{
14    type Rejection = ConfigError;
15
16    async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
17        let config = parts
18            .extensions
19            .get::<Arc<AppConfig>>()
20            .ok_or(ConfigError::NotExistError)?;
21        Ok(Config(config.clone()))
22    }
23}