From 545bc66f8c412f558485d9e57026d9ebefbad6b2 Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Wed, 31 Dec 2025 19:17:14 +0800 Subject: [PATCH] Fix: invalid config when all are backup --- apps/api/src/services/nginx/info/upstream.rs | 25 +++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/apps/api/src/services/nginx/info/upstream.rs b/apps/api/src/services/nginx/info/upstream.rs index 17dcdf2..f5590b3 100644 --- a/apps/api/src/services/nginx/info/upstream.rs +++ b/apps/api/src/services/nginx/info/upstream.rs @@ -13,6 +13,8 @@ use crate::{ set_if_some, }; +const PLACEHOLDER_TARGET: &str = "server 127.0.0.1:65535 down; # placeholder target\n"; + #[derive(Clone)] pub struct UpstreamInfo { pub id: Uuid, @@ -54,12 +56,18 @@ impl NginxConfigProvider for UpstreamInfo { .iter() .map(|target| target.to_nginx_config(Some(indent.unwrap_or(0) + INDENT_SIZE))) .collect(); - let targets_config_str = if targets_config.is_empty() { - "server 127.0.0.1:65535 down; # placeholder target".to_string() + let mut targets_config_str = if targets_config.is_empty() { + PLACEHOLDER_TARGET.to_string() } else { - targets_config.join("\n") + let mut r = targets_config.join("\n"); + r.push('\n'); + r } .indent(indent.unwrap_or(0) + INDENT_SIZE); + if self.upstream_targets.iter().all(|v| v.is_backup) { + targets_config_str + .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)) } } @@ -106,6 +114,17 @@ impl From for UpstreamInfo { } } +impl From<(upstream::Model, Option>)> for UpstreamInfo { + fn from(data: (upstream::Model, Option>)) -> Self { + let (upstream_model, upstream_target_models) = data; + if let Some(targets) = upstream_target_models { + UpstreamInfo::from((upstream_model, targets)) + } else { + UpstreamInfo::from(upstream_model) + } + } +} + impl From<(upstream::Model, Vec)> for UpstreamInfo { fn from(data: (upstream::Model, Vec)) -> Self { let (upstream_model, upstream_target_models) = data;