Skip to content

Commit

Permalink
feat: 提供备份数据http接口,以支持通过外部定时脚本发起定时备份功能; #172
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed Nov 17, 2024
1 parent 438b1a3 commit 516472b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ k8s支持使用 [helm](https://github.com/nacos-group/r-nacos/tree/master/deploy
|RNACOS_ENABLE_OPEN_API_AUTH|是否对openapi开启鉴权;(注:nacos切换到r-nacos过程中不要开启鉴权)|false|true|0.5.8|
|RNACOS_API_LOGIN_TIMEOUT|open api鉴权有效时长,单位为秒;(注:从不鉴权到开启鉴权,需要间隔对应时长以保证客户端token能更新生效)|一小时,3600秒|3600|0.5.8|
|RNACOS_CLUSTER_TOKEN|集群间的通信请求校验token,空表示不开启校验,设置后只有相同token的节点间才可通讯|空字符串|1234567890abcdefg|0.5.8|
|RNACOS_BACKUP_TOKEN|数据备份接口请求校验token,空或长度小于32位表示不开启备份接口|空字符串|1234567890abcdefg1234567890abcdefg|0.6.6|
|RNACOS_INIT_ADMIN_USERNAME|初始化管理员用户名,只在主节点第一次启动时生效|admin|rnacos|0.5.11|
|RNACOS_INIT_ADMIN_PASSWORD|初始化管理员密码,只在主节点第一次启动时生效|admin|rnacos123456|0.5.11|
|RNACOS_ENABLE_METRICS|是否开启监控指标功能|true|true|0.5.13|
Expand Down
3 changes: 3 additions & 0 deletions doc/conf/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ RNACOS_ENABLE_NO_AUTH_CONSOLE=false
#集群间的通信请求校验token,空表示不开启校验,设置后只有相同token的节点间才可通讯;默认为字符串
#RNACOS_CLUSTER_TOKEN=bbd8b0b391254e00ae1a7c8ac6ed5f82

#数据备份接口请求校验token,空或长度小于32位表示不开启备份接口
#RNACOS_BACKUP_TOKEN=

# 初始化管理员用户名,只在主节点第一次启动时生效,默认值:admin
RNACOS_INIT_ADMIN_USERNAME=admin

Expand Down
8 changes: 8 additions & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub struct AppSysConfig {
pub openapi_login_one_minute_limit: u32,
pub openapi_enable_auth: bool,
pub cluster_token: Arc<String>,
pub backup_token: Arc<String>,
pub init_admin_username: String,
pub init_admin_password: String,
pub metrics_enable: bool,
Expand Down Expand Up @@ -165,6 +166,12 @@ impl AppSysConfig {
let cluster_token = std::env::var("RNACOS_CLUSTER_TOKEN")
.map(Arc::new)
.unwrap_or(constant::EMPTY_ARC_STRING.clone());
let mut backup_token = std::env::var("RNACOS_BACKUP_TOKEN")
.map(Arc::new)
.unwrap_or(constant::EMPTY_ARC_STRING.clone());
if backup_token.len() < 32 {
backup_token = constant::EMPTY_ARC_STRING.clone();
}
let init_admin_username =
StringUtils::map_not_empty(std::env::var("RNACOS_INIT_ADMIN_USERNAME").ok())
.unwrap_or("admin".to_owned());
Expand Down Expand Up @@ -236,6 +243,7 @@ impl AppSysConfig {
gmt_fixed_offset_hours,
openapi_enable_auth,
cluster_token,
backup_token,
init_admin_username,
init_admin_password,
metrics_enable,
Expand Down
4 changes: 1 addition & 3 deletions src/console/transfer_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use std::sync::Arc;
use tokio::fs::OpenOptions;
use tokio::io::{AsyncReadExt, AsyncSeekExt};

pub async fn download_transfer_file(
app_share_data: web::Data<Arc<AppShareData>>,
) -> impl Responder {
pub async fn download_transfer_file(app_share_data: web::Data<Arc<AppShareData>>) -> HttpResponse {
if let Ok(Ok(TransferManagerResponse::BackupFile(temp_file))) = app_share_data
.transfer_writer_manager
.send(TransferManagerAsyncRequest::Backup(
Expand Down
27 changes: 27 additions & 0 deletions src/openapi/backup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::common::appdata::AppShareData;
use crate::console::transfer_api::download_transfer_file;
use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[derive(Debug, Deserialize, Serialize)]
pub struct BackupParam {
pub token: Arc<String>,
}

pub async fn backup(
app_share_data: web::Data<Arc<AppShareData>>,
web::Query(params): web::Query<BackupParam>,
) -> impl Responder {
if app_share_data.sys_config.backup_token.is_empty() {
HttpResponse::InternalServerError().body("backup api is not open")
} else if params.token.as_str() != app_share_data.sys_config.backup_token.as_str() {
HttpResponse::InternalServerError().body("backup token is not matched")
} else {
download_transfer_file(app_share_data).await
}
}

pub fn backup_config(config: &mut web::ServiceConfig) {
config.service(web::resource("/rnacos/backup").route(web::get().to(backup)));
}
1 change: 1 addition & 0 deletions src/openapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::common::AppSysConfig;
use crate::openapi::constant::NACOS_PREFIX;

pub(crate) mod auth;
pub(crate) mod backup;
pub(crate) mod config;
mod constant;
pub(crate) mod health;
Expand Down
3 changes: 3 additions & 0 deletions src/web_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rnacos_web_dist_wrap::get_embedded_file;
use crate::common::AppSysConfig;
use crate::console::api::{console_api_config_v1, console_api_config_v2};
use crate::openapi::auth::{login_config, mock_token};
use crate::openapi::backup::backup_config;
use crate::openapi::health::health_config;
use crate::openapi::metrics::metrics_config;
use crate::openapi::{openapi_config, v1::console as nacos_console};
Expand Down Expand Up @@ -85,6 +86,7 @@ async fn disable_no_auth_console_index() -> impl Responder {
pub fn app_config(conf_data: AppSysConfig) -> impl FnOnce(&mut ServiceConfig) {
move |config: &mut ServiceConfig| {
if !conf_data.enable_no_auth_console || conf_data.openapi_enable_auth {
backup_config(config);
config
.service(web::resource("/").route(web::get().to(disable_no_auth_console_index)))
.service(
Expand All @@ -107,6 +109,7 @@ pub fn app_config(conf_data: AppSysConfig) -> impl FnOnce(&mut ServiceConfig) {
nacos_console_api_config(config);
config.configure(openapi_config(conf_data));
} else {
backup_config(config);
login_config(config);
metrics_config(config);
health_config(config);
Expand Down

0 comments on commit 516472b

Please sign in to comment.