Fix: create location support for proxy pass

This commit is contained in:
GW_MC
2026-01-12 11:41:55 +08:00
parent 7ae76f622c
commit b4a36dbe4c
3 changed files with 335 additions and 19 deletions

View File

@@ -17,11 +17,36 @@ use crate::{
};
#[derive(serde::Deserialize, utoipa::ToSchema, serde::Serialize)]
pub struct CreateLocationRequestBody {
#[serde(untagged)]
pub enum CreateLocationRequestBody {
// #[serde(rename = "upstream_id")]
UpstreamId(CreateLocationRequestBodyByUpstreamId),
// #[serde(rename = "proxy_pass")]
ProxyPass(CreateLocationRequestBodyByProxyPass),
}
#[derive(serde::Deserialize, utoipa::ToSchema, serde::Serialize)]
pub struct CreateLocationRequestBodyByUpstreamId {
pub path: String,
pub match_type: String,
pub order: i64,
pub upstream_id: Option<uuid::Uuid>,
pub upstream_id: uuid::Uuid,
pub preserve_host_header: Option<bool>,
pub allowed_methods: Option<Vec<String>>,
pub custom_config: Option<String>,
}
#[derive(serde::Deserialize, utoipa::ToSchema, serde::Serialize)]
pub struct CreateLocationRequestBodyByProxyPass {
pub path: String,
pub match_type: String,
pub order: i64,
pub proxy_pass_protocol: String,
pub proxy_pass_host: String,
pub proxy_pass_port: i64,
pub preserve_host_header: Option<bool>,
pub allowed_methods: Option<Vec<String>>,
pub custom_config: Option<String>,
}
#[cfg(test)]
@@ -37,8 +62,9 @@ mod tests {
use crate::{
configs::{FromConfig, ProgramSettings},
middlewares::require_auth::mock::REQUEST_AUTH_USER_INVALID_HEADER,
routes::api::restricted::nginx::proxy_host::create_location::CreateLocationRequestBody,
routes::api::restricted::nginx::proxy_host::get_proxy_router,
routes::api::restricted::nginx::proxy_host::{
create_location::CreateLocationRequestBodyByUpstreamId, get_proxy_router,
},
services::{agent_client::MockAgentService, get_mock_app_service},
};
@@ -117,11 +143,14 @@ mod tests {
let router = get_router_with_state(db.clone());
let server = TestServer::new(router).expect("failed to create test server");
let payload = CreateLocationRequestBody {
let payload = CreateLocationRequestBodyByUpstreamId {
path: "/".to_string(),
match_type: "prefix".to_string(),
order: 1,
upstream_id: None,
upstream_id: up_id,
preserve_host_header: None,
allowed_methods: None,
custom_config: None,
};
let res = server
@@ -152,11 +181,14 @@ mod tests {
let router = get_router_with_state(db.clone());
let server = TestServer::new(router).expect("failed to create test server");
let payload = CreateLocationRequestBody {
let payload = CreateLocationRequestBodyByUpstreamId {
path: "/".to_string(),
match_type: "prefix".to_string(),
order: 1,
upstream_id: None,
upstream_id: uuid::Uuid::new_v4(),
preserve_host_header: None,
allowed_methods: None,
custom_config: None,
};
let res = server
@@ -171,18 +203,46 @@ mod tests {
impl From<(uuid::Uuid, CreateLocationRequestBody)> for CreateLocationInfo {
fn from(val: (uuid::Uuid, CreateLocationRequestBody)) -> Self {
match val.1 {
CreateLocationRequestBody::UpstreamId(body) => Self::from((val.0, body)),
CreateLocationRequestBody::ProxyPass(body) => Self::from((val.0, body)),
}
}
}
impl From<(uuid::Uuid, CreateLocationRequestBodyByUpstreamId)> for CreateLocationInfo {
fn from((proxy_id, payload): (uuid::Uuid, CreateLocationRequestBodyByUpstreamId)) -> Self {
Self {
host_id: val.0,
path: val.1.path,
match_type: val.1.match_type,
order: val.1.order,
upstream_id: val.1.upstream_id,
host_id: proxy_id,
path: payload.path,
match_type: payload.match_type,
order: payload.order,
upstream_id: Some(payload.upstream_id),
proxy_pass_protocol: None,
proxy_pass_host: None,
proxy_pass_port: None,
preserve_host_header: None,
allowed_methods: None,
custom_config: None,
preserve_host_header: payload.preserve_host_header,
allowed_methods: payload.allowed_methods,
custom_config: payload.custom_config,
enabled: true,
}
}
}
impl From<(uuid::Uuid, CreateLocationRequestBodyByProxyPass)> for CreateLocationInfo {
fn from((proxy_id, payload): (uuid::Uuid, CreateLocationRequestBodyByProxyPass)) -> Self {
Self {
host_id: proxy_id,
path: payload.path,
match_type: payload.match_type,
order: payload.order,
upstream_id: None,
proxy_pass_protocol: Some(payload.proxy_pass_protocol),
proxy_pass_host: Some(payload.proxy_pass_host),
proxy_pass_port: Some(payload.proxy_pass_port),
preserve_host_header: payload.preserve_host_header,
allowed_methods: payload.allowed_methods,
custom_config: payload.custom_config,
enabled: true,
}
}

View File

@@ -895,13 +895,41 @@
}
},
"CreateLocationRequestBody": {
"oneOf": [
{
"$ref": "#/components/schemas/CreateLocationRequestBodyByUpstreamId"
},
{
"$ref": "#/components/schemas/CreateLocationRequestBodyByProxyPass"
}
]
},
"CreateLocationRequestBodyByProxyPass": {
"type": "object",
"required": [
"path",
"match_type",
"order"
"order",
"proxy_pass_protocol",
"proxy_pass_host",
"proxy_pass_port"
],
"properties": {
"allowed_methods": {
"type": [
"array",
"null"
],
"items": {
"type": "string"
}
},
"custom_config": {
"type": [
"string",
"null"
]
},
"match_type": {
"type": "string"
},
@@ -912,11 +940,66 @@
"path": {
"type": "string"
},
"upstream_id": {
"preserve_host_header": {
"type": [
"boolean",
"null"
]
},
"proxy_pass_host": {
"type": "string"
},
"proxy_pass_port": {
"type": "integer",
"format": "int64"
},
"proxy_pass_protocol": {
"type": "string"
}
}
},
"CreateLocationRequestBodyByUpstreamId": {
"type": "object",
"required": [
"path",
"match_type",
"order",
"upstream_id"
],
"properties": {
"allowed_methods": {
"type": [
"array",
"null"
],
"items": {
"type": "string"
}
},
"custom_config": {
"type": [
"string",
"null"
],
]
},
"match_type": {
"type": "string"
},
"order": {
"type": "integer",
"format": "int64"
},
"path": {
"type": "string"
},
"preserve_host_header": {
"type": [
"boolean",
"null"
]
},
"upstream_id": {
"type": "string",
"format": "uuid"
}
}