From 9ff8c0de353e7c9a44cbbb3d6398ecd12c7f26dd Mon Sep 17 00:00:00 2001 From: RWDai <27391645+RWDai@users.noreply.github.com> Date: Tue, 5 Dec 2023 15:38:29 +0800 Subject: [PATCH] update --- .gitignore | 3 ++- admin/src/api/backend_api.rs | 5 ++--- admin/src/api/gateway_api.rs | 6 +++--- admin/src/api/plugin_api.rs | 2 +- admin/src/api/tls_api.rs | 2 +- admin/src/helper.rs | 2 ++ admin/src/initializer.rs | 3 ++- admin/src/service/backend_ref_service.rs | 1 + admin/src/service/base_service.rs | 18 ++++++++++++++---- admin/src/service/plugin_service.rs | 1 + 10 files changed, 29 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index bf2046e0..874c952d 100644 --- a/.gitignore +++ b/.gitignore @@ -25,4 +25,5 @@ util/gh-pages/lints.json .DS_Store # dev script -devsh/* \ No newline at end of file +devsh/* +.uuid diff --git a/admin/src/api/backend_api.rs b/admin/src/api/backend_api.rs index b1ba579a..0f529ab3 100644 --- a/admin/src/api/backend_api.rs +++ b/admin/src/api/backend_api.rs @@ -2,9 +2,8 @@ use crate::model::query_dto::{BackendRefQueryDto, ToInstance}; use crate::model::vo::backend_vo::SgBackendRefVo; use crate::service::backend_ref_service::BackendRefVoService; use tardis::web::poem::session::Session; -use tardis::web::poem::web::Query; use tardis::web::poem_openapi; -use tardis::web::poem_openapi::param::Path; +use tardis::web::poem_openapi::param::{Path, Query}; use tardis::web::poem_openapi::payload::Json; use tardis::web::web_resp::{TardisApiResult, TardisResp, Void}; @@ -45,7 +44,7 @@ impl BackendApi { } /// delete Backend - #[oai(path = "/:backend_id", method = "put")] + #[oai(path = "/:backend_id", method = "delete")] async fn delete(&self, backend_id: Path, session: &Session) -> TardisApiResult { let client_name = &super::get_instance_name(session).await?; BackendRefVoService::delete(client_name, &backend_id.0).await?; diff --git a/admin/src/api/gateway_api.rs b/admin/src/api/gateway_api.rs index bee1731a..601be69e 100644 --- a/admin/src/api/gateway_api.rs +++ b/admin/src/api/gateway_api.rs @@ -4,7 +4,7 @@ use crate::service::gateway_service::GatewayVoService; use tardis::basic::error::TardisError; use tardis::web::poem::session::Session; use tardis::web::poem_openapi; -use tardis::web::poem_openapi::param::Query; +use tardis::web::poem_openapi::param::{Path, Query}; use tardis::web::poem_openapi::payload::Json; use tardis::web::web_resp::{TardisApiResult, TardisResp, Void}; @@ -54,8 +54,8 @@ impl GatewayApi { } /// Delete Gateway - #[oai(path = "/", method = "delete")] - async fn delete(&self, name: Query, session: &Session) -> TardisApiResult { + #[oai(path = "/:plugin_id", method = "delete")] + async fn delete(&self, name: Path, session: &Session) -> TardisApiResult { let client_name = &super::get_instance_name(session).await?; GatewayVoService::delete(client_name, &name.0).await?; TardisResp::ok(Void {}) diff --git a/admin/src/api/plugin_api.rs b/admin/src/api/plugin_api.rs index 622bbc10..576e29bd 100644 --- a/admin/src/api/plugin_api.rs +++ b/admin/src/api/plugin_api.rs @@ -59,7 +59,7 @@ impl PluginApi { } /// Delete Plugin - #[oai(path = "/:plugin_id", method = "put")] + #[oai(path = "/:plugin_id", method = "delete")] async fn delete(&self, plugin_id: Path, session: &Session) -> TardisApiResult { let client_name = &super::get_instance_name(session).await?; PluginVoService::delete(client_name, &plugin_id.0).await?; diff --git a/admin/src/api/tls_api.rs b/admin/src/api/tls_api.rs index 917a944e..c318d090 100644 --- a/admin/src/api/tls_api.rs +++ b/admin/src/api/tls_api.rs @@ -45,7 +45,7 @@ impl TlsApi { } /// Delete Tls - #[oai(path = "/:backend_id", method = "put")] + #[oai(path = "/:backend_id", method = "delete")] async fn delete(&self, tls_config_id: Path, session: &Session) -> TardisApiResult { let client_name = &super::get_instance_name(session).await?; TlsVoService::delete(client_name, &tls_config_id.0).await?; diff --git a/admin/src/helper.rs b/admin/src/helper.rs index 7504cade..66abbc20 100644 --- a/admin/src/helper.rs +++ b/admin/src/helper.rs @@ -41,7 +41,9 @@ mod test { fn test_fuzzy_regex() { assert!(fuzzy_regex("*").unwrap().is_match("8435")); assert!(fuzzy_regex("*").unwrap().is_match("8435*erf")); + assert!(!fuzzy_regex("").unwrap().is_match("dsfasd")); + assert!(!fuzzy_regex("a").unwrap().is_match("sdfa435gt")); assert!(fuzzy_regex("a*").unwrap().is_match("a435gt")); assert!(!fuzzy_regex("a*").unwrap().is_match("sdfa435gt")); diff --git a/admin/src/initializer.rs b/admin/src/initializer.rs index 8b48dd77..4135cce6 100644 --- a/admin/src/initializer.rs +++ b/admin/src/initializer.rs @@ -1,4 +1,4 @@ -use crate::api::{auth_api, gateway_api, plugin_api, route_api, spacegate_manage_api, tls_api, BasicAuth, CookieMW}; +use crate::api::{auth_api, backend_api, gateway_api, plugin_api, route_api, spacegate_manage_api, tls_api, BasicAuth, CookieMW}; use crate::client::init_client; use crate::constants::DOMAIN_CODE; @@ -16,6 +16,7 @@ pub(crate) async fn init(web_server: &TardisWebServer) -> TardisResult<()> { async fn init_api(web_server: &TardisWebServer) -> TardisResult<()> { let module = WebServerModule::from(( + backend_api::BackendApi, gateway_api::GatewayApi, plugin_api::PluginApi, route_api::HttprouteApi, diff --git a/admin/src/service/backend_ref_service.rs b/admin/src/service/backend_ref_service.rs index 499aebdc..b64decf3 100644 --- a/admin/src/service/backend_ref_service.rs +++ b/admin/src/service/backend_ref_service.rs @@ -36,6 +36,7 @@ impl BackendRefVoService { Self::add_vo(client_name, add).await } pub(crate) async fn update(client_name: &str, update: SgBackendRefVo) -> TardisResult { + //todo update route Self::update_vo(client_name, update).await } diff --git a/admin/src/service/base_service.rs b/admin/src/service/base_service.rs index 02397a78..790a9540 100644 --- a/admin/src/service/base_service.rs +++ b/admin/src/service/base_service.rs @@ -66,20 +66,26 @@ where where T: 'async_trait, { - Self::add_or_update_vo(client_name, config, true).await + Self::add_or_update_vo(client_name, config, true, false).await } async fn update_vo(client_name: &str, config: T) -> TardisResult where T: 'async_trait, { - Self::add_or_update_vo(client_name, config, false).await + Self::add_or_update_vo(client_name, config, false, true).await } - async fn add_or_update_vo(client_name: &str, config: T, add_only: bool) -> TardisResult + /// # add_or_update_vo + /// **warning**: `add_only` and `update_only` cannot be true at the same time + async fn add_or_update_vo(client_name: &str, config: T, add_only: bool, update_only: bool) -> TardisResult where T: 'async_trait, { + if add_only && update_only { + panic!("add_only and update_only cannot be true at the same time"); + } + let id = config.get_unique_name(); let mut datas = Self::get_str_type_map(client_name).await?; if datas.get(&id).is_some() { @@ -89,7 +95,11 @@ where log::debug!("[SG.admin] add_or_update {}:{} exists , will update", T::get_vo_type(), id); } } else { - log::debug!("[SG.admin] add_or_update {}:{} not exists , will add", T::get_vo_type(), id); + if update_only { + return Err(TardisError::bad_request(&format!("[SG.admin] {}:{} not exists", T::get_vo_type(), id), "")); + } else { + log::debug!("[SG.admin] add_or_update {}:{} not exists , will add", T::get_vo_type(), id); + } } let config_str = serde_json::to_string(&config).map_err(|e| TardisError::bad_request(&format!("Serialization to json failed:{e}"), ""))?; if SpacegateManageService::client_is_kube(client_name).await? { diff --git a/admin/src/service/plugin_service.rs b/admin/src/service/plugin_service.rs index 5db0747a..ef6ea0b3 100644 --- a/admin/src/service/plugin_service.rs +++ b/admin/src/service/plugin_service.rs @@ -43,6 +43,7 @@ impl PluginVoService { } pub(crate) async fn update(client_name: &str, update: SgFilterVo) -> TardisResult { + //todo update sgfilter or httproute\gateway Self::update_vo(client_name, update).await }