feat: Update NginxConfigProvider to return Result with ServiceError for upstream and upstream target

This commit is contained in:
GW_MC
2026-01-07 15:57:53 +08:00
parent 1c0053207c
commit eb1afc87cc
2 changed files with 11 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ use tracing::warn;
use uuid::Uuid; use uuid::Uuid;
use crate::{ use crate::{
errors::service_error::ServiceError,
services::nginx::{ services::nginx::{
builder::{INDENT_SIZE, NginxConfigProvider}, builder::{INDENT_SIZE, NginxConfigProvider},
info::upstream_target as upstream_target_info, info::upstream_target as upstream_target_info,
@@ -51,12 +52,12 @@ pub struct UpdateUpstreamInfo {
} }
impl NginxConfigProvider for UpstreamInfo { impl NginxConfigProvider for UpstreamInfo {
fn to_nginx_config(&self, indent: Option<usize>) -> String { fn to_nginx_config(&self, indent: Option<usize>) -> Result<String, ServiceError> {
let targets_config: Vec<String> = self let targets_config: Vec<String> = self
.upstream_targets .upstream_targets
.iter() .iter()
.map(|target| target.to_nginx_config(Some(indent.unwrap_or(0) + INDENT_SIZE))) .map(|target| target.to_nginx_config(Some(indent.unwrap_or(0) + INDENT_SIZE)))
.collect(); .collect::<Result<Vec<String>, ServiceError>>()?;
let mut targets_config_str = { let mut targets_config_str = {
let config_str = match self.algorithm.as_str() { let config_str = match self.algorithm.as_str() {
@@ -104,7 +105,10 @@ impl NginxConfigProvider for UpstreamInfo {
.push_str(&PLACEHOLDER_TARGET.indent(indent.unwrap_or(0) + INDENT_SIZE)); .push_str(&PLACEHOLDER_TARGET.indent(indent.unwrap_or(0) + INDENT_SIZE));
} }
format!("upstream {} {{\n{}\n}}", self.name, targets_config_str).indent(indent.unwrap_or(0)) Ok(
format!("upstream {} {{\n{}\n}}", self.name, targets_config_str)
.indent(indent.unwrap_or(0)),
)
} }
} }

View File

@@ -6,6 +6,7 @@ use uuid::Uuid;
use database::generated::entities::{upstream, upstream_target}; use database::generated::entities::{upstream, upstream_target};
use crate::{ use crate::{
errors::service_error::ServiceError,
services::nginx::{builder::NginxConfigProvider, traits::indentable::Indentable}, services::nginx::{builder::NginxConfigProvider, traits::indentable::Indentable},
set_if_some, set_if_some,
}; };
@@ -128,8 +129,8 @@ impl From<UpstreamTargetCreateInfo> for upstream_target::ActiveModel {
} }
impl NginxConfigProvider for UpstreamTargetInfo { impl NginxConfigProvider for UpstreamTargetInfo {
fn to_nginx_config(&self, indent: Option<usize>) -> String { fn to_nginx_config(&self, indent: Option<usize>) -> Result<String, ServiceError> {
format!( Ok(format!(
"server {}:{} weight={}{}{};", "server {}:{} weight={}{}{};",
self.target_host, self.target_host,
self.target_port, self.target_port,
@@ -137,7 +138,7 @@ impl NginxConfigProvider for UpstreamTargetInfo {
if self.is_backup { " backup" } else { "" }, if self.is_backup { " backup" } else { "" },
if !self.enabled { " down" } else { "" }, if !self.enabled { " down" } else { "" },
) )
.indent(indent.unwrap_or(0)) .indent(indent.unwrap_or(0)))
} }
} }