chore: added trait for upstream service
This commit is contained in:
@@ -8,12 +8,12 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use sea_orm::DatabaseConnection;
|
use sea_orm::DatabaseConnection;
|
||||||
|
|
||||||
use upstream::UpstreamService;
|
use crate::services::nginx::upstream::{UpstreamService, UpstreamServiceImpl};
|
||||||
|
|
||||||
pub struct NginxService {
|
pub struct NginxService {
|
||||||
connection: Arc<DatabaseConnection>,
|
connection: Arc<DatabaseConnection>,
|
||||||
//
|
//
|
||||||
upstream_service: Arc<UpstreamService>,
|
upstream_service: Arc<dyn UpstreamService>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NginxService {
|
impl NginxService {
|
||||||
@@ -21,11 +21,11 @@ impl NginxService {
|
|||||||
Self {
|
Self {
|
||||||
connection: connection.clone(),
|
connection: connection.clone(),
|
||||||
//
|
//
|
||||||
upstream_service: Arc::new(UpstreamService::new(connection.clone())),
|
upstream_service: Arc::new(UpstreamServiceImpl::new(connection.clone())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_upstream_service(&self) -> Arc<UpstreamService> {
|
pub fn get_upstream_service(&self) -> Arc<dyn UpstreamService> {
|
||||||
self.upstream_service.clone()
|
self.upstream_service.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,65 @@ use crate::{
|
|||||||
with_conn,
|
with_conn,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct UpstreamService {
|
#[async_trait::async_trait]
|
||||||
|
pub trait UpstreamService: Send + Sync {
|
||||||
|
async fn create_upstream(
|
||||||
|
&self,
|
||||||
|
create_info: UpstreamCreateInfo,
|
||||||
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
|
) -> Result<UpstreamInfo, ServiceError>;
|
||||||
|
async fn get_upstream(
|
||||||
|
&self,
|
||||||
|
upstream_id: uuid::Uuid,
|
||||||
|
options: Option<GetUpstreamOptions>,
|
||||||
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
|
) -> Result<UpstreamInfo, ServiceError>;
|
||||||
|
async fn get_upstreams(
|
||||||
|
&self,
|
||||||
|
pagination: Option<PaginationFilter>,
|
||||||
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
|
) -> Result<Vec<UpstreamInfo>, ServiceError>;
|
||||||
|
async fn update_upstream(
|
||||||
|
&self,
|
||||||
|
id: uuid::Uuid,
|
||||||
|
upstream: UpdateUpstreamInfo,
|
||||||
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
|
) -> Result<UpstreamInfo, ServiceError>;
|
||||||
|
async fn delete_upstream(
|
||||||
|
&self,
|
||||||
|
upstream_id: uuid::Uuid,
|
||||||
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
|
) -> Result<(), ServiceError>;
|
||||||
|
async fn create_upstream_target(
|
||||||
|
&self,
|
||||||
|
create_info: UpstreamTargetCreateInfo,
|
||||||
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
|
) -> Result<UpstreamTargetInfo, ServiceError>;
|
||||||
|
async fn get_upstream_target(
|
||||||
|
&self,
|
||||||
|
target_id: uuid::Uuid,
|
||||||
|
options: Option<GetUpstreamTargetOptions>,
|
||||||
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
|
) -> Result<UpstreamTargetInfo, ServiceError>;
|
||||||
|
async fn get_upstream_targets_by_upstream(
|
||||||
|
&self,
|
||||||
|
upstream_id: uuid::Uuid,
|
||||||
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
|
) -> Result<Vec<UpstreamTargetInfo>, ServiceError>;
|
||||||
|
async fn update_upstream_target(
|
||||||
|
&self,
|
||||||
|
id: uuid::Uuid,
|
||||||
|
target: UpdateUpstreamTargetInfo,
|
||||||
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
|
) -> Result<UpstreamTargetInfo, ServiceError>;
|
||||||
|
async fn delete_upstream_target(
|
||||||
|
&self,
|
||||||
|
target_id: uuid::Uuid,
|
||||||
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
|
) -> Result<(), ServiceError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct UpstreamServiceImpl {
|
||||||
connection: Arc<DatabaseConnection>,
|
connection: Arc<DatabaseConnection>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,13 +89,15 @@ pub struct GetUpstreamTargetOptions {
|
|||||||
pub include_upstream: bool,
|
pub include_upstream: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UpstreamService {
|
impl UpstreamServiceImpl {
|
||||||
pub fn new(connection: Arc<DatabaseConnection>) -> Self {
|
pub fn new(connection: Arc<DatabaseConnection>) -> Self {
|
||||||
Self { connection }
|
Self { connection }
|
||||||
}
|
}
|
||||||
//
|
}
|
||||||
//
|
|
||||||
pub async fn create_upstream(
|
#[async_trait::async_trait]
|
||||||
|
impl UpstreamService for UpstreamServiceImpl {
|
||||||
|
async fn create_upstream(
|
||||||
&self,
|
&self,
|
||||||
create_info: UpstreamCreateInfo,
|
create_info: UpstreamCreateInfo,
|
||||||
tx: Option<&mut DatabaseTransaction>,
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
@@ -79,7 +139,7 @@ impl UpstreamService {
|
|||||||
Ok(r.into())
|
Ok(r.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_upstream(
|
async fn get_upstream(
|
||||||
&self,
|
&self,
|
||||||
upstream_id: uuid::Uuid,
|
upstream_id: uuid::Uuid,
|
||||||
options: Option<GetUpstreamOptions>,
|
options: Option<GetUpstreamOptions>,
|
||||||
@@ -117,7 +177,7 @@ impl UpstreamService {
|
|||||||
Ok(info)
|
Ok(info)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_upstreams(
|
async fn get_upstreams(
|
||||||
&self,
|
&self,
|
||||||
pagination: Option<PaginationFilter>,
|
pagination: Option<PaginationFilter>,
|
||||||
tx: Option<&mut DatabaseTransaction>,
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
@@ -136,7 +196,7 @@ impl UpstreamService {
|
|||||||
Ok(r.into_iter().map(|m| m.into()).collect())
|
Ok(r.into_iter().map(|m| m.into()).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_upstream(
|
async fn update_upstream(
|
||||||
&self,
|
&self,
|
||||||
id: uuid::Uuid,
|
id: uuid::Uuid,
|
||||||
upstream: UpdateUpstreamInfo,
|
upstream: UpdateUpstreamInfo,
|
||||||
@@ -193,7 +253,7 @@ impl UpstreamService {
|
|||||||
Ok(r.into())
|
Ok(r.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_upstream(
|
async fn delete_upstream(
|
||||||
&self,
|
&self,
|
||||||
upstream_id: uuid::Uuid,
|
upstream_id: uuid::Uuid,
|
||||||
tx: Option<&mut DatabaseTransaction>,
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
@@ -220,7 +280,7 @@ impl UpstreamService {
|
|||||||
|
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
pub async fn create_upstream_target(
|
async fn create_upstream_target(
|
||||||
&self,
|
&self,
|
||||||
create_info: UpstreamTargetCreateInfo,
|
create_info: UpstreamTargetCreateInfo,
|
||||||
tx: Option<&mut DatabaseTransaction>,
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
@@ -230,7 +290,7 @@ impl UpstreamService {
|
|||||||
Ok(r.into())
|
Ok(r.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_upstream_target(
|
async fn get_upstream_target(
|
||||||
&self,
|
&self,
|
||||||
target_id: uuid::Uuid,
|
target_id: uuid::Uuid,
|
||||||
options: Option<GetUpstreamTargetOptions>,
|
options: Option<GetUpstreamTargetOptions>,
|
||||||
@@ -273,7 +333,7 @@ impl UpstreamService {
|
|||||||
Ok(info)
|
Ok(info)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_upstream_targets_by_upstream(
|
async fn get_upstream_targets_by_upstream(
|
||||||
&self,
|
&self,
|
||||||
upstream_id: uuid::Uuid,
|
upstream_id: uuid::Uuid,
|
||||||
tx: Option<&mut DatabaseTransaction>,
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
@@ -287,7 +347,7 @@ impl UpstreamService {
|
|||||||
Ok(r.into_iter().map(|m| m.into()).collect())
|
Ok(r.into_iter().map(|m| m.into()).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_upstream_target(
|
async fn update_upstream_target(
|
||||||
&self,
|
&self,
|
||||||
id: uuid::Uuid,
|
id: uuid::Uuid,
|
||||||
target: UpdateUpstreamTargetInfo,
|
target: UpdateUpstreamTargetInfo,
|
||||||
@@ -308,7 +368,7 @@ impl UpstreamService {
|
|||||||
Ok(r.into())
|
Ok(r.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete_upstream_target(
|
async fn delete_upstream_target(
|
||||||
&self,
|
&self,
|
||||||
target_id: uuid::Uuid,
|
target_id: uuid::Uuid,
|
||||||
tx: Option<&mut DatabaseTransaction>,
|
tx: Option<&mut DatabaseTransaction>,
|
||||||
@@ -356,7 +416,7 @@ mod tests {
|
|||||||
.append_query_results(vec![vec![up_model.clone()]])
|
.append_query_results(vec![vec![up_model.clone()]])
|
||||||
.into_connection();
|
.into_connection();
|
||||||
|
|
||||||
let svc = UpstreamService::new(Arc::new(db));
|
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||||
|
|
||||||
let create_info = crate::services::nginx::info::upstream::UpstreamCreateInfo {
|
let create_info = crate::services::nginx::info::upstream::UpstreamCreateInfo {
|
||||||
name: "test_upstream".to_string(),
|
name: "test_upstream".to_string(),
|
||||||
@@ -407,7 +467,7 @@ mod tests {
|
|||||||
.append_query_results(vec![vec![target_model.clone()]])
|
.append_query_results(vec![vec![target_model.clone()]])
|
||||||
.into_connection();
|
.into_connection();
|
||||||
|
|
||||||
let svc = UpstreamService::new(Arc::new(db));
|
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||||
|
|
||||||
let res = svc
|
let res = svc
|
||||||
.get_upstream(
|
.get_upstream(
|
||||||
@@ -432,7 +492,7 @@ mod tests {
|
|||||||
.append_query_results(vec![Vec::<sea_orm::MockRow>::new()])
|
.append_query_results(vec![Vec::<sea_orm::MockRow>::new()])
|
||||||
.into_connection();
|
.into_connection();
|
||||||
|
|
||||||
let svc = UpstreamService::new(Arc::new(db));
|
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||||
|
|
||||||
let res = svc.get_upstream(uuid::Uuid::new_v4(), None, None).await;
|
let res = svc.get_upstream(uuid::Uuid::new_v4(), None, None).await;
|
||||||
|
|
||||||
@@ -466,7 +526,7 @@ mod tests {
|
|||||||
.append_query_results(vec![vec![u1.clone(), u2.clone()]])
|
.append_query_results(vec![vec![u1.clone(), u2.clone()]])
|
||||||
.into_connection();
|
.into_connection();
|
||||||
|
|
||||||
let svc = UpstreamService::new(Arc::new(db));
|
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||||
|
|
||||||
let res = svc.get_upstreams(None, None).await;
|
let res = svc.get_upstreams(None, None).await;
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
@@ -494,7 +554,7 @@ mod tests {
|
|||||||
.append_query_results(vec![vec![t.clone()]])
|
.append_query_results(vec![vec![t.clone()]])
|
||||||
.into_connection();
|
.into_connection();
|
||||||
|
|
||||||
let svc = UpstreamService::new(Arc::new(db));
|
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||||
|
|
||||||
let res = svc.get_upstream_targets_by_upstream(up_id, None).await;
|
let res = svc.get_upstream_targets_by_upstream(up_id, None).await;
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
@@ -532,7 +592,7 @@ mod tests {
|
|||||||
.append_query_results(vec![vec![updated.clone()]]) // update result
|
.append_query_results(vec![vec![updated.clone()]]) // update result
|
||||||
.into_connection();
|
.into_connection();
|
||||||
|
|
||||||
let svc = UpstreamService::new(Arc::new(db));
|
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||||
|
|
||||||
let update_info = crate::services::nginx::info::upstream::UpdateUpstreamInfo {
|
let update_info = crate::services::nginx::info::upstream::UpdateUpstreamInfo {
|
||||||
name: None,
|
name: None,
|
||||||
@@ -553,7 +613,7 @@ mod tests {
|
|||||||
.append_query_results(vec![Vec::<sea_orm::MockRow>::new()])
|
.append_query_results(vec![Vec::<sea_orm::MockRow>::new()])
|
||||||
.into_connection();
|
.into_connection();
|
||||||
|
|
||||||
let svc = UpstreamService::new(Arc::new(db));
|
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||||
|
|
||||||
let res = svc
|
let res = svc
|
||||||
.update_upstream(
|
.update_upstream(
|
||||||
@@ -595,7 +655,7 @@ mod tests {
|
|||||||
}])
|
}])
|
||||||
.into_connection();
|
.into_connection();
|
||||||
|
|
||||||
let svc = UpstreamService::new(Arc::new(db));
|
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||||
|
|
||||||
let res = svc.delete_upstream(id, None).await;
|
let res = svc.delete_upstream(id, None).await;
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
@@ -607,7 +667,7 @@ mod tests {
|
|||||||
.append_query_results(vec![Vec::<sea_orm::MockRow>::new()])
|
.append_query_results(vec![Vec::<sea_orm::MockRow>::new()])
|
||||||
.into_connection();
|
.into_connection();
|
||||||
|
|
||||||
let svc = UpstreamService::new(Arc::new(db));
|
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||||
|
|
||||||
let res = svc.delete_upstream(uuid::Uuid::new_v4(), None).await;
|
let res = svc.delete_upstream(uuid::Uuid::new_v4(), None).await;
|
||||||
assert!(matches!(res, Err(ServiceError::NotFound(_))));
|
assert!(matches!(res, Err(ServiceError::NotFound(_))));
|
||||||
@@ -633,7 +693,7 @@ mod tests {
|
|||||||
.append_query_results(vec![vec![created.clone()]])
|
.append_query_results(vec![vec![created.clone()]])
|
||||||
.into_connection();
|
.into_connection();
|
||||||
|
|
||||||
let svc = UpstreamService::new(Arc::new(db));
|
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||||
|
|
||||||
let create_info = crate::services::nginx::info::upstream_target::UpstreamTargetCreateInfo {
|
let create_info = crate::services::nginx::info::upstream_target::UpstreamTargetCreateInfo {
|
||||||
target_host: "1.2.3.4".to_string(),
|
target_host: "1.2.3.4".to_string(),
|
||||||
@@ -681,7 +741,7 @@ mod tests {
|
|||||||
.append_query_results(vec![vec![updated.clone()]])
|
.append_query_results(vec![vec![updated.clone()]])
|
||||||
.into_connection();
|
.into_connection();
|
||||||
|
|
||||||
let svc = UpstreamService::new(Arc::new(db));
|
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||||
|
|
||||||
let update_info = crate::services::nginx::info::upstream_target::UpdateUpstreamTargetInfo {
|
let update_info = crate::services::nginx::info::upstream_target::UpdateUpstreamTargetInfo {
|
||||||
target_host: None,
|
target_host: None,
|
||||||
@@ -719,7 +779,7 @@ mod tests {
|
|||||||
}])
|
}])
|
||||||
.into_connection();
|
.into_connection();
|
||||||
|
|
||||||
let svc = UpstreamService::new(Arc::new(db));
|
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||||
let res = svc.delete_upstream_target(id, None).await;
|
let res = svc.delete_upstream_target(id, None).await;
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user