Skip to content

Commit

Permalink
ref: 优化配置处理
Browse files Browse the repository at this point in the history
  • Loading branch information
HKMV committed May 26, 2024
1 parent 47938a3 commit 7cac277
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
12 changes: 6 additions & 6 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
net_type: Sangfor
login:
username: 2336 #用户名(工号)
password: 2336 #密码

username: ''
password: ''
check:
interval: 10 #单位秒

interval: 3
log:
normal: false #是否显示操作正常的日志
level: INFO
normal: false
26 changes: 23 additions & 3 deletions src/core/nal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use std::collections::HashMap;
use std::fs::File;
use std::time::Duration;
use async_trait::async_trait;
use log::warn;
use log::{LevelFilter, warn};
use reqwest::{Client, Error};
use serde::{Deserialize, Serialize};
use serde::__private::de::Content::I16;
use serde_json::Value;
use serde_yaml::Serializer;
use tokio::time::timeout;
use tracing_subscriber::fmt::MakeWriter;

/// 网络自动登录trait
#[async_trait]
Expand Down Expand Up @@ -46,12 +48,20 @@ pub struct NetStatusCheck {
pub interval: u16,
}

/// 日志相关配置
#[derive(Debug, Serialize, Deserialize)]
pub struct LogConfig{
pub level: String,
pub normal: bool
}

/// NAL配置参数
#[derive(Debug, Serialize, Deserialize)]
pub struct NalConfig {
pub net_type: Option<NetType>,
pub login: LoginConfig,
pub check: NetStatusCheck,
pub log: LogConfig
}

impl NalConfig {
Expand All @@ -66,16 +76,26 @@ impl NalConfig {
// 默认3秒
interval: 3
},
log: LogConfig {
//默认日志级别info
level: LevelFilter::Info.to_string(),
// 默认不显示正常日志
normal: false,
},
}
}
}

/// 初始化配置
pub fn init_config() -> NalConfig {
let result = File::open("./config.yml");
let conf_file_path = "./config.yml";
let result = File::open(conf_file_path.clone());
if result.is_err() {
//初始化配置
return NalConfig::default();
let config = NalConfig::default();
let file_write = File::create(conf_file_path).unwrap();
config.serialize(&mut Serializer::new(&file_write)).expect("序列化输出失败!");
return config;
}

//缺少字段会导致序列化出错
Expand Down
21 changes: 11 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::{env, fs};
use std::fs::{File, OpenOptions};
use std::str::FromStr;
use tokio::time::{Duration, sleep};
use log::{debug, info, warn};
use log::{debug, error, info, LevelFilter, warn};
use serde_json::Value::Bool;
use crate::core::nal;
use crate::core::nal::{login, LoginConfig, NetStatusCheck, NetType};
use crate::core::nal::{login, LoginConfig, NalConfig, NetStatusCheck, NetType};
use crate::core::sangfor::Sangfor;
use std::io::{Error, Write};
use std::path::PathBuf;
Expand All @@ -23,7 +23,10 @@ mod test;

#[tokio::main]
async fn main() {
util::logs::init("nal.log", log::LevelFilter::Debug).expect("初始化日志出错");
let config = nal::init_config();
debug!("config: {config:#?}");
let level_filter = LevelFilter::from_str(config.log.level.as_str()).unwrap_or(LevelFilter::Info);
util::logs::init("nal.log", level_filter).expect("初始化日志出错");

let cli = util::cmd::Cli::parse();
let service = Service::new("net-auto-login");
Expand All @@ -46,17 +49,15 @@ async fn main() {
return;
}
if cli.run {
handler().await;
handler(config).await;
return;
}
if cfg!(debug_assertions) {
handler().await
handler(config).await
}
}

async fn handler() {
let config = nal::init_config();
debug!("config: {config:#?}");
async fn handler(config: NalConfig) {
loop {
//检测网络是否正常
let is_ok = nal::check_net().await;
Expand All @@ -68,9 +69,9 @@ async fn handler() {
if login_ok.unwrap_or_else(|e| { false }) {
info!("登录成功");
} else {
info!("登录失败");
error!("登录失败");
}
} else {
} else if config.log.normal {
info!("网络正常");
};

Expand Down
4 changes: 3 additions & 1 deletion src/util/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{env, fs};
use std::fs::File;
use std::io::{Error};
use std::path::Path;
use std::str::FromStr;
use std::sync::Mutex;
use log::{error, LevelFilter};
use tracing::{info, Level};
Expand Down Expand Up @@ -151,6 +152,7 @@ fn init_tracing(logs_dir: String, log_file: String, level: LevelFilter) {
).unwrap();
// time::format_description::well_known::Rfc3339;

let tracing_level = Level::from_str(level.as_str()).unwrap();
tracing_subscriber::fmt()
.with_file(true)
.with_level(true)
Expand All @@ -159,7 +161,7 @@ fn init_tracing(logs_dir: String, log_file: String, level: LevelFilter) {
.with_thread_names(true)
.with_thread_ids(true)
.with_test_writer()
.with_max_level(Level::INFO)
.with_max_level(tracing_level)
.with_timer(tracing_subscriber::fmt::time::OffsetTime::new(
time::macros::offset!(+8), format,
))
Expand Down

0 comments on commit 7cac277

Please sign in to comment.