Skip to content

Commit

Permalink
feat: 支持通过SDK接口设置配置中心的配置格式 #87
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed Jun 2, 2024
1 parent 1eb9dd2 commit d60e817
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/grpc/api_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ pub struct ConfigPublishRequest {
pub addition_map: Option<HashMap<String, String>>,
}

impl ConfigPublishRequest {
pub fn get_addition_param(&self, key: &str) -> Option<&String> {
if let Some(addition_map) = self.addition_map.as_ref() {
addition_map.get(key)
} else {
None
}
}
}

#[derive(Debug, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct ConfigQueryRequest {
Expand Down
10 changes: 9 additions & 1 deletion src/grpc/handler/config_publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use std::sync::Arc;

use crate::common::string_utils::StringUtils;
use crate::config::config_type::ConfigType;
use crate::grpc::HandlerResult;
use crate::{
common::appdata::AppShareData,
Expand Down Expand Up @@ -35,10 +37,16 @@ impl PayloadHandler for ConfigPublishRequestHandler {
) -> anyhow::Result<HandlerResult> {
let body_vec = request_payload.body.unwrap_or_default().value;
let request: ConfigPublishRequest = serde_json::from_slice(&body_vec)?;
let req = SetConfigReq::new(
let config_type = StringUtils::map_not_empty(request.get_addition_param("type").cloned())
.map(|v| ConfigType::new_by_value(v.as_ref()).get_value());
let desc =
StringUtils::map_not_empty(request.get_addition_param("desc").cloned()).map(Arc::new);
let mut req = SetConfigReq::new(
ConfigKey::new(&request.data_id, &request.group, &request.tenant),
request.content,
);
req.config_type = config_type;
req.desc = desc;
match self.app_data.config_route.set_config(req).await {
Ok(_res) => {
//let res:ConfigResult = res.unwrap();
Expand Down
19 changes: 14 additions & 5 deletions src/openapi/config/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};

use crate::common::appdata::AppShareData;
use crate::common::model::ApiResult;
use crate::common::string_utils::StringUtils;
use crate::common::web_utils::get_req_body;
use crate::config::config_index::ConfigQueryParam;
use crate::config::config_type::ConfigType;
Expand Down Expand Up @@ -41,6 +42,8 @@ pub struct ConfigWebParams {
pub group: Option<String>,
pub tenant: Option<String>,
pub content: Option<String>,
pub desc: Option<String>,
pub r#type: Option<String>,
pub search: Option<String>, //search type
pub page_no: Option<usize>, //use at search
pub page_size: Option<usize>, //use at search
Expand Down Expand Up @@ -90,6 +93,8 @@ impl ConfigWebParams {
group: select_option_by_clone(&self.group, &o.group),
tenant: select_option_by_clone(&self.tenant, &o.tenant),
content: select_option_by_clone(&self.content, &o.content),
desc: select_option_by_clone(&self.desc, &o.desc),
r#type: select_option_by_clone(&self.r#type, &o.r#type),
search: select_option_by_clone(&self.search, &o.search),
page_no: select_option_by_clone(&self.page_no, &o.page_no),
page_size: select_option_by_clone(&self.page_size, &o.page_size),
Expand Down Expand Up @@ -124,7 +129,7 @@ impl ConfigWebParams {
Ok(param)
}

pub fn to_like_search_param(self) -> ConfigQueryParam {
pub fn build_like_search_param(self) -> ConfigQueryParam {
let limit = self.page_size.unwrap_or(0xffff_ffff);
let offset = (self.page_no.unwrap_or(1) - 1) * limit;
let mut param = ConfigQueryParam {
Expand All @@ -141,7 +146,7 @@ impl ConfigWebParams {
param
}

pub fn to_search_param(self) -> ConfigQueryParam {
pub fn build_search_param(self) -> ConfigQueryParam {
let limit = self.page_size.unwrap_or(0xffff_ffff);
let offset = (self.page_no.unwrap_or(1) - 1) * limit;
let mut param = ConfigQueryParam {
Expand Down Expand Up @@ -204,13 +209,17 @@ pub(crate) async fn add_config(
}
}

let config_type = StringUtils::map_not_empty(selected_param.r#type.clone());
let desc = StringUtils::map_not_empty(selected_param.desc.clone());
let param = selected_param.to_confirmed_param();
match param {
Ok(p) => {
let req = SetConfigReq::new(
let mut req = SetConfigReq::new(
ConfigKey::new(&p.data_id, &p.group, &p.tenant),
Arc::new(p.content.to_owned()),
);
req.config_type = config_type.map(|v| ConfigType::new_by_value(v.as_ref()).get_value());
req.desc = desc.map(Arc::new);
match appdata.config_route.set_config(req).await {
Ok(_) => HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
Expand Down Expand Up @@ -281,10 +290,10 @@ pub(crate) async fn get_config(
) -> impl Responder {
if let Some(search) = web_param.search.as_ref() {
if search == "blur" {
let query_param = web_param.0.to_like_search_param();
let query_param = web_param.0.build_like_search_param();
return do_search_config(query_param, appdata).await;
} else if search == "accurate" {
let query_param = web_param.0.to_search_param();
let query_param = web_param.0.build_search_param();
return do_search_config(query_param, appdata).await;
}
};
Expand Down

0 comments on commit d60e817

Please sign in to comment.