如何在最新版本(3.3.2)中实现Actix-web中间件?
How to implement Actix-web middleware in the latest version (3.3.2)?
我一直在尝试根据 Actix-web 文档中列出的示例编译 SayHi
中间件:https://actix.rs/docs/middleware/。问题是在最新版本的 Actix-web (3.3.2) 和 Rust (1.55) 中,此代码无法编译,模板缺少参数(大概是因为请求类型已移至它们?),即使我移动请求类型到正确位置其他东西中断。
如何在最新版本的 actix-web 中实现像 SayHi
示例这样的简单中间件?
我对 Rust 比较陌生,所以如果我遗漏了一些明显的东西,请告诉我,但我已经做了两天了,我似乎无法找到或制作任何成功创建简单代码的代码最新版本的 actix-web 中间件。
这是截至 2021 年 10 月 16 日的 actix 文档中的示例代码:
use std::pin::Pin;
use std::task::{Context, Poll};
use actix_service::{Service, Transform};
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
use futures::future::{ok, Ready};
use futures::Future;
pub struct SayHi;
impl<S, B> Transform<S> for SayHi
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = SayHiMiddleware<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ok(SayHiMiddleware { service })
}
}
pub struct SayHiMiddleware<S> {
service: S,
}
impl<S, B> Service for SayHiMiddleware<S>
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
fn call(&mut self, req: ServiceRequest) -> Self::Future {
println!("Hi from start. You requested: {}", req.path());
let fut = self.service.call(req);
Box::pin(async move {
let res = fut.await?;
println!("Hi from response");
Ok(res)
})
}
}
事实证明这个解决方案确实很简单。
与其从 actix-service
中导入 Service
和 Transform
的定义,它们的定义已过时(或更新?),而应直接从 [=14] 中导入=] 子模块 dev
,像这样:
use std::pin::Pin;
use std::task::{Context, Poll};
// use actix_service::{Service, Transform};
use actix_web::dev::{ServiceRequest, ServiceResponse, Service, Transform};
use actix_web::Error;
use futures::future::{ok, Ready};
use futures::Future;
我一直在尝试根据 Actix-web 文档中列出的示例编译 SayHi
中间件:https://actix.rs/docs/middleware/。问题是在最新版本的 Actix-web (3.3.2) 和 Rust (1.55) 中,此代码无法编译,模板缺少参数(大概是因为请求类型已移至它们?),即使我移动请求类型到正确位置其他东西中断。
如何在最新版本的 actix-web 中实现像 SayHi
示例这样的简单中间件?
我对 Rust 比较陌生,所以如果我遗漏了一些明显的东西,请告诉我,但我已经做了两天了,我似乎无法找到或制作任何成功创建简单代码的代码最新版本的 actix-web 中间件。
这是截至 2021 年 10 月 16 日的 actix 文档中的示例代码:
use std::pin::Pin;
use std::task::{Context, Poll};
use actix_service::{Service, Transform};
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
use futures::future::{ok, Ready};
use futures::Future;
pub struct SayHi;
impl<S, B> Transform<S> for SayHi
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = SayHiMiddleware<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>;
fn new_transform(&self, service: S) -> Self::Future {
ok(SayHiMiddleware { service })
}
}
pub struct SayHiMiddleware<S> {
service: S,
}
impl<S, B> Service for SayHiMiddleware<S>
where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
S::Future: 'static,
B: 'static,
{
type Request = ServiceRequest;
type Response = ServiceResponse<B>;
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
fn call(&mut self, req: ServiceRequest) -> Self::Future {
println!("Hi from start. You requested: {}", req.path());
let fut = self.service.call(req);
Box::pin(async move {
let res = fut.await?;
println!("Hi from response");
Ok(res)
})
}
}
事实证明这个解决方案确实很简单。
与其从 actix-service
中导入 Service
和 Transform
的定义,它们的定义已过时(或更新?),而应直接从 [=14] 中导入=] 子模块 dev
,像这样:
use std::pin::Pin;
use std::task::{Context, Poll};
// use actix_service::{Service, Transform};
use actix_web::dev::{ServiceRequest, ServiceResponse, Service, Transform};
use actix_web::Error;
use futures::future::{ok, Ready};
use futures::Future;