Skip to content

Add completed Rust Codelab #48

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2,355 changes: 2,355 additions & 0 deletions codelabs/grpc-rust-getting-started/completed/Cargo.lock

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions codelabs/grpc-rust-getting-started/completed/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[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/arjan-bal/tonic.git", branch = "grpc-codegen" }
tonic-protobuf = {git = "https://github.com/arjan-bal/tonic.git", branch = "grpc-codegen", package = "tonic-protobuf" }
grpc = {git = "https://github.com/arjan-bal/tonic.git", branch = "grpc-codegen", 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]
tonic-protobuf-build = {git = "https://github.com/arjan-bal/tonic.git", branch = "grpc-codegen", package = "tonic-protobuf-build" }
tonic-build = {git = "https://github.com/arjan-bal/tonic.git", branch = "grpc-codegen", package = "tonic-build" }

12 changes: 12 additions & 0 deletions codelabs/grpc-rust-getting-started/completed/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fn main() {
let proto = "src/routeguide/routeguide.proto";

tonic_build::compile_protos(proto).unwrap();
tonic_protobuf_build::CodeGen::new()
.include("src/routeguide")
.inputs(["routeguide.proto"])
.generate_and_compile()
.unwrap();
}


37 changes: 37 additions & 0 deletions codelabs/grpc-rust-getting-started/completed/src/client/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use tonic::Request;
use tonic::transport::{Channel, Endpoint};

pub mod route_guide_gen {
grpc::include_proto!("", "routeguide");
}

use route_guide_gen::{
route_guide_client::RouteGuideClient,
Point,
};

#[derive(Debug, Clone)]
pub struct RouteGuideAppClient {
inner: RouteGuideClient<Channel>,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
//Create endpoint to connect to
let endpoint = Endpoint::new("http://[::1]:10000")?;
let channel = endpoint.connect().await?;

// Create a new client
let mut client = RouteGuideClient::new(channel);

println!("*** SIMPLE RPC ***");
let mut point = Point::new();
point.set_latitude(409_146_138);
point.set_longitude(-746_188_906);
let response = client
.get_feature(Request::new(point))
.await?;

println!("RESPONSE = {response:?}");
Ok(())
}
Loading