Better example code for google protos (#542)

* Better example code for google protos

* Try fix windows build

* Try fix windows build

* Remove generated code in repo
This commit is contained in:
Fuyang Liu
2021-01-21 13:59:23 -05:00
committed by GitHub
parent 4fe5beb353
commit 729308b65b
9 changed files with 1184 additions and 197 deletions
+61
View File
@@ -21,6 +21,7 @@ tonic-build = <tonic-version>
### Simple
In `build.rs`:
```rust
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::compile_protos("proto/service.proto")?;
@@ -41,3 +42,63 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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
```
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(
&["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)