Skip to content

Commit 6d2d607

Browse files
heqingpanreigadegr
authored andcommitted
feat: 调整未配置数据目录的默认目录取值,linux、macos默认目录调整为~/.local/share/r-nacos/nacos_db,windows与docker中取nacos_db(与之前一致) nacos-group#78
1 parent 124ee8d commit 6d2d607

File tree

7 files changed

+37
-19
lines changed

7 files changed

+37
-19
lines changed

Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ FROM debian:bookworm-slim
77
# RUN apt-get update && apt-get install -y extra-runtime-dependencies && rm -rf /var/lib/apt/lists/*
88
COPY --from=builder /usr/local/cargo/bin/rnacos /usr/bin/rnacos
99
ENV USER root
10+
ENV RNACOS_RUN_IN_DOCKER=true
1011
RUN mkdir /io
1112
WORKDIR /io
1213
ENTRYPOINT ["/usr/bin/rnacos"]

docker/alpine_dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ RUN cd /rnacos && \
2727
FROM alpine
2828
ENV PATH /root/.cargo/bin:$PATH
2929
ENV USER root
30+
ENV RNACOS_RUN_IN_DOCKER=true
3031
RUN mkdir /io
3132
COPY --from=builder /usr/bin/rnacos /usr/bin/rnacos
3233
WORKDIR /io

docker/gnu_dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ FROM debian:bookworm-slim
1919
# RUN apt-get update && apt-get install -y extra-runtime-dependencies && rm -rf /var/lib/apt/lists/*
2020
COPY --from=builder /usr/bin/rnacos /usr/bin/rnacos
2121
ENV USER root
22+
ENV RNACOS_RUN_IN_DOCKER=true
2223
RUN mkdir /io
2324
WORKDIR /io
2425
ENTRYPOINT ["/usr/bin/rnacos"]

docker/pre_dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ FROM debian:bookworm-slim
1919
# RUN apt-get update && apt-get install -y extra-runtime-dependencies && rm -rf /var/lib/apt/lists/*
2020
COPY --from=builder /usr/bin/rnacos /usr/bin/rnacos
2121
ENV USER root
22+
ENV RNACOS_RUN_IN_DOCKER=true
2223
RUN mkdir /io
2324
WORKDIR /io
2425
ENTRYPOINT ["/usr/bin/rnacos"]

src/common/mod.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl NamingSysConfig {
5252
#[derive(Default, Clone, Debug)]
5353
pub struct AppSysConfig {
5454
pub config_db_file: String,
55-
pub config_db_dir: String,
55+
pub local_db_dir: String,
5656
pub config_max_content: usize,
5757
pub http_port: u16,
5858
pub http_console_port: u16,
@@ -78,6 +78,7 @@ pub struct AppSysConfig {
7878
pub metrics_log_interval_second: u64,
7979
pub metrics_log_enable: bool,
8080
pub console_captcha_enable: bool,
81+
pub run_in_docker: bool,
8182
}
8283

8384
impl AppSysConfig {
@@ -105,7 +106,10 @@ impl AppSysConfig {
105106
.unwrap_or("".to_owned())
106107
.parse()
107108
.unwrap_or(http_port + 2000);
108-
let config_db_dir = std::env::var("RNACOS_CONFIG_DB_DIR").unwrap_or("nacos_db".to_owned());
109+
let run_in_docker = std::env::var("RNACOS_RUN_IN_DOCKER")
110+
.unwrap_or("".to_owned())
111+
.eq_ignore_ascii_case("true");
112+
let local_db_dir = Self::get_data_dir(run_in_docker);
109113
let raft_node_id = std::env::var("RNACOS_RAFT_NODE_ID")
110114
.unwrap_or("1".to_owned())
111115
.parse()
@@ -189,7 +193,7 @@ impl AppSysConfig {
189193
metrics_collect_interval_second = metrics_log_interval_second;
190194
}
191195
Self {
192-
config_db_dir,
196+
local_db_dir,
193197
config_db_file,
194198
config_max_content,
195199
http_port,
@@ -216,6 +220,26 @@ impl AppSysConfig {
216220
metrics_collect_interval_second,
217221
metrics_log_interval_second,
218222
console_captcha_enable,
223+
run_in_docker,
224+
}
225+
}
226+
227+
/// 获取数据目录
228+
fn get_data_dir(run_in_docker: bool) -> String {
229+
if let Ok(v) = std::env::var("RNACOS_DATA_DIR") {
230+
v
231+
} else if let Ok(v) = std::env::var("RNACOS_CONFIG_DB_DIR") {
232+
v
233+
} else if run_in_docker {
234+
// 运行在docker,默认值保持一致
235+
"nacos_db".to_owned()
236+
} else {
237+
#[cfg(any(target_os = "linux", target_os = "macos"))]
238+
{
239+
return "~/.local/share/r-nacos/nacos_db".to_string();
240+
}
241+
// windows系统默认值保持一致
242+
"nacos_db".to_owned()
219243
}
220244
}
221245

src/main.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
5656
init_env();
5757
let rust_log = std::env::var("RUST_LOG").unwrap_or("info".to_owned());
5858
println!("version:{}, RUST_LOG:{}", APP_VERSION, &rust_log);
59-
std::env::set_var("RUST_LOG", &rust_log);
59+
unsafe {
60+
std::env::set_var("RUST_LOG", &rust_log);
61+
}
6062
let sys_config = Arc::new(AppSysConfig::init_from_env());
61-
println!("data dir:{}", sys_config.config_db_dir);
63+
println!("data dir:{}", sys_config.local_db_dir);
6264
let timezone_fmt = Arc::new(TimeZoneFormatEnv::new(
6365
sys_config.gmt_fixed_offset_hours.map(|v| v * 60 * 60),
6466
Some(TimestampPrecision::Micros),

src/starter.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,8 @@ use bean_factory::{BeanDefinition, BeanFactory, FactoryData};
4848
use chrono::{FixedOffset, Local, Offset};
4949

5050
pub async fn config_factory(sys_config: Arc<AppSysConfig>) -> anyhow::Result<FactoryData> {
51-
/*
52-
let db = Arc::new(
53-
sled::Config::new()
54-
.path(&sys_config.config_db_dir)
55-
.mode(sled::Mode::LowSpace)
56-
.cache_capacity(10 * 1024 * 1024)
57-
//.flush_every_ms(Some(1000))
58-
.open()
59-
.unwrap(),
60-
);
61-
factory.register(BeanDefinition::from_obj(db.clone()));
62-
*/
63-
std::fs::create_dir_all(sys_config.config_db_dir.as_str())?;
64-
let base_path = Arc::new(sys_config.config_db_dir.clone());
51+
std::fs::create_dir_all(sys_config.local_db_dir.as_str())?;
52+
let base_path = Arc::new(sys_config.local_db_dir.clone());
6553
let factory = BeanFactory::new();
6654
factory.register(BeanDefinition::from_obj(sys_config.clone()));
6755

0 commit comments

Comments
 (0)