From abeea4fad7b615c9babc785710e71ebdd9a0161d Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Mon, 29 Dec 2025 18:01:57 +0800 Subject: [PATCH] refactor: upstream info response structures and module --- .../routes/api/restricted/nginx/upstream.rs | 1 + .../restricted/nginx/upstream/get_upstream.rs | 80 +---------- .../nginx/upstream/get_upstream_target.rs | 59 +------- .../api/restricted/nginx/upstream/info.rs | 1 + .../nginx/upstream/info/response.rs | 129 ++++++++++++++++++ 5 files changed, 142 insertions(+), 128 deletions(-) create mode 100644 apps/api/src/routes/api/restricted/nginx/upstream/info.rs create mode 100644 apps/api/src/routes/api/restricted/nginx/upstream/info/response.rs diff --git a/apps/api/src/routes/api/restricted/nginx/upstream.rs b/apps/api/src/routes/api/restricted/nginx/upstream.rs index 2ec6f0b..408ca72 100644 --- a/apps/api/src/routes/api/restricted/nginx/upstream.rs +++ b/apps/api/src/routes/api/restricted/nginx/upstream.rs @@ -1,5 +1,6 @@ pub mod get_upstream; pub mod get_upstream_target; +pub mod info; use std::sync::Arc; diff --git a/apps/api/src/routes/api/restricted/nginx/upstream/get_upstream.rs b/apps/api/src/routes/api/restricted/nginx/upstream/get_upstream.rs index 44cb0e4..65777b7 100644 --- a/apps/api/src/routes/api/restricted/nginx/upstream/get_upstream.rs +++ b/apps/api/src/routes/api/restricted/nginx/upstream/get_upstream.rs @@ -5,7 +5,6 @@ use axum::{ extract::{Path, Query, State}, response::Result as AxumResult, }; -use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -13,7 +12,12 @@ use crate::{ errors::service_error::ServiceError, routes::{ AppState, - api::helper::pagination::{ExtractPagination, PaginationInfo}, + api::{ + helper::pagination::{ExtractPagination, PaginationInfo}, + restricted::nginx::upstream::info::response::{ + UpstreamInfoResponse, UpstreamListResponse, + }, + }, }, services::nginx::upstream::GetUpstreamOptions, }; @@ -35,77 +39,6 @@ impl From for ConcreteGetUpstreamParams { } } -#[derive(Serialize, Deserialize, utoipa::ToSchema)] -pub struct UpstreamTargetBasicInfo { - pub id: uuid::Uuid, - pub target_host: String, - pub target_port: i64, - pub enabled: bool, - pub is_backup: bool, - pub weight: i32, - // - pub created_at: DateTime, - pub updated_at: DateTime, -} - -impl From - for UpstreamTargetBasicInfo -{ - fn from(info: crate::services::nginx::info::upstream_target::UpstreamTargetInfo) -> Self { - Self { - id: info.id, - target_host: info.target_host, - target_port: info.target_port, - enabled: info.enabled, - is_backup: info.is_backup, - weight: info.weight as i32, - // - created_at: info.created_at, - updated_at: info.updated_at, - } - } -} - -#[derive(Serialize, Deserialize, utoipa::ToSchema)] -pub struct UpstreamInfoResponse { - pub id: uuid::Uuid, - pub name: String, - pub protocol: String, - pub algorithm: String, - pub sticky_session: bool, - pub created_by: Option, - pub created_at: DateTime, - pub updated_at: DateTime, - // - pub upstream_targets: Vec, -} - -impl From for UpstreamInfoResponse { - fn from(info: crate::services::nginx::info::upstream::UpstreamInfo) -> Self { - Self { - id: info.id, - name: info.name, - protocol: info.protocol, - algorithm: info.algorithm, - sticky_session: info.sticky_session, - created_by: info.created_by, - created_at: info.created_at, - updated_at: info.updated_at, - upstream_targets: info - .upstream_targets - .into_iter() - .map(|t| t.into()) - .collect(), - } - } -} - -#[derive(Serialize, Deserialize, utoipa::ToSchema)] -pub struct UpstreamListResponse { - pub items: Vec, - pub pagination: PaginationInfo, -} - pub async fn get_upstream_list( ExtractPagination(pagination): ExtractPagination, State(state): State>, @@ -168,6 +101,7 @@ mod tests { use crate::configs::{FromConfig, ProgramSettings}; use crate::routes::api::restricted::nginx::upstream::get_upstream_router; + use crate::routes::api::restricted::nginx::upstream::info::response::UpstreamInfoResponse; use crate::services::get_app_service; fn get_router_with_state(db: DatabaseConnection) -> axum::Router { diff --git a/apps/api/src/routes/api/restricted/nginx/upstream/get_upstream_target.rs b/apps/api/src/routes/api/restricted/nginx/upstream/get_upstream_target.rs index 5bf1045..4d9de96 100644 --- a/apps/api/src/routes/api/restricted/nginx/upstream/get_upstream_target.rs +++ b/apps/api/src/routes/api/restricted/nginx/upstream/get_upstream_target.rs @@ -5,11 +5,13 @@ use axum::{ extract::{Path, Query, State}, response::Result as AxumResult, }; -use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use uuid::Uuid; -use crate::{errors::service_error::ServiceError, routes::AppState}; +use crate::{ + errors::service_error::ServiceError, + routes::{AppState, api::restricted::nginx::upstream::info::response::UpstreamTargetInfo}, +}; #[derive(Serialize, Deserialize, utoipa::ToSchema)] pub struct GetUpstreamTargetsParams { @@ -28,59 +30,6 @@ impl From for ConcreteGetUpstreamTargetsParams { } } -#[derive(Serialize, Deserialize, utoipa::ToSchema)] -pub struct UpstreamTargetInfo { - pub id: uuid::Uuid, - pub target_host: String, - pub target_port: i64, - pub enabled: bool, - pub is_backup: bool, - pub weight: i32, - // - pub created_at: DateTime, - pub updated_at: DateTime, - // - pub upstream_id: Uuid, - pub upstream: Option, -} - -#[derive(Serialize, Deserialize, utoipa::ToSchema)] -pub struct UpstreamBasicInfo { - pub id: uuid::Uuid, - pub name: String, - pub protocol: String, - // - pub created_at: DateTime, - pub updated_at: DateTime, -} - -impl From - for UpstreamTargetInfo -{ - fn from(info: crate::services::nginx::info::upstream_target::UpstreamTargetInfo) -> Self { - Self { - id: info.id, - target_host: info.target_host, - target_port: info.target_port, - enabled: info.enabled, - is_backup: info.is_backup, - weight: info.weight as i32, - // - created_at: info.created_at, - updated_at: info.updated_at, - // - upstream_id: info.upstream_id, - upstream: info.upstream.map(|u| UpstreamBasicInfo { - id: u.id, - name: u.name, - protocol: u.protocol, - created_at: u.created_at, - updated_at: u.updated_at, - }), - } - } -} - pub async fn get_upstream_target( Path(upstream_target_id): Path, Query(params): Query, diff --git a/apps/api/src/routes/api/restricted/nginx/upstream/info.rs b/apps/api/src/routes/api/restricted/nginx/upstream/info.rs new file mode 100644 index 0000000..4c6f2cd --- /dev/null +++ b/apps/api/src/routes/api/restricted/nginx/upstream/info.rs @@ -0,0 +1 @@ +pub mod response; diff --git a/apps/api/src/routes/api/restricted/nginx/upstream/info/response.rs b/apps/api/src/routes/api/restricted/nginx/upstream/info/response.rs new file mode 100644 index 0000000..58d744d --- /dev/null +++ b/apps/api/src/routes/api/restricted/nginx/upstream/info/response.rs @@ -0,0 +1,129 @@ +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +use crate::routes::api::helper::pagination::PaginationInfo; + +#[derive(Serialize, Deserialize, utoipa::ToSchema)] +pub struct UpstreamTargetInfo { + pub id: uuid::Uuid, + pub target_host: String, + pub target_port: i64, + pub enabled: bool, + pub is_backup: bool, + pub weight: i32, + // + pub created_at: DateTime, + pub updated_at: DateTime, + // + pub upstream_id: Uuid, + pub upstream: Option, +} + +#[derive(Serialize, Deserialize, utoipa::ToSchema)] +pub struct UpstreamBasicInfo { + pub id: Uuid, + pub name: String, + pub protocol: String, + // + pub created_at: DateTime, + pub updated_at: DateTime, +} + +impl From + for UpstreamTargetInfo +{ + fn from(info: crate::services::nginx::info::upstream_target::UpstreamTargetInfo) -> Self { + Self { + id: info.id, + target_host: info.target_host, + target_port: info.target_port, + enabled: info.enabled, + is_backup: info.is_backup, + weight: info.weight as i32, + // + created_at: info.created_at, + updated_at: info.updated_at, + // + upstream_id: info.upstream_id, + upstream: info.upstream.map(|u| UpstreamBasicInfo { + id: u.id, + name: u.name, + protocol: u.protocol, + created_at: u.created_at, + updated_at: u.updated_at, + }), + } + } +} + +#[derive(Serialize, Deserialize, utoipa::ToSchema)] +pub struct UpstreamTargetBasicInfo { + pub id: uuid::Uuid, + pub target_host: String, + pub target_port: i64, + pub enabled: bool, + pub is_backup: bool, + pub weight: i32, + // + pub created_at: DateTime, + pub updated_at: DateTime, +} + +impl From + for UpstreamTargetBasicInfo +{ + fn from(info: crate::services::nginx::info::upstream_target::UpstreamTargetInfo) -> Self { + Self { + id: info.id, + target_host: info.target_host, + target_port: info.target_port, + enabled: info.enabled, + is_backup: info.is_backup, + weight: info.weight as i32, + // + created_at: info.created_at, + updated_at: info.updated_at, + } + } +} + +#[derive(Serialize, Deserialize, utoipa::ToSchema)] +pub struct UpstreamInfoResponse { + pub id: uuid::Uuid, + pub name: String, + pub protocol: String, + pub algorithm: String, + pub sticky_session: bool, + pub created_by: Option, + pub created_at: DateTime, + pub updated_at: DateTime, + // + pub upstream_targets: Vec, +} + +impl From for UpstreamInfoResponse { + fn from(info: crate::services::nginx::info::upstream::UpstreamInfo) -> Self { + Self { + id: info.id, + name: info.name, + protocol: info.protocol, + algorithm: info.algorithm, + sticky_session: info.sticky_session, + created_by: info.created_by, + created_at: info.created_at, + updated_at: info.updated_at, + upstream_targets: info + .upstream_targets + .into_iter() + .map(|t| t.into()) + .collect(), + } + } +} + +#[derive(Serialize, Deserialize, utoipa::ToSchema)] +pub struct UpstreamListResponse { + pub items: Vec, + pub pagination: PaginationInfo, +}