-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new and attach_reflection method
- Loading branch information
1 parent
9b93067
commit 4a92bd7
Showing
1 changed file
with
27 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,33 @@ | ||
use crate::{{crate_name}}::{{"{"}}{{crate_name}}_service_server::{{project-name | pascal_case}}Service, {{project-name | pascal_case}}Request, {{project-name | pascal_case}}Response}; | ||
use crate::{{service_name}}::{{service_name}}_service_server::{{service_name | pascal_case}}ServiceServer; | ||
use crate::{{service_name}}::{{"{"}}{{service_name}}_service_server::{{service_name | pascal_case}}Service, {{service_name | pascal_case}}Request, {{service_name | pascal_case}}Response}; | ||
use std::error::Error; | ||
use tonic::{Request, Response, Status}; | ||
use tonic_reflection::server::{Builder, ServerReflection, ServerReflectionServer}; | ||
|
||
#[derive(Default)] | ||
pub struct {{project-name | pascal_case}}ServiceImpl {} | ||
const FILE_DESCRIPTOR_SET: &[u8] = include_bytes!("{{service_name}}_reflection_descriptor.bin"); | ||
|
||
#[tonic::async_trait] | ||
impl {{project-name | pascal_case}}Service for {{project-name | pascal_case}}ServiceImpl { | ||
#[derive(Default)] | ||
pub struct {{service_name | pascal_case}}ServiceImpl {} | ||
|
||
async fn {{rpc_method}} (&self, request: Request<{{project-name | pascal_case}}Request>) -> Result<Response<{{project-name | pascal_case}}Response>, Status> { | ||
let name = request.into_inner().name; | ||
let reply = {{project-name | pascal_case}}Response { | ||
message: format!("Hello {}!", name), | ||
}; | ||
Ok(Response::new(reply)) | ||
impl {{service_name | pascal_case}}ServiceImpl { | ||
pub fn new() -> {{service_name | pascal_case}}ServiceServer<{{service_name | pascal_case}}ServiceImpl> { | ||
{{service_name | pascal_case}}ServiceServer::new({{service_name | pascal_case}}ServiceImpl {}) | ||
} | ||
pub fn attach_reflection() -> Result<ServerReflectionServer<impl ServerReflection + 'static>, Box<dyn Error>> { | ||
Builder::configure() | ||
.register_encoded_file_descriptor_set(FILE_DESCRIPTOR_SET) | ||
.build() | ||
.map_err(|e| Box::new(e) as Box<dyn Error>) | ||
} | ||
} | ||
|
||
#[tonic::async_trait] | ||
impl {{service_name | pascal_case}}Service for {{service_name | pascal_case}}ServiceImpl { | ||
async fn {{rpc_method}}(&self, request: Request<{{service_name | pascal_case}}Request>) -> Result<Response<{{service_name | pascal_case}}Response>, Status> { | ||
let name = request.into_inner().name; | ||
let reply = {{service_name | pascal_case}}Response { | ||
message: format!("Hello {}!", name), | ||
}; | ||
Ok(Response::new(reply)) | ||
} | ||
} |