feat: implement update handlers for upstream and upstream target management

This commit is contained in:
GW_MC
2025-12-30 15:09:49 +08:00
parent b43f9fcb00
commit f4db47daf2
7 changed files with 555 additions and 35 deletions

View File

@@ -2,6 +2,7 @@ use chrono::{DateTime, Utc};
use optfield::optfield;
use database::generated::entities::{upstream, upstream_target};
use sea_orm::ActiveValue::{Set, Unchanged};
use uuid::Uuid;
use crate::{
@@ -13,15 +14,14 @@ use crate::{
set_if_some,
};
#[optfield(pub UpdateUpstreamInfo)]
#[derive(Clone)]
pub struct UpstreamInfo {
pub id: uuid::Uuid,
pub id: Uuid,
pub name: String,
pub protocol: String,
pub algorithm: String,
pub sticky_session: bool,
pub created_by: Option<uuid::Uuid>,
pub created_by: Option<Uuid>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
//
@@ -33,11 +33,21 @@ pub struct UpstreamCreateInfo {
pub protocol: String,
pub algorithm: String,
pub sticky_session: bool,
pub created_by: Option<uuid::Uuid>,
pub created_by: Option<Uuid>,
//
pub upstream_targets: Vec<upstream_target_info::UpstreamTargetCreateInfo>,
}
#[derive(Clone)]
pub struct UpdateUpstreamInfo {
pub name: Option<String>,
pub protocol: Option<String>,
pub algorithm: Option<String>,
pub sticky_session: Option<bool>,
//
pub upstream_targets: Option<Vec<(Uuid, bool)>>,
}
impl NginxConfigProvider for UpstreamInfo {
fn to_nginx_config(&self, indent: Option<usize>) -> String {
let targets_config: Vec<String> = self
@@ -142,18 +152,14 @@ impl From<UpstreamInfo> for (upstream::ActiveModel, Vec<upstream_target::ActiveM
impl UpdateUpstreamInfo {
pub fn apply_to_model(self, current_model: upstream::Model) -> upstream::ActiveModel {
upstream::ActiveModel {
id: sea_orm::ActiveValue::Unchanged(current_model.id),
id: Unchanged(current_model.id),
name: set_if_some!(self.name),
protocol: set_if_some!(self.protocol),
algorithm: set_if_some!(self.algorithm),
sticky_session: set_if_some!(self.sticky_session),
created_by: set_if_some!(if self.created_by.is_some() {
Some(self.created_by)
} else {
None
}),
created_at: set_if_some!(self.created_at),
updated_at: set_if_some!(self.updated_at),
created_by: Unchanged(current_model.created_by),
created_at: Unchanged(current_model.created_at),
updated_at: Set(chrono::Utc::now()),
}
}
}

View File

@@ -1,5 +1,4 @@
use chrono::{DateTime, Utc};
use optfield::optfield;
use sea_orm::ActiveValue::{Set, Unchanged};
use uuid::Uuid;
@@ -11,7 +10,6 @@ use crate::{
set_if_some,
};
#[optfield(pub UpdateUpstreamTargetInfo)]
#[derive(Clone)]
pub struct UpstreamTargetInfo {
pub id: uuid::Uuid,
@@ -27,6 +25,15 @@ pub struct UpstreamTargetInfo {
pub upstream: Option<UpstreamBasicInfo>,
}
#[derive(Clone)]
pub struct UpdateUpstreamTargetInfo {
pub target_host: Option<String>,
pub target_port: Option<i64>,
pub weight: Option<i64>,
pub is_backup: Option<bool>,
pub enabled: Option<bool>,
}
#[derive(Clone)]
pub struct UpstreamBasicInfo {
pub id: uuid::Uuid,
@@ -146,9 +153,9 @@ impl UpdateUpstreamTargetInfo {
weight: set_if_some!(self.weight),
is_backup: set_if_some!(self.is_backup),
enabled: set_if_some!(self.enabled),
created_at: set_if_some!(self.created_at),
updated_at: set_if_some!(self.updated_at),
upstream_id: set_if_some!(self.upstream_id),
created_at: Unchanged(current_model.created_at),
updated_at: Set(chrono::Utc::now()),
upstream_id: Unchanged(current_model.upstream_id),
}
}
}