Skip to content

Commit

Permalink
update console naming_api
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed May 20, 2024
1 parent 1ae2065 commit e88e54d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/common/constant.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

pub const APP_VERSION: &str = "0.5.8-beta.1";
pub const APP_VERSION: &str = "0.5.8";

pub const EMPTY_STR: &str = "";

Expand Down
6 changes: 3 additions & 3 deletions src/console/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::common::appdata::AppShareData;
use crate::config::core::ConfigActor;

use crate::naming::ops::ops_api::query_opt_service_list;
use crate::openapi::naming::instance::{add_instance, del_instance, get_instance, update_instance};
use crate::openapi::naming::instance::{del_instance, get_instance, update_instance};
use crate::openapi::naming::service::{query_service, remove_service, update_service};
//use crate::console::raft_api::{raft_add_learner, raft_change_membership, raft_init, raft_metrics, raft_read, raft_write};

Expand Down Expand Up @@ -145,7 +145,7 @@ pub fn console_api_config(config: &mut web::ServiceConfig) {
);
}

pub fn console_api_config_new(config: &mut web::ServiceConfig) {
pub fn console_api_config_v1(config: &mut web::ServiceConfig) {
config.service(
web::scope("/rnacos/api/console")
.service(
Expand All @@ -165,7 +165,7 @@ pub fn console_api_config_new(config: &mut web::ServiceConfig) {
.service(
web::resource("/ns/instance")
.route(web::get().to(get_instance))
.route(web::post().to(add_instance))
.route(web::post().to(update_instance))
.route(web::put().to(update_instance))
.route(web::delete().to(del_instance)),
)
Expand Down
27 changes: 25 additions & 2 deletions src/console/v2/naming_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::console::model::naming_model::{
use crate::console::v2::ERROR_CODE_SYSTEM_ERROR;
use crate::naming::api_model::InstanceVO;
use crate::naming::core::{NamingActor, NamingCmd, NamingResult};
use crate::naming::model::ServiceDetailDto;
use crate::naming::model::{InstanceUpdateTag, ServiceDetailDto};
use actix::Addr;
use actix_web::web::Data;
use actix_web::{web, HttpResponse, Responder};
Expand Down Expand Up @@ -170,6 +170,25 @@ pub async fn add_instance(
appdata: Data<Arc<AppShareData>>,
web::Json(param): web::Json<InstanceParams>,
) -> impl Responder {
let update_tag = InstanceUpdateTag {
weight: match &param.weight {
Some(_v) => true,
None => false,
},
metadata: match &param.metadata {
Some(_v) => true,
None => false,
},
enabled: match &param.enabled {
Some(_v) => true,
None => false,
},
ephemeral: match &param.ephemeral {
Some(_v) => true,
None => false,
},
from_update: true,
};
match param.to_instance() {
Ok(instance) => {
if !instance.check_vaild() {
Expand All @@ -178,7 +197,11 @@ pub async fn add_instance(
Some("instance check is invalid".to_string()),
))
} else {
match appdata.naming_route.update_instance(instance, None).await {
match appdata
.naming_route
.update_instance(instance, Some(update_tag))
.await
{
Ok(_) => HttpResponse::Ok().json(ApiResult::success(Some(true))),
Err(err) => HttpResponse::Ok().json(ApiResult::<()>::error(
ERROR_CODE_SYSTEM_ERROR.to_string(),
Expand Down
2 changes: 1 addition & 1 deletion src/naming/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ pub async fn del_instance(
match instance {
Ok(instance) => {
if !instance.check_vaild() {
HttpResponse::InternalServerError().body("instance check is invalid")
HttpResponse::InternalServerError().body("console: instance check is invalid")
} else {
match appdata.naming_route.delete_instance(instance).await {
Ok(_) => HttpResponse::Ok().body("ok"),
Expand Down
53 changes: 2 additions & 51 deletions src/openapi/naming/instance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![allow(unused_imports, unused_assignments, unused_variables)]

use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
Expand All @@ -23,7 +22,7 @@ pub(super) fn service() -> Scope {
.service(
web::resource(EMPTY)
.route(web::get().to(get_instance))
.route(web::post().to(add_instance))
.route(web::post().to(update_instance))
.route(web::put().to(update_instance))
.route(web::delete().to(del_instance)),
)
Expand Down Expand Up @@ -326,53 +325,6 @@ pub async fn get_instance(
}
}

pub async fn add_instance(
a: 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 update_tag = InstanceUpdateTag {
weight: match &param.weight {
Some(v) => *v != 1.0f32,
None => false,
},
metadata: match &param.metadata {
Some(v) => !v.is_empty(),
None => false,
},
enabled: false,
ephemeral: false,
from_update: false,
};
let instance = param.convert_to_instance();
match instance {
Ok(instance) => {
if !instance.check_vaild() {
HttpResponse::InternalServerError().body("instance check is invalid")
} else {
match appdata.naming_route.update_instance(instance, None).await {
Ok(_) => HttpResponse::Ok().body("ok"),
Err(e) => HttpResponse::InternalServerError().body(e.to_string()),
}
}
}
Err(e) => HttpResponse::InternalServerError().body(e),
}
}

pub async fn update_instance(
a: web::Query<InstanceWebParams>,
payload: web::Payload,
Expand All @@ -397,8 +349,7 @@ pub async fn update_instance(
None => false,
},
metadata: match &param.metadata {
//Some(v) => !v.is_empty() && v!="{}",
Some(v) => !v.is_empty(),
Some(v) => !v.is_empty() && v != "{}",
None => false,
},
enabled: match &param.enabled {
Expand Down
6 changes: 3 additions & 3 deletions src/web_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use mime_guess::from_path;
use rnacos_web_dist_wrap::get_embedded_file;

use crate::common::AppSysConfig;
use crate::console::api::{console_api_config, console_api_config_new, console_api_config_v2};
use crate::console::api::{console_api_config, console_api_config_v1, console_api_config_v2};
use crate::openapi::auth::{login_config, mock_token};
use crate::openapi::openapi_config;
use crate::raft::network::raft_config;
Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn app_config(conf_data: AppSysConfig) -> impl FnOnce(&mut ServiceConfig) {
raft_config(config);
console_api_config(config);
console_api_config_v2(config);
console_api_config_new(config);
console_api_config_v1(config);
console_page_config(config);
};
}
Expand All @@ -129,7 +129,7 @@ pub fn app_without_no_auth_console_config(config: &mut web::ServiceConfig) {
pub fn console_config(config: &mut web::ServiceConfig) {
//console_api_config(config);
console_api_config_v2(config);
console_api_config_new(config);
console_api_config_v1(config);
console_page_config(config);
}

Expand Down

0 comments on commit e88e54d

Please sign in to comment.