volo-grpc和volo-http的中间件不知是否通用,是否支持动态加载
- Dominant language
- Rust
- Stars
- 2.6k
- Forks
- 223
- Avg merge
- 18m
- Merged PRs (30d)
- 1
Description
先说问题:
问题1:http服务能使用开发的测试中间件TestLayer和DemoLayer,但是grpc服务不能,所以一问:volo_grpc和volo_http的中间件是否通用
问题2:开发的测试中间件TestLayer和DemoLayer能否放在一个Vec中,如果能,哪这个Vec的类型如何定义,如下:
```
let mut layer_data = Vec::new(); //这里的变量类型如何定义
layer_data.push(TestLayer::new());
layer_data.push(DemoLayer::new());
```
volo-grpc和volo-http的中间件不知是否通用
下面是开发的测试中间件
```
//-----DemoLayer--------
use motore::{layer::Layer, service::Service};
use volo_http::{
context::ServerContext, request::ServerRequest, response::ServerResponse, server::IntoResponse,
};
#[derive(Debug, Clone)]
pub struct DemoLayer;
impl DemoLayer {
pub fn new() -> Self {
Self
}
}
impl Layer for DemoLayer {
type Service = Demo;
fn layer(self, inner: S) -> Self::Service {
println!("-----demo layer-----");
Self::Service::new(inner)
}
}
#[derive(Debug, Clone)]
pub struct Demo {
inner: S,
}
impl Demo {
pub fn new(inner: S) -> Self {
Self { inner }
}
}
impl Service for Demo
where
S: Service + Send + Sync + 'static,
S::Response: IntoResponse,
S::Error: IntoResponse,
{
type Response = ServerResponse;
type Error = S::Error;
async fn call(
&self,
cx: &mut ServerContext,
req: ServerRequest,
) -> Result {
println!("-----demo call-----");
Ok(self.inner.call(cx, req).await.into_response())
}
}
```
```
//-----TestLayer--------
use motore::{layer::Layer, service::Service};
use volo_http::{
context::ServerContext, request::ServerRequest, response::ServerResponse, server::IntoResponse,
};
#[derive(Debug, Clone)]
pub struct TestLayer;
impl TestLayer {
pub fn new() -> Self {
Self
}
}
impl Layer for TestLayer {
type Service = Test;
fn layer(self, inner: S) -> Self::Service {
println!("-----test layer-----");
Self::Service::new(inner)
}
}
#[derive(Debug, Clone)]
pub struct Test {
inner: S,
}
impl Test {
pub fn new(inner: S) -> Self {
Self { inner }
}
}
impl Service for Test
where
S: Service + Send + Sync + 'static,
S::Response: IntoResponse,
S::Error: IntoResponse,
{
type Response = ServerResponse;
type Error = S::Error;
async fn call(
&self,
cx: &mut ServerContext,
req: ServerRequest,
) -> Result {
println!("-----test call-----");
Ok(self.inner.call(cx, req).await.into_response())
}
}
```
http 服务启动(正常)
```
use volo_http::{Router, Server as HttpServer}
let http_app = Router::new().route("xxx", get(xx)).route("xxx", get(xx)).layer(TestLayer::new()).layer(DemoLayer::new());
let addr: SocketAddr = "0.0.0.0:8080".parse().unwrap();
let addr = volo::net::Address::from(addr);
HttpServer::new(app).run(addr).await.unwrap();
```
grpc 服务启动(问题,中间件不通用)
```
use volo_grpc::server::Server as GrpcServer;
let addr: SocketAddr = "[::]:8080".parse().unwrap();
let addr = volo::net::Address::from(addr);
GrpcServer::new().add_service(s).layer(TestLayer::new()).layer(DemoLayer::new()).run(addr).await.unwrap();
```
Contributor guide
Assessment
This issue has not been assessed yet.