feat: implement Nginx service with upstream management and configuration generation
This commit is contained in:
47
apps/api/src/services/nginx/builder.rs
Normal file
47
apps/api/src/services/nginx/builder.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
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;
|
||||
}
|
||||
|
||||
pub struct NginxConfigBuilder {
|
||||
upstreams: Vec<UpstreamInfo>,
|
||||
}
|
||||
|
||||
impl NginxConfigBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
upstreams: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user