https://github.com/tomdev5/afit-tonic-build
A fork of tonic-build that replaces async-trait with AFIT
https://github.com/tomdev5/afit-tonic-build
async grpc proto rpc rust tonic
Last synced: 2 months ago
JSON representation
A fork of tonic-build that replaces async-trait with AFIT
- Host: GitHub
- URL: https://github.com/tomdev5/afit-tonic-build
- Owner: tomDev5
- License: other
- Created: 2025-06-06T11:40:38.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-06T12:24:33.000Z (about 1 year ago)
- Last Synced: 2025-06-06T13:27:09.802Z (about 1 year ago)
- Topics: async, grpc, proto, rpc, rust, tonic
- Language: Rust
- Homepage:
- Size: 232 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tonic-build
Compiles proto files via prost and generates service stubs and proto definitions for use with tonic.
## Features
Required dependencies
```toml
[dependencies]
tonic = ""
prost = ""
[build-dependencies]
tonic-build = ""
```
## Examples
### Simple
In `build.rs`:
```rust
fn main() -> Result<(), Box> {
tonic_build::compile_protos("proto/service.proto")?;
Ok(())
}
```
### Configuration
```rust
fn main() -> Result<(), Box> {
tonic_build::configure()
.build_server(false)
.compile_protos(
&["proto/helloworld/helloworld.proto"],
&["proto/helloworld"],
)?;
Ok(())
}
```
See [more examples here](https://github.com/hyperium/tonic/tree/master/examples)
### Google APIs example
A good way to use Google API is probably using git submodules.
So suppose in our `proto` folder we do:
```
git submodule add https://github.com/googleapis/googleapis
git submodule update --remote
```
And a bunch of Google proto files in structure will be like this:
```
├── googleapis
│ └── google
│ ├── api
│ │ ├── annotations.proto
│ │ ├── client.proto
│ │ ├── field_behavior.proto
│ │ ├── http.proto
│ │ └── resource.proto
│ └── pubsub
│ └── v1
│ ├── pubsub.proto
│ └── schema.proto
```
Then we can generate Rust code via this setup in our `build.rs`
```rust
fn main() {
tonic_build::configure()
.build_server(false)
//.out_dir("src/google") // you can change the generated code's location
.compile_protos(
&["proto/googleapis/google/pubsub/v1/pubsub.proto"],
&["proto/googleapis"], // specify the root location to search proto dependencies
).unwrap();
}
```
Then you can reference the generated Rust like this this in your code:
```rust
pub mod api {
tonic::include_proto!("google.pubsub.v1");
}
use api::{publisher_client::PublisherClient, ListTopicsRequest};
```
Or if you want to save the generated code in your own code base,
you can uncomment the line `.out_dir(...)` above, and in your lib file
config a mod like this:
```rust
pub mod google {
#[path = ""]
pub mod pubsub {
#[path = "google.pubsub.v1.rs"]
pub mod v1;
}
}
```
See [the example here](https://github.com/hyperium/tonic/tree/master/examples/src/gcp)