Module grpc

Module grpc 

Source
Available on crate feature router-grpc only.
Expand description

gRPC server infrastructure with TLS support.

Production-ready gRPC server with health checks and reflection. gRPC Server Infrastructure

This module provides a fluent builder API for configuring and running gRPC servers with support for:

  • TLS/mTLS encryption
  • gRPC reflection (service discovery)
  • Health checking
  • Graceful shutdown

§Quick Start

use allframe_core::grpc::GrpcServer;
use allframe_core::shutdown::GracefulShutdown;

// Simple server
GrpcServer::builder()
    .addr("[::1]:50051")
    .serve(my_service)
    .await?;

// Full-featured server
let shutdown = GracefulShutdown::new();

GrpcServer::builder()
    .addr("0.0.0.0:50051")
    .reflection(FILE_DESCRIPTOR_SET)
    .health_check()
    .tls_from_env()
    .graceful_shutdown(shutdown)
    .serve(my_service)
    .await?;

§TLS Configuration

TLS can be configured either directly or from environment variables:

use allframe_core::grpc::{GrpcServer, TlsConfig};

// Direct configuration
let tls = TlsConfig::new("cert.pem", "key.pem")
    .with_client_ca("ca.pem"); // For mTLS

GrpcServer::builder()
    .tls(tls)
    .serve(my_service)
    .await?;

// From environment variables
// GRPC_TLS_CERT, GRPC_TLS_KEY, GRPC_TLS_CLIENT_CA
GrpcServer::builder()
    .tls_from_env()
    .serve(my_service)
    .await?;

§gRPC Reflection

Enable service discovery with gRPC reflection:

// Include the file descriptor set from your build
pub const FILE_DESCRIPTOR_SET: &[u8] =
    include_bytes!(concat!(env!("OUT_DIR"), "/proto_descriptor.bin"));

GrpcServer::builder()
    .reflection(FILE_DESCRIPTOR_SET)
    .serve(my_service)
    .await?;

§Health Checking

Enable the standard gRPC health checking protocol:

GrpcServer::builder()
    .health_check()
    .serve(my_service)
    .await?;

Structs§

GrpcServerBuilder
Builder for configuring gRPC servers
TlsConfig
TLS configuration for gRPC servers

Enums§

GrpcServerError
Error type for gRPC server operations
TlsError
Errors that can occur during TLS configuration

Type Aliases§

GrpcServer
Convenience type alias