Expand description
框架集成:把 Guard 按各框架的惯例交给处理器。
每个框架一个 feature,全部 opt-in,默认构建一个都不拉。
features = ["axum"] -> Guard 作为 axum 提取器
features = ["actix-web"] -> Guard 实现 FromRequest
features = ["rocket"] -> Guard 作为 rocket 请求守卫
features = ["poem"] -> Guard 实现 FromRequest
features = ["salvo"] -> Guard 实现 Extractible
features = ["warp"] -> with_guard 组合子
features = ["bee-rust"] -> bee-rust 路由吃 axum handler,复用 axum 适配
features = ["ecat"] -> e-cat 的 tower Layer/Service§一致的心智模型
框架各不相同,但它们在这里要解决的是同一个问题:处理器怎么拿到那个共享的
加密器。所有适配层都归结为同一件事 —— 从应用状态里取出 Guard,克隆一份
(Arc 递增引用计数,不含密钥拷贝)交给处理器。
§为什么需要一个 Guarded trait
真实应用的状态结构体不会只有一把加密器,通常还挂着连接池、配置、限流器。 如果直接实现「从状态里取守卫」,那就得为每个状态类型写一遍提取代码。所以这里 定义一个只有一件事的 trait:你的状态告诉本库「守卫在哪个字段」,剩下的提取 逻辑由适配层提供。
use std::sync::Arc;
use encryption::guard::Guard;
use encryption::integrations::Guarded;
#[derive(Clone)]
struct AppState {
pool: Arc<()>, // 你的连接池、配置……
encryption: Guard, // 加密守卫
}
impl Guarded for AppState {
fn guard(&self) -> &Guard { &self.encryption }
}
// 处理器直接声明它要一把守卫,不必再 `State(state)` 再手动取字段
async fn create_user(guard: Guard) -> String {
format!("{:?}", guard.encrypt("13800138000".as_bytes()))
}Modules§
- actix
- actix-web 4 集成。
- axum
- axum 0.8 集成。
- bee_
rust - bee-rust(
bee_router)的路由直接接收 axum handler,状态也走 axum 的State,所以适配层就是 axum 那一层,这里只把文档与再导出处放好。 - ecat
- e-cat 集成。
- poem
- poem 3 集成。
- rocket
- rocket 0.5 集成。
- salvo
- salvo 1.0 集成。
- warp
- warp 0.4 集成。
Structs§
- Guard
NotConfigured - 应用没有把守卫注册进框架的状态/数据槽。