fix: update NginxSettings to be non-optional and improve validation logic

This commit is contained in:
GW_MC
2026-04-27 07:15:09 +00:00
parent 98e07715fa
commit 984334c232
2 changed files with 35 additions and 34 deletions

View File

@@ -18,7 +18,8 @@ pub struct Settings {
pub grpc: GrpcSettings, pub grpc: GrpcSettings,
#[serde(default)] #[serde(default)]
pub log: LogSettings, pub log: LogSettings,
pub nginx: Option<NginxSettings>, #[serde(default)]
pub nginx: NginxSettings,
} }
/// gRPC client settings /// gRPC client settings
@@ -79,7 +80,7 @@ impl Default for LogSettings {
} }
} }
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct NginxSettings { pub struct NginxSettings {
#[serde(default = "default_nginx_config_path")] #[serde(default = "default_nginx_config_path")]
pub nginx_config_path: String, pub nginx_config_path: String,
@@ -101,9 +102,7 @@ pub struct NginxSettings {
impl Validate for Settings { impl Validate for Settings {
fn validate(&self) -> Result<(), ValidationError> { fn validate(&self) -> Result<(), ValidationError> {
self.grpc.validate()?; self.grpc.validate()?;
if let Some(nginx) = &self.nginx { self.nginx.validate()?;
nginx.validate()?;
}
Ok(()) Ok(())
} }
} }
@@ -125,35 +124,37 @@ impl Settings {
settings.validate().map_err(ConfigError::Message)?; settings.validate().map_err(ConfigError::Message)?;
if let Some(nginx) = &mut settings.nginx { settings.nginx.validate().map_err(ConfigError::Message)?;
nginx.validate().map_err(ConfigError::Message)?;
// replace binary path template in commands with actual binary path, if the template is present // replace binary path template in commands with actual binary path, if the template is present
nginx settings
.nginx
.override_nginx_reload_command .override_nginx_reload_command
.iter_mut() .iter_mut()
.for_each(|cmd| { .for_each(|cmd| {
*cmd = cmd.replace( *cmd = cmd.replace(
NGINX_BINARY_PATH_TEMPLATE, NGINX_BINARY_PATH_TEMPLATE,
&nginx &settings
.nginx
.nginx_binary_path .nginx_binary_path
.clone() .clone()
.unwrap_or_else(|| NGINX_DEFAULT_BINARY.into()), .unwrap_or_else(|| NGINX_DEFAULT_BINARY.into()),
); );
}); });
nginx settings
.nginx
.override_nginx_test_command .override_nginx_test_command
.iter_mut() .iter_mut()
.for_each(|cmd| { .for_each(|cmd| {
*cmd = cmd.replace( *cmd = cmd.replace(
NGINX_BINARY_PATH_TEMPLATE, NGINX_BINARY_PATH_TEMPLATE,
&nginx &settings
.nginx
.nginx_binary_path .nginx_binary_path
.clone() .clone()
.unwrap_or_else(|| NGINX_DEFAULT_BINARY.into()), .unwrap_or_else(|| NGINX_DEFAULT_BINARY.into()),
); );
}); });
}
Ok(settings) Ok(settings)
} }

View File

@@ -89,7 +89,7 @@ mod tests {
cors: None, cors: None,
}, },
log: LogSettings::default(), log: LogSettings::default(),
nginx: None, nginx: Default::default(),
} }
} }