Skip to content

Commit

Permalink
fix: 修复windows服务环境配置文件不生效问题
Browse files Browse the repository at this point in the history
  • Loading branch information
SerenitySir committed Jul 30, 2024
1 parent a4bfc31 commit 52d0b1d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/core/nal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 3 additions & 13 deletions src/util/logs.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
19 changes: 19 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 52d0b1d

Please sign in to comment.