-
Notifications
You must be signed in to change notification settings - Fork 12
Add Rust Getting Started Codelab #51
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
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
0297a53
added stencil code
cjqzhao 96bb413
added completed code
cjqzhao b680d26
add completed cargo.toml
cjqzhao e38fb42
use tonic protobuf build
cjqzhao 8b1fe99
correct completed code
cjqzhao d3fe7b3
fix something minor
cjqzhao d2b4009
fixed grpc include
cjqzhao c4094b4
saving
cjqzhao 744f007
remove unnecessary dependencies
cjqzhao 62c52cb
uncommmented code
cjqzhao 6fa8de8
add completed codelab
cjqzhao 0c50f20
add README
cjqzhao b2e58e4
added stencil
cjqzhao 41a11a8
fix using proto!
cjqzhao b3b746d
fix up readme a bit
cjqzhao 32d359d
fixed data.rs
cjqzhao a2ff3e6
implement suggestions
cjqzhao 61bdf67
fixed some changes
cjqzhao 2e2545f
update dependencies
cjqzhao dbc43f5
updated codegen
cjqzhao 1cad5e4
Merge branch 'main' of https://github.com/grpc-ecosystem/grpc-codelab…
cjqzhao 9fe6963
give users pregenerated code
cjqzhao e4b8380
fix stencil code to have generated code
cjqzhao 08b5baf
add tutorial for generating code
cjqzhao 6f985dc
saving changes
cjqzhao 58cb309
move routeguide.proto to proto
cjqzhao b04e972
update build.rs command
cjqzhao caed74e
update codelab
cjqzhao 6fa337f
comment out imports in client.rs
cjqzhao 53eaac9
remove the cargo build target
cjqzhao e5323d8
delete bin binaries
cjqzhao dd34e2a
removed comments
cjqzhao 429cf0b
fix tutorial
cjqzhao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Generate Code from Proto Files | ||
|
||
This is a tutorial for generating code from Proto files using gRPC-Rust. This tutorial will utilize the RouteGuide example. | ||
|
||
### Prerequisites | ||
|
||
* [**Tonic**](https://github.com/hyperium/tonic.git), the open source repository that gRPC-Rust is build off on | ||
```sh | ||
$ git clone https://github.com/hyperium/tonic.git | ||
``` | ||
* [**Rust**](https://www.rust-lang.org/). | ||
* Follow installation instructions [here](https://www.rust-lang.org/tools/install). | ||
* [**Bazel 8.3.1**](https://bazel.build/). | ||
* Follow installation instructions [here](https://github.com/bazelbuild/bazel/releases). | ||
* [**Protocol buffer**](https://developers.google.com/protocol-buffers) **compiler**, `protoc`, [version 3](https://protobuf.dev/programming-guides/proto3). | ||
* For installation instructions, see [Protocol Buffer Compiler Installation](https://grpc.io/docs/protoc-installation/). | ||
* NOTE: Must need a version of Protoc 3.31.1 or higher. | ||
* **Rust plugins** for the protocol compiler: | ||
```sh | ||
$ cd tonic/protoc-gen-rust-grpc | ||
$ bazel build //src:protoc-gen-rust-grpc | ||
$ PLUGIN_PATH="$(pwd)/bazel-bin/src/protoc-gen-rust-grpc" | ||
``` | ||
|
||
* Update your PATH so that the protoc compiler can find the plugins: | ||
|
||
```sh | ||
export PATH="$(pwd)/bazel-bin/src/:$PATH" | ||
``` | ||
|
||
## Generating client and server code | ||
|
||
Next we need to generate the gRPC client and server interfaces from our `.proto` | ||
service definition. | ||
|
||
### Dependencies | ||
Edit `Cargo.toml` and add the dependency we'll need for this example, which is tonic-protobuf-build: | ||
|
||
```console | ||
cargo add tonic-protobuf-build | ||
``` | ||
|
||
### Compiling and Building Proto | ||
Create a `build.rs` file at the root of your crate. A build.rs script is a Rust program that Cargo executes before compiling your main project. Its purpose is to perform tasks like generating source code, linking to non-Rust libraries, or setting environment variables that influence the build process. | ||
|
||
In this case, we will be putting the command to compile and build the `.proto` file in build.rs. We will use gRPC's tonic_protobuf_build crate to generate code from the `.proto` file. | ||
```rust | ||
fn main() { | ||
tonic_protobuf_build::CodeGen::new() | ||
.include("proto") | ||
.inputs(["routeguide.proto"]) | ||
.output_dir("generated") | ||
.compile() | ||
.unwrap(); | ||
} | ||
``` | ||
Now, run | ||
```shell | ||
$ cargo build | ||
``` | ||
|
||
That's it. The generated code contains: | ||
|
||
- Struct definitions for message types `Point` and `Feature`. | ||
- A service trait we'll need to implement: `route_guide_server::RouteGuide`. | ||
- A client type we'll use to call the server: `route_guide_client::RouteGuideClient<T>`. | ||
|
||
If your are curious as to where the generated files are, keep reading. The mystery will be revealed | ||
soon! We can now move on to the fun part. | ||
|
||
## Bringing Generated Code into Scope | ||
|
||
The generated code is placed inside our target directory, in a location defined by the `OUT_DIR` | ||
environment variable that is set by cargo. For our example, this means you can find the generated | ||
code in a path similar to `target/debug/build/routeguide/out/routeguide.rs`. | ||
|
||
We can use gRPC's `include_proto` macro to bring the generated code into scope: | ||
|
||
```rust | ||
pub mod routeguide { | ||
tonic::include_proto!("routeguide"); | ||
} | ||
``` | ||
|
||
**Note**: The token passed to the `include_proto` macro (in our case "routeguide") is the name of | ||
the package declared in our `.proto` file, not a filename, e.g "routeguide.rs". | ||
|
||
With this in place, we can stub out our service implementation: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
[package] | ||
edition = "2021" | ||
license = "MIT" | ||
name = "getting-started" | ||
|
||
[[bin]] | ||
name = "routeguide-server" | ||
path = "src/server/server.rs" | ||
|
||
[[bin]] | ||
name = "routeguide-client" | ||
path = "src/client/client.rs" | ||
|
||
[features] | ||
routeguide = ["dep:async-stream", "dep:tokio-stream", "dep:rand", "dep:serde", "dep:serde_json"] | ||
full = ["routeguide"] | ||
default = ["full"] | ||
|
||
[dependencies] | ||
# Common dependencies | ||
tokio = { version = "1.0", features = ["rt-multi-thread", "macros"] } | ||
prost = "0.14" | ||
tonic = { git = "https://github.com/hyperium/tonic", branch="master"} | ||
tonic-protobuf = {git = "https://github.com/hyperium/tonic", branch="master", package = "tonic-protobuf" } | ||
grpc = {git = "https://github.com/hyperium/tonic", branch="master", package = "grpc"} | ||
# Optional dependencies | ||
async-stream = { version = "0.3", optional = true } | ||
tokio-stream = { version = "0.1", optional = true } | ||
tokio-util = { version = "0.7.8", optional = true } | ||
tower = { version = "0.5", optional = true } | ||
rand = { version = "0.9", optional = true } | ||
serde = { version = "1.0", features = ["derive"], optional = true } | ||
serde_json = { version = "1.0", optional = true } | ||
prost-types = { version = "0.14", optional = true } | ||
http = { version = "1", optional = true } | ||
hyper = { version = "1", optional = true } | ||
hyper-util = { version = "0.1.4", optional = true } | ||
tokio-rustls = { version = "0.26.1", optional = true, features = ["ring", "tls12"], default-features = false } | ||
hyper-rustls = { version = "0.27.0", features = ["http2", "ring", "tls12"], optional = true, default-features = false } | ||
tower-http = { version = "0.6", optional = true } | ||
protobuf = { version = "4.31.1-release"} | ||
|
||
[build-dependencies] | ||
protobuf-codegen = { version = "4.31.1-release"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn main() { | ||
protobuf_codegen::CodeGen::new() | ||
.include("proto") | ||
.inputs(["routeguide.proto"]) | ||
.output_dir("generated") | ||
.compile_only() | ||
.unwrap(); | ||
} |
Empty file.
5 changes: 5 additions & 0 deletions
5
codelabs/grpc-rust-getting-started/completed/generated/generated.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[path = "routeguide.u.pb.rs"] | ||
#[allow(nonstandard_style)] | ||
pub mod internal_do_not_use_routeguide; | ||
#[allow(unused_imports, nonstandard_style)] | ||
pub use internal_do_not_use_routeguide::*; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: We don't need to set
PLUGIN_PATH
, it's not used later.