No tarball for gmail
This commit is contained in:
84
src/client.rs
Normal file
84
src/client.rs
Normal file
@@ -0,0 +1,84 @@
|
||||
use clap::Parser;
|
||||
use pubsub_client::pub_sub_client::PubSubClient;
|
||||
use pubsub_client::PublishMessage;
|
||||
use pubsub_client::Consumer;
|
||||
use std::fmt;
|
||||
use std::time::Duration;
|
||||
|
||||
pub mod pubsub_client {
|
||||
tonic::include_proto!("pubsub");
|
||||
}
|
||||
|
||||
#[derive(clap::ValueEnum, Clone, Debug)]
|
||||
enum Protocol {
|
||||
Http,
|
||||
Tcp,
|
||||
}
|
||||
|
||||
#[derive(clap::Subcommand, Debug)]
|
||||
enum Command {
|
||||
Publish { topic: String, message: String },
|
||||
Consume {
|
||||
topic: String,
|
||||
#[clap(short, long, default_value_t = 10)]
|
||||
timeout: u64},
|
||||
}
|
||||
|
||||
impl fmt::Display for Protocol {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Protocol::Http => write!(f, "http"),
|
||||
Protocol::Tcp => write!(f, "tcp"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(author, version, about, long_about = None)]
|
||||
struct Cli {
|
||||
#[clap(short, long, value_parser, default_value_t = 50051)]
|
||||
port: u16,
|
||||
|
||||
#[clap(long, value_enum, default_value_t = Protocol::Tcp)]
|
||||
protocol: Protocol,
|
||||
|
||||
#[clap(short, long, value_parser, default_value = "localhost")]
|
||||
endpoint: String,
|
||||
|
||||
#[clap(subcommand)]
|
||||
command: Command,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = Cli::parse();
|
||||
|
||||
let mut client = PubSubClient::connect(format!(
|
||||
"{}://{}:{}",
|
||||
args.protocol, args.endpoint, args.port
|
||||
))
|
||||
.await?;
|
||||
|
||||
match args.command {
|
||||
Command::Consume { topic, timeout } => {
|
||||
let mut request = tonic::Request::new(Consumer { topic: topic.clone(), timeout_ms: timeout * 1000 });
|
||||
//Add 1s to the client-side timeout to let
|
||||
request.set_timeout(Duration::from_secs(timeout + 1));
|
||||
match client.consume(request).await {
|
||||
Err(e) => eprintln!("Error consuming {}\n{}", topic, e),
|
||||
Ok(msg) => println!("{}", msg.get_ref().message)
|
||||
}
|
||||
}
|
||||
Command::Publish { topic, message } => {
|
||||
let request = tonic::Request::new(PublishMessage {
|
||||
topic: topic.clone(),
|
||||
message: message.clone(),
|
||||
});
|
||||
if let Err(e) = client.publish(request).await {
|
||||
eprintln!("Error publishing {}:{}\n{}", topic, message, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user