Skip to content

Commit

Permalink
reflection: add with_service_name method (#1042)
Browse files Browse the repository at this point in the history
* reflection: add with_service_name method

Fixes: #682

* Update tonic-reflection/src/server.rs

Co-authored-by: Lucio Franco <luciofranco14@gmail.com>
  • Loading branch information
zombiezen and LucioFranco committed Jul 29, 2022
1 parent e40ae11 commit 4288fd3
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion tonic-reflection/src/server.rs
Expand Up @@ -53,6 +53,7 @@ pub struct Builder<'b> {
include_reflection_service: bool,

service_names: Vec<String>,
use_all_service_names: bool,
symbols: HashMap<String, Arc<FileDescriptorProto>>,
}

Expand All @@ -65,6 +66,7 @@ impl<'b> Builder<'b> {
include_reflection_service: true,

service_names: Vec::new(),
use_all_service_names: true,
symbols: HashMap::new(),
}
}
Expand Down Expand Up @@ -94,6 +96,16 @@ impl<'b> Builder<'b> {
self
}

/// Advertise a fully-qualified gRPC service name.
///
/// If not called, then all services present in the registered file descriptor sets
/// will be advertised.
pub fn with_service_name(mut self, name: impl Into<String>) -> Self {
self.use_all_service_names = false;
self.service_names.push(name.into());
self
}

/// Build a gRPC Reflection Service to be served via Tonic.
pub fn build(mut self) -> Result<ServerReflectionServer<impl ServerReflection>, Error> {
if self.include_reflection_service {
Expand Down Expand Up @@ -156,7 +168,9 @@ impl<'b> Builder<'b> {

for service in &fd.service {
let service_name = extract_name(prefix, "service", service.name.as_ref())?;
self.service_names.push(service_name.clone());
if self.use_all_service_names {
self.service_names.push(service_name.clone());
}
self.symbols.insert(service_name.clone(), fd.clone());

for method in &service.method {
Expand Down

0 comments on commit 4288fd3

Please sign in to comment.