chore: added trait for upstream service

This commit is contained in:
GW_MC
2025-12-30 18:22:18 +08:00
parent f05544267c
commit b2a322ed79
2 changed files with 90 additions and 30 deletions

View File

@@ -8,12 +8,12 @@ use std::sync::Arc;
use sea_orm::DatabaseConnection;
use upstream::UpstreamService;
use crate::services::nginx::upstream::{UpstreamService, UpstreamServiceImpl};
pub struct NginxService {
connection: Arc<DatabaseConnection>,
//
upstream_service: Arc<UpstreamService>,
upstream_service: Arc<dyn UpstreamService>,
}
impl NginxService {
@@ -21,11 +21,11 @@ impl NginxService {
Self {
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()
}
}

View File

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