|
| 1 | +# rustapi-grpc |
| 2 | + |
| 3 | +`rustapi-grpc` provides gRPC integration helpers for RustAPI with [Tonic](https://github.com/hyperium/tonic). |
| 4 | + |
| 5 | +## What it gives you |
| 6 | + |
| 7 | +- `run_concurrently(http, grpc)`: run two server futures together. |
| 8 | +- `run_rustapi_and_grpc(app, http_addr, grpc)`: convenience helper for RustAPI + gRPC side-by-side. |
| 9 | +- `run_rustapi_and_grpc_with_shutdown(app, http_addr, signal, grpc_with_shutdown)`: shared shutdown signal for both servers. |
| 10 | +- Re-exports: `tonic`, `prost`. |
| 11 | + |
| 12 | +## Example |
| 13 | + |
| 14 | +```rust,ignore |
| 15 | +use rustapi_rs::grpc::{run_rustapi_and_grpc, tonic}; |
| 16 | +use rustapi_rs::prelude::*; |
| 17 | +
|
| 18 | +#[rustapi_rs::get("/health")] |
| 19 | +async fn health() -> &'static str { "ok" } |
| 20 | +
|
| 21 | +#[tokio::main] |
| 22 | +async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { |
| 23 | + let http_app = RustApi::new().route("/health", get(health)); |
| 24 | +
|
| 25 | + let grpc_addr = "127.0.0.1:50051".parse()?; |
| 26 | + let grpc_server = tonic::transport::Server::builder() |
| 27 | + .add_service(MyGreeterServer::new(MyGreeter::default())) |
| 28 | + .serve(grpc_addr); |
| 29 | +
|
| 30 | + run_rustapi_and_grpc(http_app, "127.0.0.1:8080", grpc_server).await?; |
| 31 | + Ok(()) |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +## Shared shutdown (Ctrl+C) |
| 36 | + |
| 37 | +```rust,ignore |
| 38 | +use rustapi_rs::grpc::{run_rustapi_and_grpc_with_shutdown, tonic}; |
| 39 | +
|
| 40 | +let grpc_addr = "127.0.0.1:50051".parse()?; |
| 41 | +
|
| 42 | +run_rustapi_and_grpc_with_shutdown( |
| 43 | + http_app, |
| 44 | + "127.0.0.1:8080", |
| 45 | + tokio::signal::ctrl_c(), |
| 46 | + move |shutdown| { |
| 47 | + tonic::transport::Server::builder() |
| 48 | + .add_service(MyGreeterServer::new(MyGreeter::default())) |
| 49 | + .serve_with_shutdown(grpc_addr, shutdown) |
| 50 | + }, |
| 51 | +).await?; |
| 52 | +``` |
0 commit comments