163 lines
4.9 KiB
Rust
163 lines
4.9 KiB
Rust
use chrono::{DateTime, Utc};
|
|
|
|
use sea_orm::ActiveValue::{Set, Unchanged};
|
|
use uuid::Uuid;
|
|
|
|
use database::generated::entities::{upstream, upstream_target};
|
|
|
|
use crate::{
|
|
errors::service_error::ServiceError,
|
|
services::nginx::{builder::NginxConfigProvider, traits::indentable::Indentable},
|
|
set_if_some,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct UpstreamTargetInfo {
|
|
pub id: uuid::Uuid,
|
|
pub target_host: String,
|
|
pub target_port: i64,
|
|
pub weight: i64,
|
|
pub is_backup: bool,
|
|
pub enabled: bool,
|
|
pub created_at: DateTime<Utc>,
|
|
pub updated_at: DateTime<Utc>,
|
|
//
|
|
pub upstream_id: uuid::Uuid,
|
|
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,
|
|
pub name: String,
|
|
pub protocol: String,
|
|
//
|
|
pub created_at: DateTime<Utc>,
|
|
pub updated_at: DateTime<Utc>,
|
|
}
|
|
|
|
pub struct UpstreamTargetCreateInfo {
|
|
pub target_host: String,
|
|
pub target_port: i64,
|
|
pub weight: i64,
|
|
pub is_backup: bool,
|
|
pub enabled: bool,
|
|
//
|
|
pub upstream_id: uuid::Uuid,
|
|
}
|
|
|
|
impl From<upstream_target::Model> for UpstreamTargetInfo {
|
|
fn from(model: upstream_target::Model) -> Self {
|
|
Self {
|
|
id: model.id,
|
|
target_host: model.target_host,
|
|
target_port: model.target_port,
|
|
weight: model.weight,
|
|
is_backup: model.is_backup,
|
|
enabled: model.enabled,
|
|
created_at: model.created_at,
|
|
updated_at: model.updated_at,
|
|
upstream_id: model.upstream_id,
|
|
upstream: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<(upstream_target::Model, upstream::Model)> for UpstreamTargetInfo {
|
|
fn from(data: (upstream_target::Model, upstream::Model)) -> Self {
|
|
let (target_model, up_model) = data;
|
|
Self {
|
|
id: target_model.id,
|
|
target_host: target_model.target_host,
|
|
target_port: target_model.target_port,
|
|
weight: target_model.weight,
|
|
is_backup: target_model.is_backup,
|
|
enabled: target_model.enabled,
|
|
created_at: target_model.created_at,
|
|
updated_at: target_model.updated_at,
|
|
upstream_id: target_model.upstream_id,
|
|
upstream: Some(UpstreamBasicInfo {
|
|
id: up_model.id,
|
|
name: up_model.name,
|
|
protocol: up_model.protocol,
|
|
created_at: up_model.created_at,
|
|
updated_at: up_model.updated_at,
|
|
}),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<UpstreamTargetInfo> for upstream_target::ActiveModel {
|
|
fn from(val: UpstreamTargetInfo) -> Self {
|
|
upstream_target::ActiveModel {
|
|
id: Set(val.id),
|
|
target_host: Set(val.target_host),
|
|
target_port: Set(val.target_port),
|
|
weight: Set(val.weight),
|
|
is_backup: Set(val.is_backup),
|
|
enabled: Set(val.enabled),
|
|
created_at: Set(val.created_at),
|
|
updated_at: Set(val.updated_at),
|
|
upstream_id: Set(val.upstream_id),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<UpstreamTargetCreateInfo> for upstream_target::ActiveModel {
|
|
fn from(val: UpstreamTargetCreateInfo) -> Self {
|
|
upstream_target::ActiveModel {
|
|
id: Set(Uuid::new_v4()),
|
|
target_host: Set(val.target_host),
|
|
target_port: Set(val.target_port),
|
|
weight: Set(val.weight),
|
|
is_backup: Set(val.is_backup),
|
|
enabled: Set(val.enabled),
|
|
created_at: Set(chrono::Utc::now()),
|
|
updated_at: Set(chrono::Utc::now()),
|
|
upstream_id: Set(val.upstream_id),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl NginxConfigProvider for UpstreamTargetInfo {
|
|
fn to_nginx_config(&self, indent: Option<usize>) -> Result<String, ServiceError> {
|
|
Ok(format!(
|
|
"server {}:{} weight={}{}{};",
|
|
self.target_host,
|
|
self.target_port,
|
|
self.weight,
|
|
if self.is_backup { " backup" } else { "" },
|
|
if !self.enabled { " down" } else { "" },
|
|
)
|
|
.indent(indent.unwrap_or(0)))
|
|
}
|
|
}
|
|
|
|
impl UpdateUpstreamTargetInfo {
|
|
pub fn apply_to_model(
|
|
self,
|
|
current_model: upstream_target::Model,
|
|
) -> upstream_target::ActiveModel {
|
|
upstream_target::ActiveModel {
|
|
id: Unchanged(current_model.id),
|
|
target_host: set_if_some!(self.target_host),
|
|
target_port: set_if_some!(self.target_port),
|
|
weight: set_if_some!(self.weight),
|
|
is_backup: set_if_some!(self.is_backup),
|
|
enabled: set_if_some!(self.enabled),
|
|
created_at: Unchanged(current_model.created_at),
|
|
updated_at: Set(chrono::Utc::now()),
|
|
upstream_id: Unchanged(current_model.upstream_id),
|
|
}
|
|
}
|
|
}
|