reinhardt-grpc
gRPC foundation crate for the Reinhardt framework
Overview
This crate provides the foundation for gRPC functionality in the Reinhardt framework. It includes only framework-level common types and adapter traits, with domain-specific implementations left to users.
Installation
Add reinhardt to your Cargo.toml:
[]
= { = "0.2.0", = ["grpc"] }
# Or use a preset:
# reinhardt = { version = "0.2.0", features = ["standard"] } # Recommended
# reinhardt = { version = "0.2.0", features = ["full"] } # All features
Then import gRPC features:
use ;
use ;
Note: gRPC features are included in the standard and full feature presets.
Features
- Common Protobuf types (Empty, Timestamp, Error, PageInfo, BatchResult)
- Adapter traits for gRPC service integration
- Error handling utilities
- Dependency injection support (optional, with
difeature)
1. Common Protobuf Types
Generic types provided by the framework:
// Empty - Empty response
message Empty {}
// Timestamp - Timestamp representation
message Timestamp {
int64 seconds = 1;
int32 nanos = 2;
}
// Error - Error information
message Error {
string code = 1;
string message = 2;
map<string, string> metadata = 3;
}
// PageInfo - Pagination information
message PageInfo {
int32 page = 1;
int32 per_page = 2;
int32 total = 3;
bool has_next = 4;
bool has_prev = 5;
}
// BatchResult - Batch operation result
message BatchResult {
int32 success_count = 1;
int32 failure_count = 2;
repeated Error errors = 3;
}
2. Adapter Traits
Traits for integrating gRPC services with other framework components (such as GraphQL):
use ;
/// Adapter for Query/Mutation
/// Adapter for Subscription
3. Error Handling
gRPC error types and conversions:
use ;
Usage
Using Your Own .proto Files
- Create a
proto/directory in your project
my-app/
├── proto/
│ ├── user.proto
│ └── product.proto
├── src/
│ └── main.rs
└── Cargo.toml
- Compile .proto files in
build.rs
// build.rs
- Add dependencies to
Cargo.toml
[]
= "0.2.0"
= "0.12"
= "0.13"
[]
= "0.12"
= "0.7"
- Use generated code
// src/lib.rs
// Use common types from reinhardt-grpc
use ;
Dependency Injection
Enable the di feature to use dependency injection in gRPC handlers:
[]
= { = "0.2.0", = ["di"] }
= "0.2.0"
Basic Usage
use ;
use InjectionContext;
use ;
use Arc;
Cache Control
Control dependency caching with the cache parameter:
async
Integration with GraphQL
When using with the reinhardt-graphql crate, refer to the
reinhardt-graphql documentation.
License
Licensed under the BSD 3-Clause License.