feature/upstream-service #13
@@ -1,7 +1,7 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
Extension, Json,
|
Json,
|
||||||
extract::State,
|
extract::State,
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::{IntoResponse, Response},
|
response::{IntoResponse, Response},
|
||||||
|
|||||||
@@ -36,7 +36,8 @@ impl NginxConfigProvider for NginxConfigBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add other sections like servers, locations, etc.
|
// TODO: Add other sections like servers, locations, etc.
|
||||||
|
// trailing newline for file ending
|
||||||
|
config.push('\n');
|
||||||
config
|
config
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use chrono::{DateTime, Utc};
|
|||||||
|
|
||||||
use database::generated::entities::{upstream, upstream_target};
|
use database::generated::entities::{upstream, upstream_target};
|
||||||
use sea_orm::ActiveValue::{Set, Unchanged};
|
use sea_orm::ActiveValue::{Set, Unchanged};
|
||||||
|
use tracing::warn;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -13,7 +14,7 @@ use crate::{
|
|||||||
set_if_some,
|
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)]
|
#[derive(Clone)]
|
||||||
pub struct UpstreamInfo {
|
pub struct UpstreamInfo {
|
||||||
@@ -56,18 +57,53 @@ impl NginxConfigProvider for UpstreamInfo {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|target| target.to_nginx_config(Some(indent.unwrap_or(0) + INDENT_SIZE)))
|
.map(|target| target.to_nginx_config(Some(indent.unwrap_or(0) + INDENT_SIZE)))
|
||||||
.collect();
|
.collect();
|
||||||
let mut targets_config_str = if targets_config.is_empty() {
|
|
||||||
|
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 * 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()
|
PLACEHOLDER_TARGET.to_string()
|
||||||
} else {
|
} else {
|
||||||
let mut r = targets_config.join("\n");
|
// normal targets
|
||||||
r.push('\n');
|
targets_config.join("\n")
|
||||||
r
|
|
||||||
}
|
}
|
||||||
.indent(indent.unwrap_or(0) + INDENT_SIZE);
|
.indent(indent.unwrap_or(0) + INDENT_SIZE)),
|
||||||
|
);
|
||||||
|
|
||||||
|
// add placeholder if all targets are backup
|
||||||
if self.upstream_targets.iter().all(|v| v.is_backup) {
|
if self.upstream_targets.iter().all(|v| v.is_backup) {
|
||||||
|
targets_config_str.push('\n');
|
||||||
targets_config_str
|
targets_config_str
|
||||||
.push_str(&PLACEHOLDER_TARGET.indent(indent.unwrap_or(0) + INDENT_SIZE));
|
.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))
|
format!("upstream {} {{\n{}\n}}", self.name, targets_config_str).indent(indent.unwrap_or(0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user