diff --git a/apps/api/src/routes/api/restricted/nginx/upstream.rs b/apps/api/src/routes/api/restricted/nginx/upstream.rs index 1b1f797..14e18fd 100644 --- a/apps/api/src/routes/api/restricted/nginx/upstream.rs +++ b/apps/api/src/routes/api/restricted/nginx/upstream.rs @@ -1,4 +1,5 @@ pub mod get_upstream; +pub mod get_upstream_target; use std::sync::Arc; @@ -10,5 +11,9 @@ pub fn get_upstream_router(state: Arc) -> Router { Router::new() .route("upstreams", get(get_upstream::get_upstream_list)) .route("upstreams/{upstream_id}", get(get_upstream::get_upstream)) + .route( + "upstream_targets/{upstream_target_id}", + get(get_upstream_target::get_upstream_target), + ) .with_state(state) } 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 new file mode 100644 index 0000000..bb2ac5a --- /dev/null +++ b/apps/api/src/routes/api/restricted/nginx/upstream/get_upstream_target.rs @@ -0,0 +1,99 @@ +use std::sync::Arc; + +use axum::{ + Json, + 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}; + +#[derive(Serialize, Deserialize, utoipa::ToSchema)] +pub struct GetUpstreamTargetsParams { + pub include_upstream: Option, +} + +pub struct ConcreteGetUpstreamTargetsParams { + pub include_upstream: bool, +} + +impl From for ConcreteGetUpstreamTargetsParams { + fn from(params: GetUpstreamTargetsParams) -> Self { + Self { + include_upstream: params.include_upstream.unwrap_or(false), + } + } +} + +#[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: None, + } + } +} + +pub async fn get_upstream_target( + Path(upstream_target_id): Path, + Query(params): Query, + State(_state): State>, +) -> AxumResult, ServiceError> { + let concrete_params: ConcreteGetUpstreamTargetsParams = params.into(); + let upstream_service = &_state.service.nginx.get_upstream_service(); + let upstream_target_info = upstream_service + .get_upstream_target( + upstream_target_id, + if concrete_params.include_upstream { + Some(crate::services::nginx::upstream::GetUpstreamTargetOptions { + include_upstream: true, + }) + } else { + None + }, + None, + ) + .await?; + Ok(Json(upstream_target_info.into())) +}