add reflection

This commit is contained in:
Peter Zhang 2025-07-01 19:23:55 +08:00
parent a4b0bc1dac
commit cb18ffd6c3
5 changed files with 30 additions and 1 deletions

2
.gitignore vendored
View File

@ -7,3 +7,5 @@ tests/tmp/**
.vscode/*.json .vscode/*.json
/0g-storage-contracts-dev /0g-storage-contracts-dev
/run/.env /run/.env
**.bin

14
Cargo.lock generated
View File

@ -7015,6 +7015,7 @@ dependencies = [
"tokio", "tokio",
"tonic 0.9.2", "tonic 0.9.2",
"tonic-build", "tonic-build",
"tonic-reflection",
"tracing", "tracing",
] ]
@ -8385,6 +8386,19 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "tonic-reflection"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0543d7092032041fbeac1f2c84304537553421a11a623c2301b12ef0264862c7"
dependencies = [
"prost 0.11.9",
"prost-types 0.11.9",
"tokio",
"tokio-stream",
"tonic 0.9.2",
]
[[package]] [[package]]
name = "tower" name = "tower"
version = "0.4.13" version = "0.4.13"

View File

@ -32,6 +32,7 @@ parking_lot = "0.12.3"
tonic = { version = "0.9.2", features = ["transport"] } tonic = { version = "0.9.2", features = ["transport"] }
prost = "0.11.9" prost = "0.11.9"
prost-types = "0.11.9" prost-types = "0.11.9"
tonic-reflection = "0.9.2"
[build-dependencies] [build-dependencies]
tonic-build = "0.9.2" tonic-build = "0.9.2"

View File

@ -1,5 +1,7 @@
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
// Compile proto/my_service.proto // Compile proto/my_service.proto
tonic_build::compile_protos("proto/zgs_grpc.proto")?; tonic_build::configure()
.file_descriptor_set_path("proto/zgs_grpc_descriptor.bin")
.compile(&["proto/zgs_grpc.proto"], &["proto"])?;
Ok(()) Ok(())
} }

View File

@ -34,6 +34,9 @@ pub use admin::RpcClient as ZgsAdminRpcClient;
pub use config::Config as RPCConfig; pub use config::Config as RPCConfig;
pub use miner::RpcClient as ZgsMinerRpcClient; pub use miner::RpcClient as ZgsMinerRpcClient;
pub use zgs::RpcClient as ZgsRPCClient; pub use zgs::RpcClient as ZgsRPCClient;
// bring in the reflection-builder
use tonic_reflection::server::Builder as ReflectionBuilder;
pub mod zgs_grpc_proto { pub mod zgs_grpc_proto {
tonic::include_proto!("zgs_grpc"); tonic::include_proto!("zgs_grpc");
@ -43,6 +46,8 @@ mod zgs_grpc;
use tonic::transport::Server; use tonic::transport::Server;
const DESCRIPTOR_SET: &[u8] = include_bytes!("../proto/zgs_grpc_descriptor.bin");
/// A wrapper around all the items required to spawn the HTTP server. /// A wrapper around all the items required to spawn the HTTP server.
/// ///
/// The server will gracefully handle the case where any fields are `None`. /// The server will gracefully handle the case where any fields are `None`.
@ -146,9 +151,14 @@ async fn run_server_public_private(
pub async fn run_grpc_server(ctx: Context) -> Result<(), Box<dyn Error>> { pub async fn run_grpc_server(ctx: Context) -> Result<(), Box<dyn Error>> {
let grpc_addr = ctx.config.listen_address_grpc; let grpc_addr = ctx.config.listen_address_grpc;
let reflection = ReflectionBuilder::configure()
.register_encoded_file_descriptor_set(DESCRIPTOR_SET)
.build()?;
let server = ZgsGrpcServiceServer::new(ZgsGrpcServiceImpl { ctx }); let server = ZgsGrpcServiceServer::new(ZgsGrpcServiceImpl { ctx });
Server::builder() Server::builder()
.add_service(server) .add_service(server)
.add_service(reflection)
.serve(grpc_addr) .serve(grpc_addr)
.await?; .await?;
Ok(()) Ok(())