#guard #layer #extracted #data #logic #router #values

axum_guard_logic

在路由层比较提取数据和预期数据与逻辑

9个版本

0.3.3 2022年12月28日
0.3.2 2022年12月28日
0.3.1 2022年9月29日
0.2.3 2022年8月29日
0.1.0 2022年8月27日

798HTTP服务器

每月 32 次下载

MIT 许可证

30KB
667 代码行

axum_guard_logic

https://crates.io/crates/axum_guard_logic

T 实现 Guard

pub trait Guard {
    fn check_guard(&self, expected:&Self) -> bool;
}

其中self的值根据FromRequestParts<State> for T 从请求中提取,而expected的值在路由层的层内部给出。

Router::with(state.clone())
            .route("/", get(ok))
            .layer(
                    GuardService::new(
                        state.clone(),
                        expected.clone()).into_layer()
            );

传递给GuardService::new()的参数state将是FromRequestParts实现中在T上调用的状态。

您还可以使用AND/OR逻辑嵌套。

Router::new()
    .route("/", get(ok))
    .layer(
        GuardService::new(state.clone(), expected)
            .and(
                GuardService::new(
                    other_state.clone(), 
                    other_expected
                )
            )
        .into_layer()
);

check_guard返回false时,将拒绝StatusCode::UNAUTHORIZED

依赖关系

~6–8MB
~144K SLoC