feat(refactor): update basic grpc method
This commit is contained in:
parent
3bc2286586
commit
f803aa92f7
6 changed files with 34 additions and 38 deletions
|
|
@ -9,8 +9,8 @@ use self::server::GRPCServer;
|
|||
mod client;
|
||||
mod server;
|
||||
|
||||
pub mod hello_world {
|
||||
tonic::include_proto!("helloworld");
|
||||
pub mod grpc_juno {
|
||||
tonic::include_proto!("juno");
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
use super::hello_world;
|
||||
use super::grpc_juno;
|
||||
|
||||
use hello_world::greater_client::GreaterClient;
|
||||
use hello_world::HelloRequest;
|
||||
use grpc_juno::juno_request_client::JunoRequestClient;
|
||||
use grpc_juno::PingRequestMessage;
|
||||
use tonic::async_trait;
|
||||
use tonic::Request;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct GRPCClient {
|
||||
|
|
@ -18,13 +19,11 @@ impl GRPCClient {
|
|||
#[async_trait]
|
||||
impl super::Connection for GRPCClient {
|
||||
async fn connect(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut client = GreaterClient::connect(format!("http://{}", self.address)).await?;
|
||||
let mut client = JunoRequestClient::connect(format!("http://{}", self.address)).await?;
|
||||
|
||||
let request = tonic::Request::new(HelloRequest {
|
||||
name: "Self".into(),
|
||||
});
|
||||
let request = Request::new(PingRequestMessage {});
|
||||
|
||||
let response = client.say_hello(request).await?;
|
||||
let response = client.ping(request).await?;
|
||||
|
||||
println!("RESPONSE={:?}", response);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use super::hello_world;
|
||||
use hello_world::greater_server::{Greater, GreaterServer};
|
||||
use hello_world::{HelloRequest, HelloResponse};
|
||||
use super::grpc_juno;
|
||||
use grpc_juno::juno_request_server::{JunoRequest, JunoRequestServer};
|
||||
use grpc_juno::{PingRequestMessage, PingResponseMessage};
|
||||
use std::error::Error;
|
||||
use std::net::SocketAddr;
|
||||
use tonic::transport::Server;
|
||||
|
|
@ -18,15 +18,13 @@ impl GRPCServer {
|
|||
}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl Greater for GRPCServer {
|
||||
async fn say_hello(
|
||||
impl JunoRequest for GRPCServer {
|
||||
async fn ping(
|
||||
&self,
|
||||
request: Request<HelloRequest>,
|
||||
) -> Result<Response<HelloResponse>, Status> {
|
||||
println!("Got a request {:?}", request);
|
||||
|
||||
let reply = hello_world::HelloResponse {
|
||||
message: format!("Hello {}!", request.into_inner().name),
|
||||
_request: Request<PingRequestMessage>,
|
||||
) -> Result<Response<PingResponseMessage>, Status> {
|
||||
let reply = PingResponseMessage {
|
||||
message: "pong!".to_string(),
|
||||
};
|
||||
|
||||
Ok(Response::new(reply))
|
||||
|
|
@ -41,7 +39,7 @@ impl super::Connection for GRPCServer {
|
|||
let socket: SocketAddr = self.address.parse()?;
|
||||
|
||||
Server::builder()
|
||||
.add_service(GreaterServer::new(GRPCServer::default()))
|
||||
.add_service(JunoRequestServer::new(GRPCServer::default()))
|
||||
.serve(socket)
|
||||
.await?;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue