pub struct ProxyService<U> { /* private fields */ }Expand description
The proxy as a tower service, built by
ProxyServer::service.
A request with a gRPC or gRPC-Web content type goes to the upstream as it
arrived, so one listener carries REST and native gRPC. gRPC-Web is the
upstream’s to translate (tonic-web’s GrpcWebLayer around its services),
or the proxy’s with grpc_web.translate for an upstream that speaks only
gRPC. A gRPC-Web
answer gets the proxy’s CORS policy (cors.grpc_web), since the proxy
answers the browser’s preflight for it. Every other request goes to the
proxy’s routes (transcoded RPCs, health, metrics, OpenAPI, OIDC,
forward-auth, extra routes) behind the proxy’s middleware. A request no
route matches is answered 404, or handed to the service set with
with_fallback. Native gRPC and the fallback pass
only the guards whose scope names them (grpc, fallback); a guard’s
rejection of a gRPC call is a gRPC status.
Serve it with serve or
serve_with (TLS, a connection limit), or hand it to
any server that takes a tower service of http types: a Unix socket, an
existing hyper or axum server. Native gRPC needs HTTP/2 on that server
(ALPN h2 next to http/1.1 behind TLS). Such a server tells the proxy
which connection a request came on with
for_connection.
§Examples
use structured_proxy::ProxyServer;
// The embedder's own tonic services, called in process.
let grpc = tonic::service::Routes::default();
let service = ProxyServer::from_yaml_str("service:\n name: demo\n")?.service(grpc)?;Implementations§
Source§impl<U: Upstream> ProxyService<U>
impl<U: Upstream> ProxyService<U>
Sourcepub fn with_fallback<F>(self, fallback: F) -> Selfwhere
F: Service<Request, Error = Infallible> + Clone + Send + Sync + 'static,
F::Response: IntoResponse,
F::Future: Send + 'static,
pub fn with_fallback<F>(self, fallback: F) -> Selfwhere
F: Service<Request, Error = Infallible> + Clone + Send + Sync + 'static,
F::Response: IntoResponse,
F::Future: Send + 'static,
Hand the requests no route matches to fallback instead of answering
404: an embedder’s own REST routes, a static site, anything that is a
tower service. Only the guards whose scope names fallback traffic see
them; CORS and tracing are the fallback’s own.
A URL whose path a transcoded template’s route matches, but which no
binding answers (its field template or custom verb does not fit), is
the fallback’s too. A request whose path a route answers but not with
its method stays with the proxy (405), as do every gRPC request and
every browser preflight for a gRPC-Web call, which follows the call it
announces.
Sourcepub fn for_connection(&self, connection: impl Into<ConnectionInfo>) -> Self
pub fn for_connection(&self, connection: impl Into<ConnectionInfo>) -> Self
This service for the requests of one connection: the proxy’s
middleware sees its client’s address (rate limits by IP, the auth
decider), and a tonic upstream in process reads it with
Request::remote_addr, and its TLS client certificates with
Request::peer_certs, for native and transcoded calls alike.
A server of your own calls it once per accepted connection, with what
tonic’s Connected trait
reports for the stream. serve_with does this
itself.
§Examples
use structured_proxy::ProxyServer;
use tonic::transport::server::Connected;
let proxy = ProxyServer::from_yaml_str("service:\n name: demo\n")?
.service(tonic::service::Routes::default())?;
let listener = tokio::net::TcpListener::bind("0.0.0.0:8443").await?;
let (tcp, _) = listener.accept().await?;
// After a TLS handshake, the `TlsStream` reports the client's certificates too.
let service = proxy.for_connection(tcp.connect_info());Without it, a request’s peer comes from the ConnectInfo an outer
axum server recorded, when there is one.
Trait Implementations§
Source§impl<U: Clone> Clone for ProxyService<U>
impl<U: Clone> Clone for ProxyService<U>
Source§impl<U> Debug for ProxyService<U>
impl<U> Debug for ProxyService<U>
Auto Trait Implementations§
impl<U> !RefUnwindSafe for ProxyService<U>
impl<U> !UnwindSafe for ProxyService<U>
impl<U> Freeze for ProxyService<U>
impl<U> Send for ProxyService<U>
impl<U> Sync for ProxyService<U>
impl<U> Unpin for ProxyService<U>
impl<U> UnsafeUnpin for ProxyService<U>where
Arc<Shared<U>>: UnsafeUnpin,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, ReqBody, ResBody> GrpcService<ReqBody> for T
impl<T, ReqBody, ResBody> GrpcService<ReqBody> for T
Source§type ResponseBody = ResBody
type ResponseBody = ResBody
Source§fn poll_ready(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<(), <T as GrpcService<ReqBody>>::Error>>
fn poll_ready( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<(), <T as GrpcService<ReqBody>>::Error>>
Ready when the service is able to process requests. Read moreSource§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<S, R> ServiceExt<R> for Swhere
S: Service<R>,
impl<S, R> ServiceExt<R> for Swhere
S: Service<R>,
Source§fn into_make_service(self) -> IntoMakeService<S>
fn into_make_service(self) -> IntoMakeService<S>
MakeService, that is a Service whose
response is another service. Read moreSource§fn into_make_service_with_connect_info<C>(
self,
) -> IntoMakeServiceWithConnectInfo<S, C>
fn into_make_service_with_connect_info<C>( self, ) -> IntoMakeServiceWithConnectInfo<S, C>
MakeService, that will store C’s
associated ConnectInfo in a request extension such that ConnectInfo
can extract it. Read moreSource§fn handle_error<F, T>(self, f: F) -> HandleError<Self, F, T>
fn handle_error<F, T>(self, f: F) -> HandleError<Self, F, T>
HandleError, that will handle errors
by converting them into responses. Read moreSource§impl<T, Request> ServiceExt<Request> for T
impl<T, Request> ServiceExt<Request> for T
Source§fn ready(&mut self) -> Ready<'_, Self, Request> ⓘwhere
Self: Sized,
fn ready(&mut self) -> Ready<'_, Self, Request> ⓘwhere
Self: Sized,
Source§fn ready_oneshot(self) -> ReadyOneshot<Self, Request> ⓘwhere
Self: Sized,
fn ready_oneshot(self) -> ReadyOneshot<Self, Request> ⓘwhere
Self: Sized,
Source§fn oneshot(self, req: Request) -> Oneshot<Self, Request> ⓘwhere
Self: Sized,
fn oneshot(self, req: Request) -> Oneshot<Self, Request> ⓘwhere
Self: Sized,
Service, calling it with the provided request once it is ready.Source§fn and_then<F>(self, f: F) -> AndThen<Self, F>
fn and_then<F>(self, f: F) -> AndThen<Self, F>
poll_ready method. Read moreSource§fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
poll_ready method. Read moreSource§fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
poll_ready method. Read moreSource§fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
Result<Self::Response, Self::Error>)
to a different value, regardless of whether the future succeeds or
fails. Read more