From ca232cb8ccabd0bccc2064ee5b2edb1fc05605b1 Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Fri, 19 Aug 2022 07:16:12 +0000 Subject: [PATCH] game api is added --- src/api/game/mod.rs | 1 + src/api/game/service.rs | 173 ++++++++++++++++++++++++++++++++ src/api/mod.rs | 3 +- src/api/vendor/mod.rs | 1 + src/api/{ => vendor}/service.rs | 0 src/main.rs | 8 +- 6 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 src/api/game/mod.rs create mode 100644 src/api/game/service.rs create mode 100644 src/api/vendor/mod.rs rename src/api/{ => vendor}/service.rs (100%) diff --git a/src/api/game/mod.rs b/src/api/game/mod.rs new file mode 100644 index 0000000..1f278a4 --- /dev/null +++ b/src/api/game/mod.rs @@ -0,0 +1 @@ +pub mod service; diff --git a/src/api/game/service.rs b/src/api/game/service.rs new file mode 100644 index 0000000..f9a1da4 --- /dev/null +++ b/src/api/game/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-backend-server-edge::game::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_games(),).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_games(&self) -> Result<(), Box> { + let s = self + .connection_broker + .queue_subscribe( + bpr::c2se::backend::api::game::SUBJECT_LIST_GAMES, + 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::game::ListGamesRequest::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_games_req = bpr::ss::api::game::ListGamesRequest { + client: Some(client), + request: Some(bpr::ss::api::game::list_games_request::Request { + pagination: req.pagination, + sorts: req.sorts, + search: match req.search { + Some(s) => Some(bpr::ss::api::game::list_games_request::request::Search { + vendor_id: s.vendor_id, + key_like: s.key_like, + category_like: s.category_like, + platform_like: s.platform_like, + game_type_like: s.game_type_like, + }), + None => None, + }, + }), + }; + + let ss_list_games_res_msg = self + .connection_broker + .request( + bpr::ss::api::game::SUBJECT_LIST_GAMES, + ss_list_games_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_games_res = + bpr::ss::api::game::ListGamesResponse::decode(ss_list_games_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_games_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_games_res.result { + message + .respond( + bpr::c2se::api::game::ListGamesResponse { + error: None, + result: Some(bpr::c2se::api::game::list_games_response::Result { games: r.games }), + } + .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::game::ListGamesResponse { + error: Some(bpr::protobuf::rpc::Error::from(e)), + result: None, + } + .encode_to_vec(), + ) + .await?; + } + } + + Ok(()) + } +} diff --git a/src/api/mod.rs b/src/api/mod.rs index 1f278a4..fd2b58f 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1 +1,2 @@ -pub mod service; +pub mod game; +pub mod vendor; diff --git a/src/api/vendor/mod.rs b/src/api/vendor/mod.rs new file mode 100644 index 0000000..1f278a4 --- /dev/null +++ b/src/api/vendor/mod.rs @@ -0,0 +1 @@ +pub mod service; diff --git a/src/api/service.rs b/src/api/vendor/service.rs similarity index 100% rename from src/api/service.rs rename to src/api/vendor/service.rs diff --git a/src/main.rs b/src/main.rs index f4e36eb..2c5d386 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,10 @@ async fn main() -> Result<(), Box> { member_referrer::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()); + let api_vendor_service = + api::vendor::service::Service::new(connection_broker.clone(), queue_broker.clone()); + let api_game_service = + api::game::service::Service::new(connection_broker.clone(), queue_broker.clone()); println!("Server edge[beteran-backend-server-edge] is started"); @@ -61,7 +64,8 @@ async fn main() -> Result<(), Box> { member_level_service.subscribe(), member_referrer_service.subscribe(), site_service.subscribe(), - api_service.subscribe(), + api_vendor_service.subscribe(), + api_game_service.subscribe(), )?; Ok(())