pub struct Multiplexer<Grpc, Web> { /* private fields */ }Expand description
Service that routes to a gRPC service and other service
This service checks the Content-Type header, and send all requests
with application/grpc to the grpc service, and all other requests
to the web service.
§Examples:
Routing to the web service:
use hyper::{header::CONTENT_TYPE, service::service_fn, Body, Request, Response};
use tower::{Service, ServiceExt};
async fn str_to_res(str: &'static str) -> Result<Response<Body>, Infallible> {
Ok(Response::new(Body::from(str)))
}
//Services that answer every request with a word
let grpc = service_fn(|_| str_to_res("gRPC"));
let web = service_fn(|_| str_to_res("web"));
let mut multiplex = Multiplexer::new(grpc, web);
//Request web without content-type header
let response = multiplex.call(Request::new(Body::empty())).await?;
let content = hyper::body::to_bytes(response.into_body()).await?;
assert_eq!(content, "web");Routing to the gRPC service:
//...
let grpc = service_fn(|_| str_to_res("gRPC"));
let web = service_fn(|_| str_to_res("web"));
let mut multiplex = Multiplexer::new(grpc, web);
//Request grpc using content-type header
let request = Request::builder()
.header(CONTENT_TYPE, "application/grpc")
.body(Body::empty())?;
let response = multiplex.call(request).await?;
let content = hyper::body::to_bytes(response.into_body()).await?;
assert_eq!(content, "gRPC");Implementations§
Trait Implementations§
Source§impl<Grpc, Web, GrpcBody, WebBody> Service<Request<Body>> for Multiplexer<Grpc, Web>
impl<Grpc, Web, GrpcBody, WebBody> Service<Request<Body>> for Multiplexer<Grpc, Web>
Source§fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>
Call inner services poll_ready, and propagate errors. Only is ready if both are ready.
Source§type Response = Response<EncapsulatedBody<GrpcBody, WebBody>>
type Response = Response<EncapsulatedBody<GrpcBody, WebBody>>
Responses given by the service.
Auto Trait Implementations§
impl<Grpc, Web> Freeze for Multiplexer<Grpc, Web>
impl<Grpc, Web> RefUnwindSafe for Multiplexer<Grpc, Web>where
Grpc: RefUnwindSafe,
Web: RefUnwindSafe,
impl<Grpc, Web> Send for Multiplexer<Grpc, Web>
impl<Grpc, Web> Sync for Multiplexer<Grpc, Web>
impl<Grpc, Web> Unpin for Multiplexer<Grpc, Web>
impl<Grpc, Web> UnwindSafe for Multiplexer<Grpc, Web>where
Grpc: UnwindSafe,
Web: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more