Is there how to apply two or more layers or interceptors to a service? #944
-
Suppose we have the following code. I would like to apply interceptor as follows
How can this be resolved? #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse()?;
let sample_a = SampleApiAImpl::default();
let sample_b = SampleApiBImpl::default();
let sample_c = SampleApiCImpl::default();
let svc_a = SampleApiAServer::new(sample_a);
let svc_b = SampleApiBServer::new(sample_b);
let svc_c = SampleApiCServer::new(sample_c);
Server::builder()
.layer(tonic::service::interceptor(intercept0))
.add_service(svc_a)
.add_service(svc_b)
.add_service(svc_c)
.serve(addr)
.await?;
Ok(())
}
fn intercept0(req: Request<()>) -> Result<Request<()>, Status> {
println!("Intercepting 0");
Ok(req)
}
fn intercept1(req: Request<()>) -> Result<Request<()>, Status> {
println!("Intercepting 1");
Ok(req)
}
fn intercept2(req: Request<()>) -> Result<Request<()>, Status> {
println!("Intercepting 2");
Ok(req)
}
fn intercept3(req: Request<()>) -> Result<Request<()>, Status> {
println!("Intercepting 3");
Ok(req)
} |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Mar 21, 2022
Replies: 1 comment 1 reply
-
Look at tower middleware. There are examples in this repo. Interceptors are implemented as tower middleware internally. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mm1995tk
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Look at tower middleware. There are examples in this repo. Interceptors are implemented as tower middleware internally.