feature/upstream-service #13
@@ -1,7 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
Extension, Json,
|
||||
Json,
|
||||
extract::State,
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
|
||||
@@ -36,7 +36,8 @@ impl NginxConfigProvider for NginxConfigBuilder {
|
||||
}
|
||||
|
||||
// TODO: Add other sections like servers, locations, etc.
|
||||
|
||||
// trailing newline for file ending
|
||||
config.push('\n');
|
||||
config
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ use chrono::{DateTime, Utc};
|
||||
|
||||
use database::generated::entities::{upstream, upstream_target};
|
||||
use sea_orm::ActiveValue::{Set, Unchanged};
|
||||
use tracing::warn;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
@@ -13,7 +14,7 @@ use crate::{
|
||||
set_if_some,
|
||||
};
|
||||
|
||||
const PLACEHOLDER_TARGET: &str = "server 127.0.0.1:65535 down; # placeholder target\n";
|
||||
const PLACEHOLDER_TARGET: &str = "server 127.0.0.1:65535 down; # placeholder target";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct UpstreamInfo {
|
||||
@@ -56,18 +57,53 @@ impl NginxConfigProvider for UpstreamInfo {
|
||||
.iter()
|
||||
.map(|target| target.to_nginx_config(Some(indent.unwrap_or(0) + INDENT_SIZE)))
|
||||
.collect();
|
||||
let mut targets_config_str = if targets_config.is_empty() {
|
||||
PLACEHOLDER_TARGET.to_string()
|
||||
} else {
|
||||
let mut r = targets_config.join("\n");
|
||||
r.push('\n');
|
||||
r
|
||||
|
||||
let mut targets_config_str = {
|
||||
let config_str = match self.algorithm.as_str() {
|
||||
"least-conn" => "least_conn",
|
||||
"ip-hash" => "ip_hash",
|
||||
"round-robin" => "",
|
||||
v => {
|
||||
// TODO: allow arbitrary algorithms via config extensions/plugins
|
||||
warn!(
|
||||
"Unknown upstream algorithm '{}', defaulting to 'round-robin'",
|
||||
v
|
||||
);
|
||||
""
|
||||
}
|
||||
}
|
||||
.to_string();
|
||||
// TODO: add support for sticky session / checking for nginx sticky module existence
|
||||
// if self.sticky_session {
|
||||
// config_str.push_str("sticky")
|
||||
// }
|
||||
if config_str.trim().is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
config_str + ";"
|
||||
}
|
||||
}
|
||||
.indent(indent.unwrap_or(0) + INDENT_SIZE);
|
||||
.indent(indent.unwrap_or(0) + INDENT_SIZE * 2);
|
||||
targets_config_str.push('\n');
|
||||
|
||||
targets_config_str.push_str(
|
||||
&(if targets_config.is_empty() {
|
||||
// add placeholder if no targets
|
||||
PLACEHOLDER_TARGET.to_string()
|
||||
} else {
|
||||
// normal targets
|
||||
targets_config.join("\n")
|
||||
}
|
||||
.indent(indent.unwrap_or(0) + INDENT_SIZE)),
|
||||
);
|
||||
|
||||
// add placeholder if all targets are backup
|
||||
if self.upstream_targets.iter().all(|v| v.is_backup) {
|
||||
targets_config_str.push('\n');
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user