From 268b7783db6387667030731b14ef676e2a58aff0 Mon Sep 17 00:00:00 2001 From: plazmoid Date: Wed, 2 Sep 2020 03:16:02 +0500 Subject: [PATCH] all working fuck yey --- bin/u_agent/src/main.rs | 8 +- bin/u_panel/src/main.rs | 2 +- bin/u_server/src/main.rs | 2 +- lib/u_lib/src/client/network.rs | 137 ++++++++++++++------------------ lib/u_lib/src/contracts/jobs.rs | 2 +- lib/u_lib/src/errors.rs | 20 ++--- 6 files changed, 73 insertions(+), 98 deletions(-) diff --git a/bin/u_agent/src/main.rs b/bin/u_agent/src/main.rs index 675de6d..c7fad73 100644 --- a/bin/u_agent/src/main.rs +++ b/bin/u_agent/src/main.rs @@ -11,7 +11,10 @@ use { std::thread::sleep, std::time::Duration, std::env, - u_lib::client::network::ClientHandler + u_lib::client::{ + ClientInfo, + network::ClientHandler + } }; #[tokio::main] @@ -19,7 +22,8 @@ async fn main() { //daemonize(); let arg_ip = env::args().nth(1); let instance = ClientHandler::new(arg_ip); - instance.init().await; + let cli_info = ClientInfo::gather(); + instance.init(&cli_info).await; loop { sleep(Duration::from_secs(2)); } diff --git a/bin/u_panel/src/main.rs b/bin/u_panel/src/main.rs index 6d318a3..3001a8c 100644 --- a/bin/u_panel/src/main.rs +++ b/bin/u_panel/src/main.rs @@ -16,7 +16,7 @@ async fn main() -> Result<(), &'static str> { .password("123qwe".to_string()); match method.as_str() { "ls" => { - let result = cli_handler.list().await; + let result = cli_handler.ls().await; for cli in result.iter() { println!("{:#?}", cli) } diff --git a/bin/u_server/src/main.rs b/bin/u_server/src/main.rs index a14710b..7e9581b 100644 --- a/bin/u_server/src/main.rs +++ b/bin/u_server/src/main.rs @@ -26,7 +26,7 @@ async fn main() { let db = warp::any().map(move || base_db.clone()); let new_client = warp::post() - .and(warp::path("new")) + .and(warp::path("init")) .and(get_content()) .and(db.clone()) .and_then(handlers::add_client); diff --git a/lib/u_lib/src/client/network.rs b/lib/u_lib/src/client/network.rs index 78a037d..ffd62df 100644 --- a/lib/u_lib/src/client/network.rs +++ b/lib/u_lib/src/client/network.rs @@ -16,85 +16,6 @@ use std::{ str::FromStr }; -pub struct Paths; - -impl Paths { - pub const LIST: &'static str = "list"; - pub const NEW: &'static str = "new"; - //pub const DEL: &'static str = "del"; - pub const JOB_REQ: &'static str = "job_req"; - pub const JOB_ANS: &'static str = "job_ans"; -} - - -pub struct ClientHandler { - base_url: Url, - client: Client, - password: Option -} - -impl ClientHandler { - pub fn new(server: Option) -> Self { - let master_server = server - .map(|s| Ipv4Addr::from_str(&s).unwrap()) - .unwrap_or(MASTER_SERVER); - Self { - client: Client::new(), - base_url: Url::parse( - &format!("http://{}:{}", master_server, MASTER_PORT) - ).unwrap(), - password: None - } - } - - pub fn password(mut self, password: String) -> ClientHandler { - self.password = Some(password); - self - } - - fn set_pwd(&self, rb: RequestBuilder) -> RequestBuilder { - match &self.password { - Some(p) => rb.bearer_auth(p), - None => rb - } - } - - fn build_get(&self, url: &str) -> RequestBuilder { - let rb = self.client.get(self.base_url.join(url).unwrap()); - self.set_pwd(rb) - } - - fn build_post(&self, url: &str) -> RequestBuilder { - let rb = self.client.post(self.base_url.join(url).unwrap()); - self.set_pwd(rb) - } - - pub async fn init(&self, param: &ClientInfo) -> UResult { - let response: Response = self.build_post(Paths::NEW) - .json::>(¶m.as_message()) - .send() - .await?; - let msg = response - .json::>() - .await?; - Ok(msg.into_item().into_owned()) - } - - pub async fn list(&self) -> UResult> { - let response: Response = self.build_get(Paths::LIST) - .send() - .await?; - let msg = response - .json::>>() - .await?; - Ok(msg.into_item().into_owned()) - } -/* - pub async fn del(&self) -> UResult<()> { - self.build_post(Paths::DEL).send().await?; - Ok(()) - }*/ -} #[macro_export] macro_rules! epilogue { @@ -111,6 +32,7 @@ macro_rules! epilogue { } // build_handler![path = new, method = "post", param = ClientInfo, result = RawMsg] +// new syntax: build_handler!(POST new(ClientInfo) -> RawMsg) // param and result must impl ToMsg #[macro_export] macro_rules! build_handler { @@ -164,4 +86,61 @@ macro_rules! build_handler { ); } +pub struct Paths; + +impl Paths { + pub const LIST: &'static str = "list"; + pub const NEW: &'static str = "new"; + //pub const DEL: &'static str = "del"; + pub const JOB_REQ: &'static str = "job_req"; + pub const JOB_ANS: &'static str = "job_ans"; +} + + +pub struct ClientHandler { + base_url: Url, + client: Client, + password: Option +} + +impl ClientHandler { + pub fn new(server: Option) -> Self { + let master_server = server + .map(|s| Ipv4Addr::from_str(&s).unwrap()) + .unwrap_or(MASTER_SERVER); + Self { + client: Client::new(), + base_url: Url::parse( + &format!("http://{}:{}", master_server, MASTER_PORT) + ).unwrap(), + password: None + } + } + + pub fn password(mut self, password: String) -> ClientHandler { + self.password = Some(password); + self + } + + fn set_pwd(&self, rb: RequestBuilder) -> RequestBuilder { + match &self.password { + Some(p) => rb.bearer_auth(p), + None => rb + } + } + + fn build_get(&self, url: &str) -> RequestBuilder { + let rb = self.client.get(self.base_url.join(url).unwrap()); + self.set_pwd(rb) + } + + fn build_post(&self, url: &str) -> RequestBuilder { + let rb = self.client.post(self.base_url.join(url).unwrap()); + self.set_pwd(rb) + } +} + + +build_handler!(path = init, method = "post", param = ClientInfo, result = RawMsg); +build_handler!(path = ls, method = "get", result = Vec); build_handler!(path = del, method = "post"); diff --git a/lib/u_lib/src/contracts/jobs.rs b/lib/u_lib/src/contracts/jobs.rs index ec5f5a8..d05dd11 100644 --- a/lib/u_lib/src/contracts/jobs.rs +++ b/lib/u_lib/src/contracts/jobs.rs @@ -209,7 +209,7 @@ impl<'meta> Job<'meta> { } Err(e) => { self.result.data = Err( - UError::new(UErrType::JobError, Some(e)) + UError::new(UErrType::JobError, e.to_string()) ); self.result.retcode = None; } diff --git a/lib/u_lib/src/errors.rs b/lib/u_lib/src/errors.rs index 8d4a65c..e388f58 100644 --- a/lib/u_lib/src/errors.rs +++ b/lib/u_lib/src/errors.rs @@ -21,7 +21,7 @@ pub enum UErrType { #[derive(Serialize, Deserialize, Clone, Debug)] struct Inner { err_type: UErrType, - source: Option + source: String, } #[derive(Serialize, Deserialize, Clone)] @@ -30,10 +30,10 @@ pub struct UError { } impl UError { - pub fn new>(err_type: UErrType, source: Option) -> Self { + pub fn new(err_type: UErrType, source: String) -> Self { Self { inner: Box::new(Inner { - source: source.map(Into::into), + source, err_type }) } @@ -45,11 +45,7 @@ impl fmt::Debug for UError { let mut builder = f.debug_struct("errors::UError"); builder.field("kind", &self.inner.err_type); - - if let Some(ref source) = self.inner.source { - builder.field("source", source); - } - + builder.field("source", &self.inner.source); builder.finish() } } @@ -65,11 +61,7 @@ impl fmt::Display for UError { }; f.write_str(e_type)?; - if let Some(ref e) = self.inner.source { - write!(f, ": {}", e) - } else { - Ok(()) - } + write!(f, ": {}", self.inner.source) } } @@ -82,6 +74,6 @@ impl From for UError { } else { UErrType::Unknown }; - UError::new(err_type, Some(e)) + UError::new(err_type, e.to_string()) } } \ No newline at end of file