pub struct GrpcPlugin { /* private fields */ }Expand description
Serves tonic services from an Autumn app, on a dedicated HTTP/2 listener
or on Autumn’s HTTP port (listener = "shared").
use autumn_plugin_grpc::GrpcPlugin;
autumn_web::app()
.plugin(
GrpcPlugin::new()
.add_service(GreeterServer::new(MyGreeter))
.file_descriptor_set(greeter::FILE_DESCRIPTOR_SET),
)
.run()
.await;The plugin reads [grpc] from autumn.toml (see GrpcConfig).
Fluent setters apply on top of the file values.
On boot, the plugin:
- builds the services (with
AppStateforadd_service_with), - binds the listener (a failure aborts boot),
- serves health and (in
dev/test) reflection, - adds a
grpchealth indicator andgrpc_server_*metrics to the actuator, - puts a
GrpcHandleintoAppState.
On shutdown, it drains (see GrpcHandle::shutdown).
Implementations§
Source§impl GrpcPlugin
impl GrpcPlugin
Sourcepub fn add_service<S>(self, service: S) -> Selfwhere
S: Service<Request<TonicBody>, Error = Infallible> + NamedService + Clone + Send + Sync + 'static,
S::Response: IntoResponse,
S::Future: Send + 'static,
pub fn add_service<S>(self, service: S) -> Selfwhere
S: Service<Request<TonicBody>, Error = Infallible> + NamedService + Clone + Send + Sync + 'static,
S::Response: IntoResponse,
S::Future: Send + 'static,
Serve a tonic service, for example GreeterServer::new(MyGreeter).
Sourcepub fn add_service_with<S, F>(self, build: F) -> Selfwhere
F: FnOnce(&AppState) -> S + Send + 'static,
S: Service<Request<TonicBody>, Error = Infallible> + NamedService + Clone + Send + Sync + 'static,
S::Response: IntoResponse,
S::Future: Send + 'static,
pub fn add_service_with<S, F>(self, build: F) -> Selfwhere
F: FnOnce(&AppState) -> S + Send + 'static,
S: Service<Request<TonicBody>, Error = Infallible> + NamedService + Clone + Send + Sync + 'static,
S::Response: IntoResponse,
S::Future: Send + 'static,
Serve a tonic service that the plugin builds at startup, from
AppState. Use this when the service needs the database pool or
other app state.
GrpcPlugin::new().add_service_with(|state| {
GreeterServer::new(MyGreeter::new(state.clone()))
})Sourcepub fn file_descriptor_set(self, encoded: &'static [u8]) -> Self
pub fn file_descriptor_set(self, encoded: &'static [u8]) -> Self
Add an encoded FileDescriptorSet for server reflection. Get it from
tonic_prost_build (file_descriptor_set_path) and
include_bytes!.
Sourcepub fn layer<L>(self, layer: L) -> Selfwhere
L: Layer<Route> + Clone + Send + Sync + 'static,
L::Service: Service<Request> + Clone + Send + Sync + 'static,
<L::Service as Service<Request>>::Response: IntoResponse + 'static,
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
pub fn layer<L>(self, layer: L) -> Selfwhere
L: Layer<Route> + Clone + Send + Sync + 'static,
L::Service: Service<Request> + Clone + Send + Sync + 'static,
<L::Service as Service<Request>>::Response: IntoResponse + 'static,
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
Wrap every user service with a tower layer (for example rate limits
or tracing). The layer does not wrap health or reflection, so probes
and tools still work. For an auth layer, use guard.
Layers apply in call order: the last layer is the outermost.
Sourcepub fn interceptor<F>(self, interceptor: F) -> Self
pub fn interceptor<F>(self, interceptor: F) -> Self
Run a tonic interceptor before every user call. Like
layer, it does not wrap health or reflection. For an
auth interceptor, use guard_interceptor.
GrpcPlugin::new().interceptor(|mut request: tonic::Request<()>| {
request.extensions_mut().insert(RequestStart(Instant::now()));
Ok(request)
})Sourcepub fn guard<L>(self, layer: L, label: impl Into<String>) -> Selfwhere
L: Layer<Route> + Clone + Send + Sync + 'static,
L::Service: Service<Request> + Clone + Send + Sync + 'static,
<L::Service as Service<Request>>::Response: IntoResponse + 'static,
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
pub fn guard<L>(self, layer: L, label: impl Into<String>) -> Selfwhere
L: Layer<Route> + Clone + Send + Sync + 'static,
L::Service: Service<Request> + Clone + Send + Sync + 'static,
<L::Service as Service<Request>>::Response: IntoResponse + 'static,
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
Guard every user service with layer, for example an auth layer.
autumn routes then shows the services as gated, with label as
the middleware name.
Sourcepub fn guard_interceptor<F>(
self,
interceptor: F,
label: impl Into<String>,
) -> Self
pub fn guard_interceptor<F>( self, interceptor: F, label: impl Into<String>, ) -> Self
Guard every user service with a tonic interceptor. Like
guard, with an interceptor in place of a layer.
Sourcepub fn public(self) -> Self
pub fn public(self) -> Self
Declare the user services open to all clients. autumn routes then
shows them as public. Without guard* or public, the services are
unclassified, and autumn routes audit fails.
Sourcepub fn route_infos(&self) -> Vec<RouteInfo>
pub fn route_infos(&self) -> Vec<RouteInfo>
The gRPC services as autumn routes lists them. The method is
GRPC and the path is /<service>/*. These routes are on the gRPC
listener, or on the HTTP port in shared mode. A plugin on a
non-default section uses the method GRPC:<section>. Autumn 0.8
refuses two plugins that declare the same method and path, and two
servers can serve one service on different ports.
Sourcepub fn configure(
self,
apply: impl Fn(&mut GrpcConfig) + Send + Sync + 'static,
) -> Self
pub fn configure( self, apply: impl Fn(&mut GrpcConfig) + Send + Sync + 'static, ) -> Self
Change the configuration in code, after files and environment.
Sourcepub fn config(self, config: GrpcConfig) -> Self
pub fn config(self, config: GrpcConfig) -> Self
Use config and read no files or environment variables. Fluent
setters still apply on top.
Sourcepub fn config_section(self, section: impl Into<String>) -> Self
pub fn config_section(self, section: impl Into<String>) -> Self
Read a different section than [grpc]. The environment prefix
changes with the section: grpc_admin reads AUTUMN_GRPC_ADMIN__*.
Use this to run two servers.
Sourcepub fn development(self, development: bool) -> Self
pub fn development(self, development: bool) -> Self
Force development (true) or production (false) defaults for
auto settings. By default, the active profile decides.
Sourcepub fn listener(self, listener: Listener) -> Self
pub fn listener(self, listener: Listener) -> Self
Choose the listener: Listener::Dedicated (default) or
Listener::Shared (Autumn’s HTTP port, feature multiplex).
Sourcepub fn handle(&self) -> GrpcHandle
pub fn handle(&self) -> GrpcHandle
A handle to this plugin’s server. It is valid before and after boot.
Sourcepub fn service_names(&self) -> &[&'static str]
pub fn service_names(&self) -> &[&'static str]
Names of the services added with add_service*.
Sourcepub fn effective_config(&self) -> Result<&GrpcConfig, &ConfigError>
pub fn effective_config(&self) -> Result<&GrpcConfig, &ConfigError>
Trait Implementations§
Source§impl Debug for GrpcPlugin
impl Debug for GrpcPlugin
Source§impl Default for GrpcPlugin
impl Default for GrpcPlugin
Source§impl Plugin for GrpcPlugin
impl Plugin for GrpcPlugin
Source§fn name(&self) -> Cow<'static, str>
fn name(&self) -> Cow<'static, str>
The name contains the config section. Two servers need two sections.
Source§fn build(self, app: AppBuilder) -> AppBuilder
fn build(self, app: AppBuilder) -> AppBuilder
Auto Trait Implementations§
impl !Freeze for GrpcPlugin
impl !RefUnwindSafe for GrpcPlugin
impl !Sync for GrpcPlugin
impl !UnwindSafe for GrpcPlugin
impl Forget for GrpcPlugin
impl Send for GrpcPlugin
impl Unpin for GrpcPlugin
impl UnsafeUnpin for GrpcPlugin
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§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::Request