-
Notifications
You must be signed in to change notification settings - Fork 138
feat: 批量导出配置文件 #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
heqingpan
merged 2 commits into
nacos-group:master
from
a981008:feature/download_config_by_keys
Jul 6, 2025
Merged
feat: 批量导出配置文件 #236
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,27 +7,21 @@ use std::sync::Arc; | |
| use actix_multipart::form::tempfile::TempFile; | ||
| use actix_multipart::form::text::Text; | ||
| use actix_multipart::form::MultipartForm; | ||
| use actix_multipart::Multipart; | ||
| use actix_web::{http::header, web, Error, HttpMessage, HttpRequest, HttpResponse, Responder}; | ||
| use zip::write::FileOptions; | ||
|
|
||
| use super::model::PageResult; | ||
| use crate::common::appdata::AppShareData; | ||
| use crate::config::core::{ | ||
| ConfigActor, ConfigAsyncCmd, ConfigCmd, ConfigInfoDto, ConfigKey, ConfigResult, | ||
| ConfigActor, ConfigCmd, ConfigInfoDto, ConfigKey, ConfigResult, | ||
| }; | ||
| use crate::config::ConfigUtils; | ||
| use crate::console::model::config_model::{ | ||
| OpsConfigOptQueryListResponse, OpsConfigQueryListRequest, | ||
| }; | ||
| use crate::console::model::config_model::{ConfigParams, OpsConfigOptQueryListResponse, OpsConfigQueryListRequest}; | ||
| use crate::raft::cluster::model::SetConfigReq; | ||
| use crate::{now_millis, user_namespace_privilege}; | ||
| use actix::prelude::Addr; | ||
| use tokio_stream::StreamExt; | ||
| use uuid::Uuid; | ||
| use zip::{ZipArchive, ZipWriter}; | ||
|
|
||
| use super::model::config_model::OpsConfigImportInfo; | ||
| use super::model::PageResult; | ||
| use zip::ZipWriter; | ||
|
|
||
| pub async fn query_config_list( | ||
| req: HttpRequest, | ||
|
|
@@ -231,3 +225,53 @@ pub async fn download_config( | |
| Err(err) => HttpResponse::InternalServerError().body(err.to_string()), | ||
| } | ||
| } | ||
|
|
||
| /// 按 key 导出配置 | ||
| pub async fn download_config_by_keys( | ||
| request: web::Json<Vec<ConfigParams>>, | ||
| config_addr: web::Data<Addr<ConfigActor>>, | ||
| ) -> impl Responder { | ||
| let params = request.into_inner(); | ||
| if params.is_empty() { | ||
| return HttpResponse::BadRequest().body("keys cannot be empty"); | ||
| } | ||
|
|
||
| let keys = params | ||
| .into_iter() | ||
| .map(|k| { | ||
| let k = k.to_key(); | ||
| ConfigKey { | ||
| tenant: Arc::new(ConfigUtils::default_tenant(k.tenant.to_string())), | ||
| ..k | ||
| } | ||
| }) | ||
| .collect(); | ||
|
|
||
| let cmd = ConfigCmd::QueryInfoByKeys(Box::new(keys)); | ||
| match config_addr.send(cmd).await { | ||
| Ok(res) => { | ||
| let r: ConfigResult = res.unwrap(); | ||
| match r { | ||
| ConfigResult::ConfigInfoPage(_, list) => { | ||
| let mut tmpfile: File = tempfile::tempfile().unwrap(); | ||
| { | ||
| let write = std::io::Write::by_ref(&mut tmpfile); | ||
| let zip = ZipWriter::new(write); | ||
| zip_file(zip, list).ok(); | ||
| } | ||
|
Comment on lines
+257
to
+261
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改进错误处理避免panic
📋 问题详情在download_config_by_keys函数中,tempfile::tempfile().unwrap()的错误未被处理,可能导致程序panic。此外,zip_file(zip, list).ok()会忽略压缩失败的错误,无法正确反馈错误信息。 💡 解决方案改进错误处理: - let mut tmpfile: File = tempfile::tempfile().unwrap();
+ let mut tmpfile = match tempfile::tempfile() {
+ Ok(f) => f,
+ Err(e) => return HttpResponse::InternalServerError().body(e.to_string()),
+ };
修改建议:
1. 使用match处理临时文件创建失败
2. 将zip_file的错误返回给客户端
3. 优化ZipWriter的生命周期管理
|
||
| tmpfile.seek(SeekFrom::Start(0)).unwrap(); | ||
| let mut buf = vec![]; | ||
| tmpfile.read_to_end(&mut buf).unwrap(); | ||
|
|
||
| let filename = format!("rnacos_config_export_{}.zip", now_millis()); | ||
| HttpResponse::Ok() | ||
| .insert_header(header::ContentType::octet_stream()) | ||
| .insert_header(header::ContentDisposition::attachment(filename)) | ||
| .body(buf) | ||
| } | ||
| _ => HttpResponse::InternalServerError().body("config result error"), | ||
| } | ||
| } | ||
| Err(err) => HttpResponse::InternalServerError().body(err.to_string()), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修正Actor消息处理语法错误
📋 问题详情
在ConfigActor的消息处理逻辑中,.into_actor(self).map(...)的代码块缩进存在语法问题,可能导致编译失败。
💡 解决方案
修正语法错误:
确保代码块与前序代码正确缩进对齐
有用意见👍 | 无用意见👎 | 错误意见❌