implemented

This commit is contained in:
병준 박 2022-08-11 05:34:46 +00:00
parent 2720923087
commit afcc1d7ade
5 changed files with 399 additions and 4 deletions

View File

@ -19,7 +19,7 @@ prost = { version = "0" }
nats = { version = "0" } nats = { version = "0" }
base64 = { version = "0" } base64 = { version = "0" }
beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.29-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.12-snapshot" } beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.15-snapshot" }
[build-dependencies] [build-dependencies]

View File

@ -1,6 +1,7 @@
use std::env; use std::env;
mod identity; mod identity;
mod member;
mod site; mod site;
#[tokio::main] #[tokio::main]
@ -17,13 +18,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let broker_opts = nats::asynk::Options::new(); let broker_opts = nats::asynk::Options::new();
let connection_broker = broker_opts.connect(url_broker).await?; 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 = let identity_service =
identity::service::Service::new(connection_broker.clone(), queue_broker.clone()); 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"); 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(()) Ok(())
} }

1
src/member/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod service;

381
src/member/service.rs Normal file
View File

@ -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<dyn std::error::Error>> {
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<bpr::models::core::network::Client, bcr::error::rpc::Error> {
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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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(())
}
}

View File

@ -93,6 +93,12 @@ impl Service {
pagination: req.pagination, pagination: req.pagination,
searches: req.searches, searches: req.searches,
sorts: req.sorts, 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 let ss_list_sites_res_msg = self