feat: implement Nginx service with upstream management and configuration generation
This commit is contained in:
118
apps/api/src/services/nginx/info/upstream_target.rs
Normal file
118
apps/api/src/services/nginx/info/upstream_target.rs
Normal file
@@ -0,0 +1,118 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use optfield::optfield;
|
||||
|
||||
use sea_orm::ActiveValue::{Set, Unchanged};
|
||||
use uuid::Uuid;
|
||||
|
||||
use database::generated::entities::upstream_target;
|
||||
|
||||
use crate::{
|
||||
services::nginx::{builder::NginxConfigProvider, traits::indentable::Indentable},
|
||||
set_if_some,
|
||||
};
|
||||
|
||||
#[optfield(pub UpdateUpstreamTargetInfo)]
|
||||
#[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 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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>) -> String {
|
||||
format!(
|
||||
"{}:{} 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: set_if_some!(self.created_at),
|
||||
updated_at: set_if_some!(self.updated_at),
|
||||
upstream_id: set_if_some!(self.upstream_id),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user