Skip to content

Commit

Permalink
使用merge_web_param统一简化openapi需要同时支持queryParam与bodyParam合并逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed Aug 16, 2024
1 parent e643315 commit bf56f3b
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 159 deletions.
19 changes: 19 additions & 0 deletions src/common/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,22 @@ macro_rules! merge_web_param {
$param.merge(_b)
}};
}

#[macro_export]
macro_rules! merge_web_param_with_result {
($param:expr,$payload:expr) => {{
let _body = match crate::common::web_utils::get_req_body($payload).await {
Ok(v) => v,
Err(err) => {
return Ok(actix_web::HttpResponse::InternalServerError().body(err.to_string()));
}
};
let _b = match serde_urlencoded::from_bytes(&_body) {
Ok(v) => v,
Err(err) => {
return Ok(actix_web::HttpResponse::InternalServerError().body(err.to_string()));
}
};
$param.merge(_b)
}};
}
25 changes: 8 additions & 17 deletions src/naming/api_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::now_millis_i64;

use super::model::{Instance, ServiceDetailDto, ServiceKey};
use super::NamingUtils;
use crate::common::option_utils::OptionUtils;
use chrono::Local;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
Expand Down Expand Up @@ -133,25 +134,15 @@ pub struct ServiceInfoParam {
pub selector: Option<String>,
}

pub fn select_option<T>(a: Option<T>, b: Option<T>) -> Option<T>
where
T: Clone,
{
match a {
Some(v) => Some(v),
None => b,
}
}

impl ServiceInfoParam {
pub(crate) fn merge_value(a: Self, b: Self) -> Self {
pub(crate) fn merge(self, b: Self) -> Self {
Self {
namespace_id: select_option(a.namespace_id, b.namespace_id),
group_name: select_option(a.group_name, b.group_name),
service_name: select_option(a.service_name, b.service_name),
protect_threshold: select_option(a.protect_threshold, b.protect_threshold),
metadata: select_option(a.metadata, b.metadata),
selector: select_option(a.selector, b.selector),
namespace_id: OptionUtils::select(self.namespace_id, b.namespace_id),
group_name: OptionUtils::select(self.group_name, b.group_name),
service_name: OptionUtils::select(self.service_name, b.service_name),
protect_threshold: OptionUtils::select(self.protect_threshold, b.protect_threshold),
metadata: OptionUtils::select(self.metadata, b.metadata),
selector: OptionUtils::select(self.selector, b.selector),
}
}

Expand Down
24 changes: 6 additions & 18 deletions src/openapi/auth.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::common::appdata::AppShareData;
use crate::common::model::TokenSession;
use crate::common::web_utils::get_req_body;
use crate::common::option_utils::OptionUtils;
use crate::merge_web_param_with_result;
use crate::raft::cache::model::{CacheKey, CacheType, CacheValue};
use crate::raft::cache::{CacheLimiterReq, CacheManagerReq, CacheManagerResult};
use crate::user::{UserManagerReq, UserManagerResult};
use crate::utils;
use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
Expand All @@ -19,8 +19,8 @@ pub struct LoginParams {
impl LoginParams {
pub fn merge(self, other: Self) -> Self {
Self {
username: utils::merge_option(self.username, other.username),
password: utils::merge_option(self.password, other.password),
username: OptionUtils::select(self.username, other.username),
password: OptionUtils::select(self.password, other.password),
}
}
}
Expand All @@ -37,22 +37,10 @@ const UNKNOWN_USER: &str = "unknown user!";

pub async fn login(
app: web::Data<Arc<AppShareData>>,
web::Query(a): web::Query<LoginParams>,
web::Query(param): web::Query<LoginParams>,
payload: web::Payload,
) -> actix_web::Result<impl Responder> {
let body = match get_req_body(payload).await {
Ok(v) => v,
Err(err) => {
return Ok(HttpResponse::InternalServerError().body(err.to_string()));
}
};
let b = match serde_urlencoded::from_bytes(&body) {
Ok(v) => v,
Err(err) => {
return Ok(HttpResponse::InternalServerError().body(err.to_string()));
}
};
let param = a.merge(b);
let param = merge_web_param_with_result!(param, payload);
match do_login(param, &app).await {
Ok(v) => Ok(v),
Err(e) => {
Expand Down
51 changes: 14 additions & 37 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::option_utils::OptionUtils;
use crate::common::string_utils::StringUtils;
use crate::common::web_utils::get_req_body;
use crate::config::config_index::ConfigQueryParam;
Expand All @@ -19,6 +20,7 @@ use crate::config::core::{
use crate::config::utils::param_utils;
use crate::config::ConfigUtils;
use crate::console::v2::ERROR_CODE_SYSTEM_ERROR;
use crate::merge_web_param;
use crate::openapi::constant::EMPTY;
use crate::raft::cluster::model::{DelConfigReq, SetConfigReq};
use crate::utils::select_option_by_clone;
Expand Down Expand Up @@ -87,17 +89,17 @@ impl From<ConfigInfoDto> for ConfigInfo {
}

impl ConfigWebParams {
pub fn select_option(&self, o: &Self) -> Self {
pub fn merge(self, other: Self) -> Self {
Self {
data_id: select_option_by_clone(&self.data_id, &o.data_id),
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),
data_id: OptionUtils::select(self.data_id, other.data_id),
group: OptionUtils::select(self.group, other.group),
tenant: OptionUtils::select(self.tenant, other.tenant),
content: OptionUtils::select(self.content, other.content),
desc: OptionUtils::select(self.desc, other.desc),
r#type: OptionUtils::select(self.r#type, other.r#type),
search: OptionUtils::select(self.search, other.search),
page_no: OptionUtils::select(self.page_no, other.page_no),
page_size: OptionUtils::select(self.page_size, other.page_size),
}
}

Expand Down Expand Up @@ -177,19 +179,7 @@ pub(crate) async fn add_config(
payload: web::Payload,
appdata: web::Data<Arc<AppShareData>>,
) -> impl Responder {
let body = match get_req_body(payload).await {
Ok(v) => v,
Err(err) => {
return HttpResponse::InternalServerError().body(err.to_string());
}
};
let b = match serde_urlencoded::from_bytes(&body) {
Ok(v) => v,
Err(err) => {
return HttpResponse::InternalServerError().body(err.to_string());
}
};
let selected_param = a.select_option(&b);
let selected_param = merge_web_param!(a.0, payload);
match param_utils::check_tenant(&selected_param.tenant) {
Ok(v) => v,
Err(err) => {
Expand Down Expand Up @@ -236,20 +226,7 @@ pub(crate) async fn del_config(
payload: web::Payload,
appdata: web::Data<Arc<AppShareData>>,
) -> impl Responder {
let body = match get_req_body(payload).await {
Ok(v) => v,
Err(err) => {
return HttpResponse::InternalServerError().body(err.to_string());
}
};
let b = match serde_urlencoded::from_bytes(&body) {
Ok(v) => v,
Err(err) => {
return HttpResponse::InternalServerError().body(err.to_string());
}
};

let selected_param = a.select_option(&b);
let selected_param = merge_web_param!(a.0, payload);
match param_utils::check_tenant(&selected_param.tenant) {
Ok(v) => v,
Err(err) => {
Expand Down
44 changes: 7 additions & 37 deletions src/openapi/naming/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde::{Deserialize, Serialize};

use crate::common::appdata::AppShareData;
use crate::common::web_utils::get_req_body;
use crate::merge_web_param;
use crate::naming::api_model::InstanceVO;
use crate::naming::core::{NamingActor, NamingCmd, NamingResult};
use crate::naming::model::{Instance, InstanceUpdateTag, ServiceKey};
Expand Down Expand Up @@ -61,23 +62,11 @@ pub async fn get_instance(
}

pub async fn update_instance(
a: web::Query<InstanceWebParams>,
param: web::Query<InstanceWebParams>,
payload: web::Payload,
appdata: web::Data<Arc<AppShareData>>,
) -> impl Responder {
let body = match get_req_body(payload).await {
Ok(v) => v,
Err(err) => {
return HttpResponse::InternalServerError().body(err.to_string());
}
};
let b = match serde_urlencoded::from_bytes(&body) {
Ok(v) => v,
Err(err) => {
return HttpResponse::InternalServerError().body(err.to_string());
}
};
let param = a.select_option(&b);
let param = merge_web_param!(param.0, payload);
let update_tag = InstanceUpdateTag {
weight: match &param.weight {
Some(v) => *v != 1.0f32,
Expand Down Expand Up @@ -118,23 +107,11 @@ pub async fn update_instance(
}

pub async fn del_instance(
a: web::Query<InstanceWebParams>,
param: web::Query<InstanceWebParams>,
payload: web::Payload,
appdata: web::Data<Arc<AppShareData>>,
) -> impl Responder {
let body = match get_req_body(payload).await {
Ok(v) => v,
Err(err) => {
return HttpResponse::InternalServerError().body(err.to_string());
}
};
let b = match serde_urlencoded::from_bytes(&body) {
Ok(v) => v,
Err(err) => {
return HttpResponse::InternalServerError().body(err.to_string());
}
};
let param = a.select_option(&b);
let param = merge_web_param!(param.0, payload);
let instance = param.convert_to_instance();
match instance {
Ok(instance) => {
Expand All @@ -153,18 +130,11 @@ pub async fn del_instance(

#[put("/beat")]
pub async fn beat_instance(
a: web::Query<BeatRequest>,
param: web::Query<BeatRequest>,
payload: web::Payload,
appdata: web::Data<Arc<AppShareData>>,
) -> impl Responder {
let body = get_req_body(payload).await.unwrap_or_default();
let b = match serde_urlencoded::from_bytes(&body) {
Ok(v) => v,
Err(err) => {
return HttpResponse::InternalServerError().body(err.to_string());
}
};
let param = a.select_option(&b);
let param = merge_web_param!(param.0, payload);
//debug
//log::info!("beat request param:{}",serde_json::to_string(&param).unwrap());
let instance = param.convert_to_instance();
Expand Down
43 changes: 22 additions & 21 deletions src/openapi/naming/model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(unused_imports, unused_assignments, unused_variables)]
use crate::common::option_utils::OptionUtils;
use crate::naming::model::{Instance, ServiceKey};
use crate::naming::NamingUtils;
use crate::utils::{get_bool_from_string, select_option_by_clone};
Expand All @@ -24,19 +25,19 @@ pub struct InstanceWebParams {
}

impl InstanceWebParams {
pub(crate) fn select_option(&self, o: &Self) -> Self {
pub(crate) fn merge(self, o: Self) -> Self {
Self {
ip: select_option_by_clone(&self.ip, &o.ip),
port: select_option_by_clone(&self.port, &o.port),
namespace_id: select_option_by_clone(&self.namespace_id, &o.namespace_id),
weight: select_option_by_clone(&self.weight, &o.weight),
enabled: select_option_by_clone(&self.enabled, &o.enabled),
healthy: select_option_by_clone(&self.healthy, &o.healthy),
ephemeral: select_option_by_clone(&self.ephemeral, &o.ephemeral),
metadata: select_option_by_clone(&self.metadata, &o.metadata),
cluster_name: select_option_by_clone(&self.cluster_name, &o.cluster_name),
service_name: select_option_by_clone(&self.service_name, &o.service_name),
group_name: select_option_by_clone(&self.group_name, &o.group_name),
ip: OptionUtils::select(self.ip, o.ip),
port: OptionUtils::select(self.port, o.port),
namespace_id: OptionUtils::select(self.namespace_id, o.namespace_id),
weight: OptionUtils::select(self.weight, o.weight),
enabled: OptionUtils::select(self.enabled, o.enabled),
healthy: OptionUtils::select(self.healthy, o.healthy),
ephemeral: OptionUtils::select(self.ephemeral, o.ephemeral),
metadata: OptionUtils::select(self.metadata, o.metadata),
cluster_name: OptionUtils::select(self.cluster_name, o.cluster_name),
service_name: OptionUtils::select(self.service_name, o.service_name),
group_name: OptionUtils::select(self.group_name, o.group_name),
}
}

Expand Down Expand Up @@ -175,16 +176,16 @@ pub struct BeatRequest {
}

impl BeatRequest {
pub(crate) fn select_option(&self, o: &Self) -> Self {
pub(crate) fn merge(self, o: Self) -> Self {
Self {
namespace_id: select_option_by_clone(&self.namespace_id, &o.namespace_id),
cluster_name: select_option_by_clone(&self.cluster_name, &o.cluster_name),
service_name: select_option_by_clone(&self.service_name, &o.service_name),
group_name: select_option_by_clone(&self.group_name, &o.group_name),
ephemeral: select_option_by_clone(&self.ephemeral, &o.ephemeral),
beat: select_option_by_clone(&self.beat, &o.beat),
ip: select_option_by_clone(&self.ip, &o.ip),
port: select_option_by_clone(&self.port, &o.port),
namespace_id: OptionUtils::select(self.namespace_id, o.namespace_id),
cluster_name: OptionUtils::select(self.cluster_name, o.cluster_name),
service_name: OptionUtils::select(self.service_name, o.service_name),
group_name: OptionUtils::select(self.group_name, o.group_name),
ephemeral: OptionUtils::select(self.ephemeral, o.ephemeral),
beat: OptionUtils::select(self.beat, o.beat),
ip: OptionUtils::select(self.ip, o.ip),
port: OptionUtils::select(self.port, o.port),
}
}

Expand Down
Loading

0 comments on commit bf56f3b

Please sign in to comment.