diff --git a/Cargo.toml b/Cargo.toml index 93f9b65..d97f903 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.67-snapshot" } -beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.51-snapshot" } +beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.68-snapshot" } +beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.52-snapshot" } [build-dependencies] diff --git a/src/main.rs b/src/main.rs index 5b41c90..f298c1b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::env; mod api; mod bank; mod identity; +mod member_referrer; #[tokio::main] async fn main() -> Result<(), Box> { @@ -21,6 +22,8 @@ async fn main() -> Result<(), Box> { let bank_service = bank::service::Service::new(connection_broker.clone(), queue_broker.clone()); let identity_service = identity::service::Service::new(connection_broker.clone(), queue_broker.clone()); + let member_referrer_service = + member_referrer::service::Service::new(connection_broker.clone(), queue_broker.clone()); let api_vendor_service = api::vendor::service::Service::new(connection_broker.clone(), queue_broker.clone()); @@ -32,6 +35,7 @@ async fn main() -> Result<(), Box> { futures::try_join!( bank_service.subscribe(), identity_service.subscribe(), + member_referrer_service.subscribe(), api_vendor_service.subscribe(), api_game_service.subscribe(), )?; diff --git a/src/member_referrer/mod.rs b/src/member_referrer/mod.rs new file mode 100644 index 0000000..1f278a4 --- /dev/null +++ b/src/member_referrer/mod.rs @@ -0,0 +1 @@ +pub mod service; diff --git a/src/member_referrer/service.rs b/src/member_referrer/service.rs new file mode 100644 index 0000000..961363d --- /dev/null +++ b/src/member_referrer/service.rs @@ -0,0 +1,173 @@ +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-frontend-server-edge::member_referrer::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.get_member_referrer_by_code(),).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 get_member_referrer_by_code(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::frontend::member_referrer::SUBJECT_GET_MEMBER_REFERRER_BY_CODE, + 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::member_referrer::GetMemberReferrerByCodeRequest::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_referrer_by_code_req = + bpr::ss::member_referrer::GetMemberReferrerByCodeRequest { + client: Some(client), + request: Some( + bpr::ss::member_referrer::get_member_referrer_by_code_request::Request { + code: req.code, + }, + ), + }; + + let ss_get_member_referrer_by_code_res_msg = self + .connection_broker + .request( + bpr::ss::member_referrer::SUBJECT_GET_MEMBER_REFERRER_BY_CODE, + ss_get_member_referrer_by_code_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_referrer_by_code_res = + bpr::ss::member_referrer::GetMemberReferrerByCodeResponse::decode( + ss_get_member_referrer_by_code_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_referrer_by_code_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_referrer_by_code_res.result { + message + .respond( + bpr::c2se::member_referrer::GetMemberReferrerByCodeResponse { + error: None, + result: Some( + bpr::c2se::member_referrer::get_member_referrer_by_code_response::Result { + member_referrer: r.member_referrer, + }, + ), + } + .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::member_referrer::GetMemberReferrerByCodeResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } +}