site is added

This commit is contained in:
병준 박 2022-08-08 10:10:34 +00:00
parent 499101855c
commit 93332c1628
10 changed files with 648 additions and 21 deletions

View File

@ -30,7 +30,7 @@ tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tokio-cron-scheduler = { version = "0" } tokio-cron-scheduler = { version = "0" }
uuid = { version = "0", features = ["serde", "v4", "v5"] } uuid = { version = "0", features = ["serde", "v4", "v5"] }
beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.25-snapshot" } beteran-protobuf-rust = { git = "https://gitlab.loafle.net/bet/beteran-protobuf-rust.git", tag = "v0.1.27-snapshot" }
beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.5-snapshot" } beteran-common-rust = { git = "https://gitlab.loafle.net/bet/beteran-common-rust.git", tag = "v0.1.10-snapshot" }
[build-dependencies] [build-dependencies]

View File

@ -39,21 +39,8 @@ impl Composition {
let expiration_at = issued_at + Duration::days(1); let expiration_at = issued_at + Duration::days(1);
let claims = models::Claims { let claims = models::Claims {
iss: issuer, iss: issuer,
iat: issued_at iat: issued_at.unix_timestamp(),
.date() exp: expiration_at.unix_timestamp(),
.with_hms_milli(issued_at.hour(), issued_at.minute(), issued_at.second(), 0)
.unwrap()
.assume_utc(),
exp: expiration_at
.date()
.with_hms_milli(
expiration_at.hour(),
expiration_at.minute(),
expiration_at.second(),
0,
)
.unwrap()
.assume_utc(),
session_id, session_id,
}; };

View File

@ -1,5 +1,4 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
// #[derive(Debug, Serialize, Deserialize)] // #[derive(Debug, Serialize, Deserialize)]
// struct Claims { // struct Claims {
@ -14,7 +13,7 @@ use time::OffsetDateTime;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Claims { pub struct Claims {
pub iss: String, pub iss: String,
pub iat: OffsetDateTime, pub iat: i64,
pub exp: OffsetDateTime, pub exp: i64,
pub session_id: String, pub session_id: String,
} }

View File

@ -67,10 +67,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
password_salt.clone(), password_salt.clone(),
jwt_secret.clone(), jwt_secret.clone(),
); );
let site_service = services::site::service::Service::new(
connection_server_broker.clone(),
queue_server_broker.clone(),
pool.clone(),
);
println!("Server service [beteran-server-service] is started"); println!("Server service [beteran-server-service] is started");
futures::try_join!(identity_service.subscribe(),)?; futures::try_join!(site_service.subscribe(), identity_service.subscribe(),)?;
Ok(()) Ok(())
} }

View File

@ -21,6 +21,30 @@ pub struct Site {
pub deleted_at: Option<i64>, pub deleted_at: Option<i64>,
} }
///
#[derive(Insertable, Debug, Clone)]
#[table_name = "sites"]
pub struct NewSite {
///
pub url: String,
///
pub show: bool,
///
pub can_use: bool,
}
///
#[derive(AsChangeset, Debug, Clone)]
#[table_name = "sites"]
pub struct ModifySite {
///
pub url: String,
///
pub show: bool,
///
pub can_use: bool,
}
/// ///
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FindAll { pub struct FindAll {

View File

@ -26,6 +26,19 @@ impl Repository {
Repository {} Repository {}
} }
///
pub fn insert(
&self,
conn: &diesel::PgConnection,
new_member: &models::NewSite,
) -> Result<models::Site, Error> {
let inserted = diesel::insert_into(sites::table)
.values(new_member)
.get_result::<models::Site>(conn)?;
Ok(inserted)
}
/// ///
pub fn select( pub fn select(
&self, &self,
@ -147,4 +160,28 @@ impl Repository {
q.load::<models::Site>(conn) q.load::<models::Site>(conn)
} }
///
pub fn update(
&self,
conn: &diesel::PgConnection,
id: uuid::Uuid,
modify: &models::ModifySite,
) -> Result<u64, Error> {
use sites::dsl;
diesel::update(dsl::sites.filter(dsl::id.eq(id)))
.set(modify)
.execute(conn)
.map(|c| c as u64)
}
///
pub fn delete(&self, conn: &diesel::PgConnection, id: uuid::Uuid) -> Result<u64, Error> {
use sites::dsl;
diesel::delete(sites::table.filter(dsl::id.eq(id)))
.execute(conn)
.map(|c| c as u64)
}
} }

View File

@ -1 +1,2 @@
pub mod identity; pub mod identity;
pub mod site;

2
src/services/site/mod.rs Normal file
View File

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

View File

@ -0,0 +1,16 @@
use crate::repositories;
use beteran_protobuf_rust as bpr;
impl From<&repositories::site::models::Site> for bpr::models::domain::Site {
fn from(d: &repositories::site::models::Site) -> Self {
bpr::models::domain::Site {
id: d.id.to_string(),
url: d.url.clone(),
show: d.show,
can_use: d.can_use,
created_at: d.created_at as u64,
updated_at: d.updated_at as u64,
deleted_at: d.deleted_at.map(|d| d as u64),
}
}
}

View File

@ -0,0 +1,556 @@
//!
//!
use std::str::FromStr;
use super::models;
use crate::compositions;
use crate::repositories;
use beteran_common_rust as bcr;
use beteran_protobuf_rust as bpr;
use diesel::{
r2d2::{ConnectionManager, Pool},
PgConnection,
};
use prost::Message;
///
pub struct Service {
connection_broker: nats::asynk::Connection,
queue_broker: String,
pool: Pool<ConnectionManager<PgConnection>>,
site_repository: repositories::site::repository::Repository,
site_composition: compositions::site::composition::Composition,
}
impl std::fmt::Debug for Service {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("Service of service.member.service.identity")
.finish()
}
}
impl Service {
///
pub fn new(
connection_broker: nats::asynk::Connection,
queue_broker: String,
pool: Pool<ConnectionManager<PgConnection>>,
) -> Service {
Service {
connection_broker,
queue_broker,
pool,
site_repository: repositories::site::repository::Repository::new(),
site_composition: compositions::site::composition::Composition::new(),
}
}
pub async fn subscribe(&self) -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
futures::try_join!(
self.list_sites(),
self.create_site(),
self.update_site(),
self.delete_site(),
)
.map(|_| ())
}
fn check_site(
&self,
conn: &diesel::PgConnection,
url: Option<String>,
site_id: uuid::Uuid,
) -> Result<repositories::site::models::Site, bcr::error::rpc::Error> {
match self
.site_composition
.select_by_url(conn, url, site_id)
.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,
})
})? {
Some(s) => Ok(s),
None => Err(bcr::error::rpc::Error::InvalidParams(
bcr::error::rpc::InvalidParams {
message: "invalid site_url information".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "client.site_url".to_string(),
value: "".to_string(),
error_type: bcr::error::rpc::InvalidParamsType::None,
message: "".to_string(),
},
},
)),
}
}
async fn list_sites(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::ss::domain::site::SUBJECT_LIST_SITES,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let req = bpr::ss::domain::site::ListSitesRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let client = match req.client {
Some(c) => c,
None => {
return Err(bcr::error::rpc::Error::InvalidParams(
bcr::error::rpc::InvalidParams {
message: "invalid client information".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "client".to_string(),
value: "".to_string(),
error_type: bcr::error::rpc::InvalidParamsType::Required,
message: "".to_string(),
},
},
));
}
};
let conn = self.pool.get().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 count = self
.site_repository
.select_all_count(
&conn,
repositories::site::models::FindAll {
show: None,
can_use: None,
url_like: None,
pagination: req
.pagination
.as_ref()
.map(|d| bcr::models::pagination::Pagination::from(d)),
sorts: Some(
req
.sorts
.iter()
.map(|d| beteran_common_rust::models::pagination::Sort::from(d))
.collect(),
),
},
)
.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 list = self
.site_repository
.select_all(
&conn,
repositories::site::models::FindAll {
show: None,
can_use: None,
url_like: None,
pagination: req
.pagination
.as_ref()
.map(|d| bcr::models::pagination::Pagination::from(d)),
sorts: Some(
req
.sorts
.iter()
.map(|d| beteran_common_rust::models::pagination::Sort::from(d))
.collect(),
),
},
)
.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,
})
})?;
message
.respond(
bpr::ss::domain::site::ListSitesResponse {
error: None,
result: Some(bpr::ss::domain::site::list_sites_response::Result {
sites: list
.iter()
.map(|d| bpr::models::domain::Site::from(d))
.collect(),
}),
}
.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::ss::domain::site::ListSitesResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn create_site(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::ss::domain::site::SUBJECT_CREATE_SITE,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let req = bpr::ss::domain::site::CreateSiteRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let client = match req.client {
Some(c) => c,
None => {
return Err(bcr::error::rpc::Error::InvalidParams(
bcr::error::rpc::InvalidParams {
message: "invalid client information".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "client".to_string(),
value: "".to_string(),
error_type: bcr::error::rpc::InvalidParamsType::Required,
message: "".to_string(),
},
},
));
}
};
let conn = self.pool.get().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 s = self
.site_repository
.insert(
&conn,
&repositories::site::models::NewSite {
url: req.url,
show: req.show,
can_use: req.can_use,
},
)
.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,
})
})?;
message
.respond(
bpr::ss::domain::site::CreateSiteResponse {
error: None,
result: Some(bpr::ss::domain::site::create_site_response::Result {
site: Some(bpr::models::domain::Site::from(&s)),
}),
}
.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::ss::domain::site::CreateSiteResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn update_site(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::ss::domain::site::SUBJECT_UPDATE_SITE,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let req = bpr::ss::domain::site::UpdateSiteRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| {
bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams {
message: "invalid id param".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "id".to_string(),
value: req.id.clone(),
error_type: bcr::error::rpc::InvalidParamsType::Required,
message: e.to_string(),
},
})
})?;
let client = match req.client {
Some(c) => c,
None => {
return Err(bcr::error::rpc::Error::InvalidParams(
bcr::error::rpc::InvalidParams {
message: "invalid client information".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "client".to_string(),
value: "".to_string(),
error_type: bcr::error::rpc::InvalidParamsType::Required,
message: "".to_string(),
},
},
));
}
};
let conn = self.pool.get().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 _affected = self
.site_repository
.update(
&conn,
id,
&repositories::site::models::ModifySite {
url: req.url,
show: req.show,
can_use: req.can_use,
},
)
.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 s = self.site_repository.select(&conn, id).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,
})
})?;
message
.respond(
bpr::ss::domain::site::UpdateSiteResponse {
error: None,
result: Some(bpr::ss::domain::site::update_site_response::Result {
site: s.map(|d| bpr::models::domain::Site::from(&d)),
}),
}
.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::ss::domain::site::UpdateSiteResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
async fn delete_site(&self) -> Result<(), Box<dyn std::error::Error>> {
let s = self
.connection_broker
.queue_subscribe(
bpr::ss::domain::site::SUBJECT_DELETE_SITE,
self.queue_broker.as_str(),
)
.await?;
while let Some(message) = s.next().await {
if let Err(e) = async {
let req = bpr::ss::domain::site::DeleteSiteRequest::decode(message.data.as_slice())
.map_err(|e| {
bcr::error::rpc::Error::InvalidRequest(bcr::error::rpc::InvalidRequest {
message: format!("invalid request: {}", e),
})
})?;
let id = uuid::Uuid::from_str(req.id.as_str()).map_err(|e| {
bcr::error::rpc::Error::InvalidParams(bcr::error::rpc::InvalidParams {
message: "invalid id param".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "id".to_string(),
value: req.id.clone(),
error_type: bcr::error::rpc::InvalidParamsType::Required,
message: e.to_string(),
},
})
})?;
let client = match req.client {
Some(c) => c,
None => {
return Err(bcr::error::rpc::Error::InvalidParams(
bcr::error::rpc::InvalidParams {
message: "invalid client information".to_string(),
detail: bcr::error::rpc::InvalidParamsDetail {
location: "request".to_string(),
param: "client".to_string(),
value: "".to_string(),
error_type: bcr::error::rpc::InvalidParamsType::Required,
message: "".to_string(),
},
},
));
}
};
let conn = self.pool.get().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 _affected = self.site_repository.delete(&conn, id).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,
})
})?;
message
.respond(
bpr::ss::domain::site::DeleteSiteResponse {
error: None,
result: Some(bpr::ss::domain::site::delete_site_response::Result {}),
}
.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::ss::domain::site::DeleteSiteResponse {
error: Some(bpr::protobuf::rpc::Error::from(e)),
result: None,
}
.encode_to_vec(),
)
.await?;
}
}
Ok(())
}
}