From b2a322ed796b43ffaa39de5d870993eb9b9721ab Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Tue, 30 Dec 2025 18:22:18 +0800 Subject: [PATCH] chore: added trait for upstream service --- apps/api/src/services/nginx.rs | 8 +- apps/api/src/services/nginx/upstream.rs | 112 ++++++++++++++++++------ 2 files changed, 90 insertions(+), 30 deletions(-) diff --git a/apps/api/src/services/nginx.rs b/apps/api/src/services/nginx.rs index 383c4cc..bb16164 100644 --- a/apps/api/src/services/nginx.rs +++ b/apps/api/src/services/nginx.rs @@ -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, // - upstream_service: Arc, + upstream_service: Arc, } 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 { + pub fn get_upstream_service(&self) -> Arc { self.upstream_service.clone() } } diff --git a/apps/api/src/services/nginx/upstream.rs b/apps/api/src/services/nginx/upstream.rs index 8833be0..df12161 100644 --- a/apps/api/src/services/nginx/upstream.rs +++ b/apps/api/src/services/nginx/upstream.rs @@ -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; + async fn get_upstream( + &self, + upstream_id: uuid::Uuid, + options: Option, + tx: Option<&mut DatabaseTransaction>, + ) -> Result; + async fn get_upstreams( + &self, + pagination: Option, + tx: Option<&mut DatabaseTransaction>, + ) -> Result, ServiceError>; + async fn update_upstream( + &self, + id: uuid::Uuid, + upstream: UpdateUpstreamInfo, + tx: Option<&mut DatabaseTransaction>, + ) -> Result; + 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; + async fn get_upstream_target( + &self, + target_id: uuid::Uuid, + options: Option, + tx: Option<&mut DatabaseTransaction>, + ) -> Result; + async fn get_upstream_targets_by_upstream( + &self, + upstream_id: uuid::Uuid, + tx: Option<&mut DatabaseTransaction>, + ) -> Result, ServiceError>; + async fn update_upstream_target( + &self, + id: uuid::Uuid, + target: UpdateUpstreamTargetInfo, + tx: Option<&mut DatabaseTransaction>, + ) -> Result; + async fn delete_upstream_target( + &self, + target_id: uuid::Uuid, + tx: Option<&mut DatabaseTransaction>, + ) -> Result<(), ServiceError>; +} + +pub struct UpstreamServiceImpl { connection: Arc, } @@ -31,13 +89,15 @@ pub struct GetUpstreamTargetOptions { pub include_upstream: bool, } -impl UpstreamService { +impl UpstreamServiceImpl { pub fn new(connection: Arc) -> 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, @@ -117,7 +177,7 @@ impl UpstreamService { Ok(info) } - pub async fn get_upstreams( + async fn get_upstreams( &self, pagination: Option, 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, @@ -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::::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::::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::::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()); }