diff --git a/src/core/nal.rs b/src/core/nal.rs index fe010d4..8dd927b 100644 --- a/src/core/nal.rs +++ b/src/core/nal.rs @@ -5,6 +5,7 @@ use serde_yaml::Serializer; use std::fs::File; use std::time::Duration; use reqwest::blocking::Client; +use crate::util::app_dir; /// 网络自动登录trait pub trait Nal { @@ -73,8 +74,8 @@ impl NalConfig { /// 初始化配置 pub fn init_config() -> NalConfig { - let conf_file_path = "./config.yml"; - let result = File::open(conf_file_path); + let conf_file_path = app_dir() + "/config.yml"; + let result = File::open(conf_file_path.clone()); if result.is_err() { //初始化配置 let config = NalConfig::default(); diff --git a/src/util/logs.rs b/src/util/logs.rs index fe5ba51..03ed43f 100644 --- a/src/util/logs.rs +++ b/src/util/logs.rs @@ -1,26 +1,16 @@ use log::LevelFilter; use std::io::Error; -use std::path::Path; use std::str::FromStr; use std::sync::Mutex; -use std::{env, fs}; +use std::{fs}; use tracing::Level; use tracing_subscriber::fmt::writer::MakeWriterExt; use tracing_subscriber::util::SubscriberInitExt; +use crate::util::app_dir; /// 初始化日志 pub fn init(log_file: String, level: LevelFilter) -> Result<(), Error> { - let current_dir = if cfg!(target_os = "windows") { - // env::current_dir()?.to_str().unwrap_or(".").to_string() - env::current_exe()? - .parent() - .unwrap_or(Path::new(".")) - .to_str() - .unwrap_or(".") - .to_string() - } else { - ".".to_string() - }; + let current_dir = app_dir(); let logs_dir = current_dir + "/logs/"; fs::create_dir_all(logs_dir.clone()).unwrap(); // 如果需要,创建日志目录 // let log_file_path = logs_dir.clone().to_string() + log_file; diff --git a/src/util/mod.rs b/src/util/mod.rs index 18c3ed0..612e451 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,4 +1,23 @@ +use std::env; +use std::path::{Path, PathBuf}; + pub mod cmd; pub mod logs; pub mod rc4; pub mod service; + +/// app所在目录 +pub fn app_dir() -> String { + if cfg!(target_os = "windows") { + // env::current_dir()?.to_str().unwrap_or(".").to_string() + env::current_exe() + .unwrap_or(PathBuf::new()) + .parent() + .unwrap_or(Path::new(".")) + .to_str() + .unwrap_or(".") + .to_string() + } else { + ".".to_string() + } +} \ No newline at end of file