Skip to main content

run_rustapi_and_grpc_with_shutdown

Function run_rustapi_and_grpc_with_shutdown 

Source
pub async fn run_rustapi_and_grpc_with_shutdown<GF, GE, SF, F>(
    app: RustApi,
    http_addr: impl AsRef<str>,
    shutdown_signal: SF,
    grpc_with_shutdown: F,
) -> Result<()>
where GF: Future<Output = Result<(), GE>> + Send, GE: Error + Send + Sync + 'static, SF: Future<Output = ()> + Send + 'static, F: FnOnce(ShutdownFuture) -> GF,
Expand description

Run RustAPI HTTP and gRPC servers together with a shared shutdown signal.

This helper lets you provide a single shutdown signal (for example tokio::signal::ctrl_c()) and uses it for both servers.

§Example

use rustapi_rs::grpc::{run_rustapi_and_grpc_with_shutdown, tonic};
use rustapi_rs::prelude::*;

#[rustapi_rs::get("/health")]
async fn health() -> &'static str { "ok" }

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let app = RustApi::new().route("/health", get(health));
    let grpc_addr = "127.0.0.1:50051".parse()?;

    run_rustapi_and_grpc_with_shutdown(
        app,
        "127.0.0.1:8080",
        tokio::signal::ctrl_c(),
        move |shutdown| {
            tonic::transport::Server::builder()
                .add_service(MyGreeterServer::new(MyGreeter::default()))
                .serve_with_shutdown(grpc_addr, shutdown)
        },
    ).await?;

    Ok(())
}