From 6b167b37d85f78611409c5f0963cb7803933b577 Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Thu, 18 Aug 2022 07:04:33 +0000 Subject: [PATCH] list_vendors is added --- Cargo.toml | 4 +- src/api/mod.rs | 1 + src/api/service.rs | 179 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 5 ++ 4 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 src/api/mod.rs create mode 100644 src/api/service.rs diff --git a/Cargo.toml b/Cargo.toml index 2e6dfe6..3d534fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ prost = { version = "0" } nats = { version = "0" } base64 = { version = "0" } -beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.63-snapshot" } -beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.47-snapshot" } +beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.64-snapshot" } +beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.48-snapshot" } [build-dependencies] diff --git a/src/api/mod.rs b/src/api/mod.rs new file mode 100644 index 0000000..1f278a4 --- /dev/null +++ b/src/api/mod.rs @@ -0,0 +1 @@ +pub mod service; diff --git a/src/api/service.rs b/src/api/service.rs new file mode 100644 index 0000000..8e4f4d3 --- /dev/null +++ b/src/api/service.rs @@ -0,0 +1,179 @@ +use beteran_common_rust as bcr; +use beteran_protobuf_rust as bpr; +use prost::Message; + +/// +pub struct Service { + connection_broker: nats::asynk::Connection, + queue_broker: String, +} + +impl std::fmt::Debug for Service { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("beteran-backend-server-edge::vendor::service::Service") + .finish() + } +} + +impl Service { + /// + pub fn new(connection_broker: nats::asynk::Connection, queue_broker: String) -> Service { + Service { + connection_broker, + queue_broker, + } + } + + pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box> { + futures::try_join!(self.list_vendors(),).map(|_| ()) + } + + fn get_client_in_header( + &self, + message: &nats::asynk::Message, + ) -> Result { + match &message.headers { + Some(headers) => { + let client = match headers.get(bpr::c2se::core::network::HEADER_CLIENT) { + Some(c) => { + let msg = base64::decode(c).map_err(|e| { + bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse { + message: format!("invalid header: {}", e), + }) + })?; + bpr::models::core::network::Client::decode(msg.as_slice()).map_err(|e| { + bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse { + message: format!("invalid header: {}", e), + }) + })? + } + None => { + return Err(bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse { + message: "invalid client information".to_string(), + })); + } + }; + + Ok(client) + } + None => Err(bcr::error::rpc::Error::Parse(bcr::error::rpc::Parse { + message: "invalid header".to_string(), + })), + } + } + + async fn list_vendors(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::api::vendor::SUBJECT_LIST_VENDORS, + self.queue_broker.as_str(), + ) + .await?; + + while let Some(message) = s.next().await { + if let Err(e) = async { + let client = self.get_client_in_header(&message)?; + + let req = bpr::c2se::api::vendor::ListVendorsRequest::decode(message.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { + message: format!("invalid request: {}", e), + }) + })?; + + let ss_list_vendors_req = bpr::ss::api::vendor::ListVendorsRequest { + client: Some(client), + request: Some(bpr::ss::api::vendor::list_vendors_request::Request { + pagination: req.pagination, + sorts: req.sorts, + search: match req.search { + Some(s) => Some( + bpr::ss::api::vendor::list_vendors_request::request::Search { + name_like: s.name_like, + company_id: s.company_id, + vendor_id: s.vendor_id, + key_like: s.key_like, + category_like: s.category_like, + is_enable: s.is_enable, + }, + ), + None => None, + }, + }), + }; + + let ss_list_vendors_res_msg = self + .connection_broker + .request( + bpr::ss::api::vendor::SUBJECT_LIST_VENDORS, + ss_list_vendors_req.encode_to_vec(), + ) + .await + .map_err(|e| { + bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + }) + })?; + + let ss_list_vendors_res = bpr::ss::api::vendor::ListVendorsResponse::decode( + ss_list_vendors_res_msg.data.as_slice(), + ) + .map_err(|e| { + bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + }) + })?; + + if let Some(e) = ss_list_vendors_res.error { + return Err(bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + })); + } + + if let Some(r) = ss_list_vendors_res.result { + message + .respond( + bpr::c2se::api::vendor::ListVendorsResponse { + error: None, + result: Some(bpr::c2se::api::vendor::list_vendors_response::Result { + vendors: r.vendors, + }), + } + .encode_to_vec(), + ) + .await + .map_err(|e| { + bcr::error::rpc::Error::Server(bcr::error::rpc::Server { + code: bpr::protobuf::rpc::Error::SERVER_00, + message: format!("server {}", e), + data: None, + }) + })?; + } + + Ok::<(), bcr::error::rpc::Error>(()) + } + .await + { + message + .respond( + bpr::c2se::api::vendor::ListVendorsResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index e76ecf2..70a3274 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,8 @@ mod member_class; mod member_level; mod site; +mod api; + #[tokio::main] async fn main() -> Result<(), Box> { let url_broker = match env::var_os("URL_BROKER") { @@ -41,6 +43,8 @@ async fn main() -> Result<(), Box> { member_level::service::Service::new(connection_broker.clone(), queue_broker.clone()); let site_service = site::service::Service::new(connection_broker.clone(), queue_broker.clone()); + let api_service = api::service::Service::new(connection_broker.clone(), queue_broker.clone()); + println!("Server edge[beteran-backend-server-edge] is started"); futures::try_join!( @@ -53,6 +57,7 @@ async fn main() -> Result<(), Box> { member_class_service.subscribe(), member_level_service.subscribe(), site_service.subscribe(), + api_service.subscribe(), )?; Ok(())