Multiplexer

Struct Multiplexer 

Source
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§

Source§

impl<Grpc, Web> Multiplexer<Grpc, Web>
where Grpc: Service<Request<Body>>, Web: Service<Request<Body>>,

Source

pub fn new(grpc: Grpc, web: Web) -> Self

This function consumes two Services, and returns a Multiplexer

Trait Implementations§

Source§

impl<Grpc, Web, GrpcBody, WebBody> Service<Request<Body>> for Multiplexer<Grpc, Web>
where Grpc: Service<Request<Body>, Response = Response<GrpcBody>>, Web: Service<Request<Body>, Response = Response<WebBody>>, GrpcBody: HttpBody, WebBody: HttpBody, Grpc::Error: Into<Box<dyn Error + Send + Sync + 'static>>, Web::Error: Into<Box<dyn Error + Send + Sync + 'static>>,

Source§

type Error = Box<dyn Error + Sync + Send>

Generic error that can be moved between threads

Source§

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>>

Responses given by the service.
Source§

type Future = EncapsulatedFuture<<Grpc as Service<Request<Body>>>::Future, <Web as Service<Request<Body>>>::Future>

The future response value.
Source§

fn call(&mut self, req: Request<Body>) -> Self::Future

Process the request and return the response asynchronously. Read more

Auto Trait Implementations§

§

impl<Grpc, Web> Freeze for Multiplexer<Grpc, Web>
where Grpc: Freeze, Web: Freeze,

§

impl<Grpc, Web> RefUnwindSafe for Multiplexer<Grpc, Web>
where Grpc: RefUnwindSafe, Web: RefUnwindSafe,

§

impl<Grpc, Web> Send for Multiplexer<Grpc, Web>
where Grpc: Send, Web: Send,

§

impl<Grpc, Web> Sync for Multiplexer<Grpc, Web>
where Grpc: Sync, Web: Sync,

§

impl<Grpc, Web> Unpin for Multiplexer<Grpc, Web>
where Grpc: Unpin, Web: Unpin,

§

impl<Grpc, Web> UnwindSafe for Multiplexer<Grpc, Web>
where Grpc: UnwindSafe, Web: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.