From afcc1d7ade708ea6495f884f9ffa8c7ce7aa4adf Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Thu, 11 Aug 2022 05:34:46 +0000 Subject: [PATCH] implemented --- Cargo.toml | 4 +- src/main.rs | 11 +- src/member/mod.rs | 1 + src/member/service.rs | 381 ++++++++++++++++++++++++++++++++++++++++++ src/site/service.rs | 6 + 5 files changed, 399 insertions(+), 4 deletions(-) create mode 100644 src/member/mod.rs create mode 100644 src/member/service.rs diff --git a/Cargo.toml b/Cargo.toml index af6d519..87ededa 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.29-snapshot" } -beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.12-snapshot" } +beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.32-snapshot" } +beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.15-snapshot" } [build-dependencies] diff --git a/src/main.rs b/src/main.rs index 153c32a..8950b2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::env; mod identity; +mod member; mod site; #[tokio::main] @@ -17,13 +18,19 @@ async fn main() -> Result<(), Box> { let broker_opts = nats::asynk::Options::new(); let connection_broker = broker_opts.connect(url_broker).await?; + let site_service = site::service::Service::new(connection_broker.clone(), queue_broker.clone()); let identity_service = identity::service::Service::new(connection_broker.clone(), queue_broker.clone()); - let site_service = site::service::Service::new(connection_broker.clone(), queue_broker.clone()); + let member_service = + member::service::Service::new(connection_broker.clone(), queue_broker.clone()); println!("Server edge[beteran-backend-server-edge] is started"); - futures::try_join!(site_service.subscribe(), identity_service.subscribe())?; + futures::try_join!( + site_service.subscribe(), + identity_service.subscribe(), + member_service.subscribe() + )?; Ok(()) } diff --git a/src/member/mod.rs b/src/member/mod.rs new file mode 100644 index 0000000..1f278a4 --- /dev/null +++ b/src/member/mod.rs @@ -0,0 +1 @@ +pub mod service; diff --git a/src/member/service.rs b/src/member/service.rs new file mode 100644 index 0000000..99da44c --- /dev/null +++ b/src/member/service.rs @@ -0,0 +1,381 @@ +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::identity::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_members(), + self.get_member(), + self.get_member_by_username(), + ) + .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_members(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::member::SUBJECT_LIST_MEMBERS, + 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::backend::member::ListMembersRequest::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_members_req = bpr::ss::member::member::ListMembersRequest { + client: Some(client), + pagination: req.pagination, + searches: req.searches, + sorts: req.sorts, + site_id: req.site_id, + member_class_id: req.member_class_id, + member_level_id: req.member_level_id, + referrer_member_id: req.referrer_member_id, + username_like: req.username_like, + nickname_like: req.nickname_like, + mobile_phone_number_like: req.mobile_phone_number_like, + last_signined_ip: req.last_signined_ip, + state: req.state, + deleted_at: req.deleted_at, + }; + + let ss_list_members_res_msg = self + .connection_broker + .request( + bpr::ss::member::member::SUBJECT_LIST_MEMBERS, + ss_list_members_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_members_res = bpr::ss::member::member::ListMembersResponse::decode( + ss_list_members_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_members_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_members_res.result { + message + .respond( + bpr::c2se::backend::member::ListMembersResponse { + error: None, + result: Some(bpr::c2se::backend::member::list_members_response::Result { + members: r.members, + }), + } + .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::backend::member::ListMembersResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn get_member(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::member::SUBJECT_GET_MEMBER, + 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::backend::member::GetMemberRequest::decode(message.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { + message: format!("invalid request: {}", e), + }) + })?; + + let ss_get_member_req = bpr::ss::member::member::GetMemberRequest { + client: Some(client), + id: req.id, + }; + + let ss_get_member_res_msg = self + .connection_broker + .request( + bpr::ss::member::member::SUBJECT_GET_MEMBER, + ss_get_member_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_get_member_res = + bpr::ss::member::member::GetMemberResponse::decode(ss_get_member_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_get_member_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_get_member_res.result { + message + .respond( + bpr::c2se::backend::member::GetMemberResponse { + error: None, + result: Some(bpr::c2se::backend::member::get_member_response::Result { + member: r.member, + }), + } + .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::backend::member::GetMemberResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } + + async fn get_member_by_username(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::member::SUBJECT_GET_MEMBER_BY_USERNAME, + 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::backend::member::GetMemberByUsernameRequest::decode(message.data.as_slice()) + .map_err(|e| { + bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest { + message: format!("invalid request: {}", e), + }) + })?; + + let ss_get_member_by_username_req = bpr::ss::member::member::GetMemberByUsernameRequest { + client: Some(client), + username: req.username, + }; + + let ss_get_member_by_username_res_msg = self + .connection_broker + .request( + bpr::ss::member::member::SUBJECT_GET_MEMBER_BY_USERNAME, + ss_get_member_by_username_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_get_member_by_username_res = + bpr::ss::member::member::GetMemberByUsernameResponse::decode( + ss_get_member_by_username_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_get_member_by_username_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_get_member_by_username_res.result { + message + .respond( + bpr::c2se::backend::member::GetMemberByUsernameResponse { + error: None, + result: Some( + bpr::c2se::backend::member::get_member_by_username_response::Result { + member: r.member, + }, + ), + } + .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::backend::member::GetMemberByUsernameResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } +} diff --git a/src/site/service.rs b/src/site/service.rs index a8eb49a..76fb65d 100644 --- a/src/site/service.rs +++ b/src/site/service.rs @@ -93,6 +93,12 @@ impl Service { pagination: req.pagination, searches: req.searches, sorts: req.sorts, + url_like: req.url_like, + name_like: req.name_like, + path_like: req.path_like, + show: req.show, + can_use: req.can_use, + memo_like: req.memo_like, }; let ss_list_sites_res_msg = self