- Added transaction support in `add_upstream_target`, `remove_upstream`, `remove_upstream_target`, `update_upstream`, and `update_upstream_target` functions to ensure atomicity of operations. - Updated the `NginxService` to include methods for validating and applying configurations using the agent service. - Enhanced error handling in agent service interactions, returning appropriate internal server errors when agent communication fails. - Introduced mock agent service for testing, allowing for simulation of agent interactions without actual network calls. - Refactored tests to cover scenarios where agent operations fail, ensuring that internal server errors are returned as expected.
43 lines
1.0 KiB
Rust
43 lines
1.0 KiB
Rust
use crate::services::nginx::info::upstream::UpstreamInfo;
|
|
|
|
pub const INDENT_SIZE: usize = 2;
|
|
|
|
pub trait NginxConfigProvider {
|
|
fn to_nginx_config(&self, indent: Option<usize>) -> String;
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub struct NginxConfigBuilder {
|
|
upstreams: Vec<UpstreamInfo>,
|
|
}
|
|
|
|
impl NginxConfigBuilder {
|
|
pub fn add_upstream(&mut self, upstream: UpstreamInfo) {
|
|
self.upstreams.push(upstream);
|
|
}
|
|
|
|
pub fn add_upstreams(&mut self, upstreams: Vec<UpstreamInfo>) {
|
|
for upstream in upstreams {
|
|
self.add_upstream(upstream);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl NginxConfigProvider for NginxConfigBuilder {
|
|
fn to_nginx_config(&self, indent: Option<usize>) -> String {
|
|
let mut config = format!(
|
|
"# Nginx Config Generated by YANPM at {}",
|
|
chrono::Utc::now()
|
|
);
|
|
|
|
for upstream in &self.upstreams {
|
|
config.push('\n');
|
|
config.push_str(&upstream.to_nginx_config(indent));
|
|
}
|
|
|
|
// TODO: Add other sections like servers, locations, etc.
|
|
|
|
config
|
|
}
|
|
}
|