feat: Implement gRPC Reflection Service (#340)
Co-authored-by: Samani G. Gikandi <[email protected]>
This commit is contained in:
co-authored by
Samani G. Gikandi
parent
f49d4bdf99
commit
c54f24721c
@@ -130,6 +130,10 @@ path = "src/hyper_warp/server.rs"
|
||||
name = "health-server"
|
||||
path = "src/health/server.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "reflection-server"
|
||||
path = "src/reflection/server.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "autoreload-server"
|
||||
path = "src/autoreload/server.rs"
|
||||
@@ -173,6 +177,8 @@ http-body = "0.4"
|
||||
pin-project = "1.0"
|
||||
# Health example
|
||||
tonic-health = { path = "../tonic-health" }
|
||||
# Reflection example
|
||||
tonic-reflection = { path = "../tonic-reflection" }
|
||||
listenfd = "0.3"
|
||||
|
||||
[build-dependencies]
|
||||
|
||||
@@ -94,6 +94,13 @@ $ cargo run --bin tls-server
|
||||
$ cargo run --bin health-server
|
||||
```
|
||||
|
||||
## Server Reflection
|
||||
|
||||
### Server
|
||||
```bash
|
||||
$ cargo run --bin reflection-server
|
||||
```
|
||||
|
||||
## Tower Middleware
|
||||
|
||||
### Server
|
||||
|
||||
+9
-1
@@ -1,10 +1,18 @@
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
tonic_build::configure()
|
||||
.type_attribute("routeguide.Point", "#[derive(Hash)]")
|
||||
.compile(&["proto/routeguide/route_guide.proto"], &["proto"])
|
||||
.unwrap();
|
||||
|
||||
tonic_build::compile_protos("proto/helloworld/helloworld.proto").unwrap();
|
||||
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
||||
tonic_build::configure()
|
||||
.file_descriptor_set_path(out_dir.join("helloworld_descriptor.bin"))
|
||||
.compile(&["proto/helloworld/helloworld.proto"], &["proto"])
|
||||
.unwrap();
|
||||
|
||||
tonic_build::compile_protos("proto/echo/echo.proto").unwrap();
|
||||
|
||||
tonic_build::configure()
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
use tonic::transport::Server;
|
||||
use tonic::{Request, Response, Status};
|
||||
|
||||
mod proto {
|
||||
tonic::include_proto!("helloworld");
|
||||
|
||||
pub(crate) const FILE_DESCRIPTOR_SET: &'static [u8] =
|
||||
tonic::include_file_descriptor_set!("helloworld_descriptor");
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MyGreeter {}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl proto::greeter_server::Greeter for MyGreeter {
|
||||
async fn say_hello(
|
||||
&self,
|
||||
request: Request<proto::HelloRequest>,
|
||||
) -> Result<Response<proto::HelloReply>, Status> {
|
||||
println!("Got a request from {:?}", request.remote_addr());
|
||||
|
||||
let reply = proto::HelloReply {
|
||||
message: format!("Hello {}!", request.into_inner().name),
|
||||
};
|
||||
Ok(Response::new(reply))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let service = tonic_reflection::server::Builder::configure()
|
||||
.register_encoded_file_descriptor_set(proto::FILE_DESCRIPTOR_SET)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let addr = "[::1]:50052".parse().unwrap();
|
||||
let greeter = MyGreeter::default();
|
||||
|
||||
Server::builder()
|
||||
.add_service(service)
|
||||
.add_service(proto::greeter_server::GreeterServer::new(greeter))
|
||||
.serve(addr)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user