Skip to content

Commit 4baf5ea

Browse files
committed
add support for reflection
1 parent 456ab65 commit 4baf5ea

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ This repository contains a gRPC-based Rust server implementing CRUD operations f
1313
## Prerequisites
1414

1515
Before you begin, ensure you have installed:
16+
1617
- [Rust](https://www.rust-lang.org/tools/install)
18+
1719
## Installation
1820

1921
## Running the Server
@@ -63,10 +65,18 @@ message NewsList {
6365
}
6466
```
6567

68+
## Reflection api
69+
70+
The server supports reflection api by default
71+
72+
### example
73+
74+
`grpcurl -plaintext localhost:50051 list`
75+
6676
## License
6777

6878
This project is licensed under the MIT License.
6979

70-
* * *
80+
---
7181

7282
© 2024 @ Tailcall

build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ fn main() {
44
let mut news = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
55
news.push("news.proto");
66
tonic_build::compile_protos(news).expect("Failed to compile protos");
7+
8+
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
9+
10+
tonic_build::configure()
11+
.file_descriptor_set_path(out_dir.join("news_descriptor.bin"))
12+
.compile(&["news.proto"], &["proto"])
13+
.unwrap();
714
}

src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ use news::news_service_server::NewsService;
66
use news::{MultipleNewsId, News, NewsId, NewsList};
77
use std::sync::{Arc, Mutex};
88
use tower::make::Shared;
9+
910
pub mod news {
1011
tonic::include_proto!("news"); // The package name specified in your .proto
12+
pub(crate) const FILE_DESCRIPTOR_SET: &[u8] =
13+
tonic::include_file_descriptor_set!("news_descriptor");
1114
}
1215

1316
#[derive(Debug, Default)]
@@ -149,11 +152,16 @@ async fn main() -> Result<()> {
149152
let addr = ([127, 0, 0, 1], 50051).into();
150153

151154
let news_service = MyNewsService::new();
155+
let service = tonic_reflection::server::Builder::configure()
156+
.register_encoded_file_descriptor_set(news::FILE_DESCRIPTOR_SET)
157+
.build()
158+
.unwrap();
152159

153160
println!("NewsService server listening on {}", addr);
154161

155162
let tonic_service = TonicServer::builder()
156163
.add_service(NewsServiceServer::new(news_service))
164+
.add_service(service)
157165
.into_service();
158166
let make_svc = Shared::new(tonic_service);
159167
println!("Server listening on http://{}", addr);

0 commit comments

Comments
 (0)