feat(nacos): add Nacos management console (#1456)
This commit is contained in:
62 files changed
+7314
-55
No files matched your search
@@ -102,6 +102,9 @@ pub async fn disconnect_db(
|
||||
let app = &state.app;
|
||||
|
||||
app.remove_connection_pools(&body.connection_id).await;
|
||||
app.nacos_registry.drop_connection(&body.connection_id).await;
|
||||
#[cfg(feature = "mq-admin")]
|
||||
app.mq_registry.drop_connection(&body.connection_id).await;
|
||||
app.reset_connection_transport(&body.connection_id).await;
|
||||
if body.connection_id.starts_with("__visible_draft_") {
|
||||
app.configs.write().await.remove(&body.connection_id);
|
||||
@@ -134,6 +137,7 @@ pub async fn save_connections(
|
||||
state.app.storage.save_connections(&body.configs).await.map_err(AppError)?;
|
||||
let sync = sync_connection_configs(&state, &body.configs).await;
|
||||
remove_connection_pools_for_connection_ids(&state, &sync.connection_pool_ids_to_drop).await;
|
||||
drop_nacos_adapters_for_connection_ids(&state, &sync.nacos_adapter_ids_to_drop).await;
|
||||
drop_mq_adapters_for_connection_ids(&state, &sync.mq_adapter_ids_to_drop).await;
|
||||
Ok(Json(()))
|
||||
}
|
||||
@@ -142,17 +146,20 @@ pub async fn load_connections(State(state): State<Arc<WebState>>) -> Result<Json
|
||||
let configs = state.app.storage.load_connections().await.map_err(AppError)?;
|
||||
let sync = sync_connection_configs(&state, &configs).await;
|
||||
remove_connection_pools_for_connection_ids(&state, &sync.connection_pool_ids_to_drop).await;
|
||||
drop_nacos_adapters_for_connection_ids(&state, &sync.nacos_adapter_ids_to_drop).await;
|
||||
drop_mq_adapters_for_connection_ids(&state, &sync.mq_adapter_ids_to_drop).await;
|
||||
Ok(Json(configs))
|
||||
}
|
||||
|
||||
struct ConnectionConfigSync {
|
||||
nacos_adapter_ids_to_drop: Vec<String>,
|
||||
mq_adapter_ids_to_drop: Vec<String>,
|
||||
connection_pool_ids_to_drop: Vec<String>,
|
||||
}
|
||||
|
||||
async fn sync_connection_configs(state: &WebState, configs: &[ConnectionConfig]) -> ConnectionConfigSync {
|
||||
let saved_ids: HashSet<&str> = configs.iter().map(|config| config.id.as_str()).collect();
|
||||
let mut nacos_adapter_ids_to_drop = HashSet::new();
|
||||
let mut mq_adapter_ids_to_drop = HashSet::new();
|
||||
let mut connection_pool_ids_to_drop = HashSet::new();
|
||||
let mut runtime_configs = state.app.configs.write().await;
|
||||
@@ -161,6 +168,9 @@ async fn sync_connection_configs(state: &WebState, configs: &[ConnectionConfig])
|
||||
true
|
||||
} else {
|
||||
connection_pool_ids_to_drop.insert(id.clone());
|
||||
if existing.db_type == dbx_core::models::connection::DatabaseType::Nacos {
|
||||
nacos_adapter_ids_to_drop.insert(id.clone());
|
||||
}
|
||||
if existing.db_type == dbx_core::models::connection::DatabaseType::MessageQueue {
|
||||
mq_adapter_ids_to_drop.insert(id.clone());
|
||||
}
|
||||
@@ -168,10 +178,16 @@ async fn sync_connection_configs(state: &WebState, configs: &[ConnectionConfig])
|
||||
}
|
||||
});
|
||||
for config in configs {
|
||||
if config.db_type == dbx_core::models::connection::DatabaseType::Nacos {
|
||||
nacos_adapter_ids_to_drop.insert(config.id.clone());
|
||||
}
|
||||
if config.db_type == dbx_core::models::connection::DatabaseType::MessageQueue {
|
||||
mq_adapter_ids_to_drop.insert(config.id.clone());
|
||||
}
|
||||
if let Some(previous) = runtime_configs.insert(config.id.clone(), config.clone()) {
|
||||
if previous.db_type == dbx_core::models::connection::DatabaseType::Nacos {
|
||||
nacos_adapter_ids_to_drop.insert(config.id.clone());
|
||||
}
|
||||
if previous.db_type == dbx_core::models::connection::DatabaseType::MessageQueue {
|
||||
mq_adapter_ids_to_drop.insert(config.id.clone());
|
||||
}
|
||||
@@ -181,6 +197,7 @@ async fn sync_connection_configs(state: &WebState, configs: &[ConnectionConfig])
|
||||
}
|
||||
}
|
||||
ConnectionConfigSync {
|
||||
nacos_adapter_ids_to_drop: nacos_adapter_ids_to_drop.into_iter().collect(),
|
||||
mq_adapter_ids_to_drop: mq_adapter_ids_to_drop.into_iter().collect(),
|
||||
connection_pool_ids_to_drop: connection_pool_ids_to_drop.into_iter().collect(),
|
||||
}
|
||||
@@ -190,6 +207,12 @@ fn is_transient_runtime_config_id(id: &str) -> bool {
|
||||
id.starts_with("__test_") || id.starts_with("__visible_draft_")
|
||||
}
|
||||
|
||||
async fn drop_nacos_adapters_for_connection_ids(state: &WebState, connection_ids: &[String]) {
|
||||
for connection_id in connection_ids {
|
||||
state.app.nacos_registry.drop_connection(connection_id).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mq-admin")]
|
||||
async fn drop_mq_adapters_for_connection_ids(state: &WebState, connection_ids: &[String]) {
|
||||
for connection_id in connection_ids {
|
||||
|
||||
@@ -12,6 +12,7 @@ pub mod layout;
|
||||
pub mod mongo;
|
||||
#[cfg(feature = "mq-admin")]
|
||||
pub mod mq;
|
||||
pub mod nacos;
|
||||
pub mod plugins;
|
||||
pub mod query;
|
||||
pub mod redis;
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::extract::State;
|
||||
use axum::Json;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::state::WebState;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct ConnReq {
|
||||
connection_id: String,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct NamespaceCreateReq {
|
||||
connection_id: String,
|
||||
req: dbx_core::nacos::NacosNamespaceCreate,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct NamespaceUpdateReq {
|
||||
connection_id: String,
|
||||
req: dbx_core::nacos::NacosNamespaceUpdate,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct ConfigListReq {
|
||||
connection_id: String,
|
||||
query: dbx_core::nacos::NacosConfigQuery,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct ConfigKeyReq {
|
||||
connection_id: String,
|
||||
key: dbx_core::nacos::NacosConfigKey,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct ConfigPublishReq {
|
||||
connection_id: String,
|
||||
req: dbx_core::nacos::NacosConfigUpsert,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct ConfigHistoryListReq {
|
||||
connection_id: String,
|
||||
query: dbx_core::nacos::NacosConfigHistoryQuery,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct ConfigHistoryKeyReq {
|
||||
connection_id: String,
|
||||
key: dbx_core::nacos::NacosConfigHistoryKey,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct ConfigRollbackReq {
|
||||
connection_id: String,
|
||||
req: dbx_core::nacos::NacosConfigRollbackRequest,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct ServiceListReq {
|
||||
connection_id: String,
|
||||
query: dbx_core::nacos::NacosServiceQuery,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct InstanceListReq {
|
||||
connection_id: String,
|
||||
query: dbx_core::nacos::NacosInstanceQuery,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct InstanceUpdateReq {
|
||||
connection_id: String,
|
||||
req: dbx_core::nacos::NacosInstanceUpdate,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct RawReq {
|
||||
connection_id: String,
|
||||
req: dbx_core::nacos::NacosRawRequest,
|
||||
}
|
||||
|
||||
pub async fn test_connection(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<ConnReq>,
|
||||
) -> Result<Json<dbx_core::nacos::NacosConnectionInfo>, AppError> {
|
||||
let result =
|
||||
dbx_core::nacos::service::nacos_test_connection_core(&state.app, &req.connection_id).await.map_err(AppError)?;
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
pub async fn list_namespaces(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<ConnReq>,
|
||||
) -> Result<Json<Vec<dbx_core::nacos::NacosNamespaceInfo>>, AppError> {
|
||||
let result =
|
||||
dbx_core::nacos::service::nacos_list_namespaces_core(&state.app, &req.connection_id).await.map_err(AppError)?;
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
pub async fn create_namespace(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<NamespaceCreateReq>,
|
||||
) -> Result<Json<()>, AppError> {
|
||||
dbx_core::nacos::service::nacos_create_namespace_core(&state.app, &req.connection_id, req.req)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(()))
|
||||
}
|
||||
|
||||
pub async fn update_namespace(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<NamespaceUpdateReq>,
|
||||
) -> Result<Json<()>, AppError> {
|
||||
dbx_core::nacos::service::nacos_update_namespace_core(&state.app, &req.connection_id, req.req)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(()))
|
||||
}
|
||||
|
||||
pub async fn list_configs(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<ConfigListReq>,
|
||||
) -> Result<Json<dbx_core::nacos::NacosConfigList>, AppError> {
|
||||
let result = dbx_core::nacos::service::nacos_list_configs_core(&state.app, &req.connection_id, req.query)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
pub async fn get_config(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<ConfigKeyReq>,
|
||||
) -> Result<Json<dbx_core::nacos::NacosConfigItem>, AppError> {
|
||||
let result = dbx_core::nacos::service::nacos_get_config_core(&state.app, &req.connection_id, req.key)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
pub async fn publish_config(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<ConfigPublishReq>,
|
||||
) -> Result<Json<()>, AppError> {
|
||||
dbx_core::nacos::service::nacos_publish_config_core(&state.app, &req.connection_id, req.req)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(()))
|
||||
}
|
||||
|
||||
pub async fn delete_config(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<ConfigKeyReq>,
|
||||
) -> Result<Json<()>, AppError> {
|
||||
dbx_core::nacos::service::nacos_delete_config_core(&state.app, &req.connection_id, req.key)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(()))
|
||||
}
|
||||
|
||||
pub async fn list_config_history(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<ConfigHistoryListReq>,
|
||||
) -> Result<Json<dbx_core::nacos::NacosConfigHistoryList>, AppError> {
|
||||
let result = dbx_core::nacos::service::nacos_list_config_history_core(&state.app, &req.connection_id, req.query)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
pub async fn get_config_history(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<ConfigHistoryKeyReq>,
|
||||
) -> Result<Json<dbx_core::nacos::NacosConfigItem>, AppError> {
|
||||
let result = dbx_core::nacos::service::nacos_get_config_history_core(&state.app, &req.connection_id, req.key)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
pub async fn rollback_config(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<ConfigRollbackReq>,
|
||||
) -> Result<Json<()>, AppError> {
|
||||
dbx_core::nacos::service::nacos_rollback_config_core(&state.app, &req.connection_id, req.req)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(()))
|
||||
}
|
||||
|
||||
pub async fn list_services(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<ServiceListReq>,
|
||||
) -> Result<Json<dbx_core::nacos::NacosServiceList>, AppError> {
|
||||
let result = dbx_core::nacos::service::nacos_list_services_core(&state.app, &req.connection_id, req.query)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
pub async fn list_instances(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<InstanceListReq>,
|
||||
) -> Result<Json<Vec<dbx_core::nacos::NacosInstanceInfo>>, AppError> {
|
||||
let result = dbx_core::nacos::service::nacos_list_instances_core(&state.app, &req.connection_id, req.query)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
pub async fn update_instance(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<InstanceUpdateReq>,
|
||||
) -> Result<Json<()>, AppError> {
|
||||
dbx_core::nacos::service::nacos_update_instance_core(&state.app, &req.connection_id, req.req)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(()))
|
||||
}
|
||||
|
||||
pub async fn raw_request(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<RawReq>,
|
||||
) -> Result<Json<dbx_core::nacos::NacosRawResponse>, AppError> {
|
||||
let result = dbx_core::nacos::service::nacos_raw_request_core(&state.app, &req.connection_id, req.req)
|
||||
.await
|
||||
.map_err(AppError)?;
|
||||
Ok(Json(result))
|
||||
}
|
||||
Reference in new issue
Block a user