-
-
Notifications
You must be signed in to change notification settings - Fork 2
Add optional rustapi-grpc crate (tonic/prost) #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| [package] | ||
| name = "rustapi-grpc" | ||
| description = "gRPC integration helpers for RustAPI powered by Tonic" | ||
| documentation = "https://docs.rs/rustapi-grpc" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| authors.workspace = true | ||
| license.workspace = true | ||
| repository.workspace = true | ||
| keywords = ["web", "framework", "api", "grpc", "tonic"] | ||
| categories = ["web-programming::http-server", "network-programming"] | ||
| rust-version.workspace = true | ||
| readme = "README.md" | ||
|
|
||
| [dependencies] | ||
| rustapi-core = { workspace = true } | ||
| tokio = { workspace = true, features = ["macros"] } | ||
| tonic = { workspace = true, features = ["transport"] } | ||
| prost = { workspace = true } | ||
|
|
||
| [dev-dependencies] | ||
| tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] } | ||
| tonic-health = "0.14" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # rustapi-grpc | ||
|
|
||
| `rustapi-grpc` provides gRPC integration helpers for RustAPI with [Tonic](https://github.com/hyperium/tonic). | ||
|
|
||
| ## What it gives you | ||
|
|
||
| - `run_concurrently(http, grpc)`: run two server futures together. | ||
| - `run_rustapi_and_grpc(app, http_addr, grpc)`: convenience helper for RustAPI + gRPC side-by-side. | ||
| - `run_rustapi_and_grpc_with_shutdown(app, http_addr, signal, grpc_with_shutdown)`: shared shutdown signal for both servers. | ||
| - Re-exports: `tonic`, `prost`. | ||
|
|
||
| ## Example | ||
|
|
||
| ```rust,ignore | ||
| use rustapi_rs::grpc::{run_rustapi_and_grpc, 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 http_app = RustApi::new().route("/health", get(health)); | ||
|
|
||
| let grpc_addr = "127.0.0.1:50051".parse()?; | ||
| let grpc_server = tonic::transport::Server::builder() | ||
| .add_service(MyGreeterServer::new(MyGreeter::default())) | ||
| .serve(grpc_addr); | ||
|
|
||
| run_rustapi_and_grpc(http_app, "127.0.0.1:8080", grpc_server).await?; | ||
| Ok(()) | ||
| } | ||
|
Comment on lines
+14
to
+32
|
||
| ``` | ||
|
|
||
| ## Shared shutdown (Ctrl+C) | ||
|
|
||
| ```rust,ignore | ||
| use rustapi_rs::grpc::{run_rustapi_and_grpc_with_shutdown, tonic}; | ||
|
|
||
| let grpc_addr = "127.0.0.1:50051".parse()?; | ||
|
|
||
| run_rustapi_and_grpc_with_shutdown( | ||
| http_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?; | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rustapi-grpcusestokio::spawnandtokio::sync::watch, but the dependency only enables themacrosfeature. Other crates in this workspace explicitly list the Tokio features they rely on; consider adding the needed Tokio features here as well (at leastrtandsync, and optionallytimeonly for tests) so the crate doesn’t implicitly depend on Tokio default features or a workspace-wide configuration.