feature/upstream-service #13
@@ -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<AppState>) -> 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)
|
||||
}
|
||||
|
||||
@@ -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<bool>,
|
||||
}
|
||||
|
||||
pub struct ConcreteGetUpstreamTargetsParams {
|
||||
pub include_upstream: bool,
|
||||
}
|
||||
|
||||
impl From<GetUpstreamTargetsParams> 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<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
//
|
||||
pub upstream_id: Uuid,
|
||||
pub upstream: Option<UpstreamBasicInfo>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, utoipa::ToSchema)]
|
||||
pub struct UpstreamBasicInfo {
|
||||
pub id: uuid::Uuid,
|
||||
pub name: String,
|
||||
pub protocol: String,
|
||||
//
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl From<crate::services::nginx::info::upstream_target::UpstreamTargetInfo>
|
||||
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<Uuid>,
|
||||
Query(params): Query<GetUpstreamTargetsParams>,
|
||||
State(_state): State<Arc<AppState>>,
|
||||
) -> AxumResult<Json<UpstreamTargetInfo>, 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()))
|
||||
}
|
||||
Reference in New Issue
Block a user