From 6f1f9279153a2a2304f5dda66b52c57ee2dc5817 Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Fri, 19 Apr 2024 14:13:17 +0200 Subject: [PATCH] sender: receiver: http_server: clean up the code --- src/args.rs | 17 ++++++++++++----- src/http_server.rs | 18 +++++++++--------- src/receiver.rs | 9 --------- src/sender.rs | 24 +++++++++--------------- 4 files changed, 30 insertions(+), 38 deletions(-) diff --git a/src/args.rs b/src/args.rs index 12b566f..7dd6710 100644 --- a/src/args.rs +++ b/src/args.rs @@ -2,15 +2,16 @@ use crate::http_server; use crate::receiver; use crate::sender; use clap::{Parser, Subcommand}; +use log::debug; -#[derive(Parser)] +#[derive(Parser, Debug)] #[command(version, about, long_about = None)] pub struct Args { #[command(subcommand)] pub command: Option, } -#[derive(Subcommand)] +#[derive(Subcommand, Debug)] pub enum Commands { /// Send files to the receiver or relay server Send { @@ -18,7 +19,7 @@ pub enum Commands { #[arg(short, long)] relay: Option, /// Path to file(s) - #[arg(short, long, value_name = "FILE")] + #[arg(value_name = "FILE")] file: Option, }, /// Receives Files from the sender with the matching password @@ -75,9 +76,15 @@ impl Args { Self::parse() } pub async fn run(&self) -> Result<(), Box> { + env_logger::init(); + debug!("args: {:?}", self); match &self.command { - Some(Commands::Send { relay: _, file }) => { - sender::send_info(file.as_deref().unwrap_or("test.txt")).await?; + Some(Commands::Send { relay, file }) => { + sender::send_info( + relay.as_deref().unwrap_or("http://192.168.178.43:1323"), + file.as_deref().unwrap_or("test.txt"), + ) + .await?; } Some(Commands::Receive { relay: _, diff --git a/src/http_server.rs b/src/http_server.rs index e4f390e..7b5b896 100644 --- a/src/http_server.rs +++ b/src/http_server.rs @@ -18,20 +18,20 @@ use tokio::signal; use tracing::{debug, error, info, trace, warn}; #[derive(Debug, Serialize, Deserialize, Clone)] -struct TransferRequest { - ip: String, - name: String, - body: TransferBody, +pub struct TransferRequest { + pub ip: String, + pub name: String, + pub body: TransferBody, } #[derive(Debug, Serialize, Deserialize, Clone)] -struct TransferBody { - keyword: String, - files: String, +pub struct TransferBody { + pub keyword: String, + pub files: String, } impl TransferRequest { - fn new() -> Self { + pub fn new() -> Self { Self { ip: "".to_string(), name: "".to_string(), @@ -49,7 +49,7 @@ struct AppState { } pub async fn start_server(port: Option<&i32>, listen_addr: Option<&String>) { - env_logger::init(); + // env_logger::init(); info!("Server starting..."); let shared_state = AppState { data: Arc::new(Mutex::new(Vec::new())), diff --git a/src/receiver.rs b/src/receiver.rs index 43580db..c8db741 100644 --- a/src/receiver.rs +++ b/src/receiver.rs @@ -1,7 +1,4 @@ use serde::{Deserialize, Serialize}; -// use std::collections::HashMap; - -// use crate::http_client; type Result = std::result::Result>; @@ -27,11 +24,5 @@ pub async fn download_info(filename: &str) -> Result<()> { println!("Error: {err}"); } } - // http_client::send_request( - // format!("http://192.168.178.43:1323/download/{}", filename).trim(), - // "GET", - // None, - // ) - // .await?; Ok(()) } diff --git a/src/sender.rs b/src/sender.rs index b2842c6..5e138c1 100644 --- a/src/sender.rs +++ b/src/sender.rs @@ -1,35 +1,29 @@ -// use crate::http_client; +use crate::http_server::TransferRequest; +use log::{debug, error}; use reqwest::{Client, StatusCode}; -use serde_json::Value; use std::collections::HashMap; type Result = std::result::Result>; -pub async fn send_info(file: &str) -> Result<()> { +pub async fn send_info(relay: &str, file: &str) -> Result<()> { + debug!("Send Request to: {:?}", relay.to_string()); let mut map = HashMap::new(); map.insert("keyword", "test"); map.insert("files", file); - // let json_data = serde_json::to_string(&map)?; - let client = Client::new(); let res = client - .post("http://192.168.178.43:1323/upload") + .post(relay.to_string() + "/upload") .json(&map) .send() .await?; if res.status() == StatusCode::CREATED { - let json: Value = res.json().await?; - println!("Json Response: {:#?}", json); + let transfer_info: TransferRequest = res.json().await?; + debug!("Json Response: {:#?}", transfer_info); + println!("Transfer name: {}", transfer_info.name); } else { - println!("Error reading response"); + error!("Error reading response"); } - // http_client::send_request( - // "http://192.168.178.43:1323/upload".trim(), - // "POST", - // Some(json_data), - // ) - // .await?; Ok(()) }