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