相关疑难解决方法(0)

如何将结构从 Actix 中间件传递到处理程序?

我正在尝试为我的 Actix 应用程序编写一个身份验证中间件。在中间件中验证请求时,我调用数据库来检索必要的用户数据以验证传入的请求。一旦请求获得授权,我希望能够将此用户数据传递给处理程序,因为这将使我避免两次查询相同的数据。

我无法找到解决方案。到目前为止我能找到的最好的建议是“设置请求扩展”。似乎没有任何这方面的示例,而且围绕此的文档也太少,无法弄清楚这里要做什么。

rust actix-web

7
推荐指数
1
解决办法
2793
查看次数

有没有办法将变量绑定到 tower-http / hyper / axum 中的请求?

我正在尝试向每个跟踪事件添加请求 ID。tower_http::trace我可以这样做:

#[derive(Clone)]
pub struct RequestSpan;

impl<B> tower_http::trace::MakeSpan<B> for RequestSpan {
    fn make_span(&mut self, request: &http::Request<B>) -> tracing::Span {
        tracing::error_span!(
            "rq",
            id = %ulid::Ulid::new().to_string(),
            method = %request.method(),
            uri = %request.uri(),
            version = ?request.version(),
        )
    }
}

...

let middleware_stack = tower::ServiceBuilder::new()
    .layer(TraceLayer::new_for_http().make_span_with(RequestSpan))
Run Code Online (Sandbox Code Playgroud)

它在服务器范围内工作,但我还需要将请求 ID 传递到外部任务队列中。有什么建议么?

rust hyper rust-tower rust-axum

2
推荐指数
1
解决办法
1419
查看次数

标签 统计

rust ×2

actix-web ×1

hyper ×1

rust-axum ×1

rust-tower ×1