feat(refactor): update basic grpc method

This commit is contained in:
Alexander Navarro 2024-04-24 10:26:14 -04:00
parent 3bc2286586
commit f803aa92f7
6 changed files with 34 additions and 38 deletions

View file

@ -1,5 +1,5 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::compile_protos("proto/helloworld.proto")?;
tonic_build::compile_protos("proto/juno.proto")?;
Ok(())
}

View file

@ -1,15 +0,0 @@
syntax = "proto3";
package helloworld;
service Greater {
rpc SayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string message = 1;
}

14
proto/juno.proto Normal file
View file

@ -0,0 +1,14 @@
syntax = "proto3";
package juno;
service JunoRequest {
rpc Ping (PingRequestMessage) returns (PingResponseMessage);
}
message PingRequestMessage {
}
message PingResponseMessage {
string message = 1;
}

View file

@ -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]

View file

@ -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);

View file

@ -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?;