feature/upstream-service #13

Merged
GW_MC merged 43 commits from feature/upstream-service into master 2026-01-01 10:49:32 +08:00
3 changed files with 47 additions and 10 deletions
Showing only changes of commit 3be9ecc4c1 - Show all commits

View File

@@ -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},

View File

@@ -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
} }
} }

View File

@@ -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() {
PLACEHOLDER_TARGET.to_string() let mut targets_config_str = {
} else { let config_str = match self.algorithm.as_str() {
let mut r = targets_config.join("\n"); "least-conn" => "least_conn",
r.push('\n'); "ip-hash" => "ip_hash",
r "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) { 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))
} }
} }