Skip to content

Commit

Permalink
ref: 重构优化
Browse files Browse the repository at this point in the history
  • Loading branch information
HKMV committed Apr 29, 2024
1 parent 175ac63 commit 9f6fad1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 41 deletions.
44 changes: 44 additions & 0 deletions src/core/nal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashMap;
use std::fs::File;
use std::time::Duration;
use async_trait::async_trait;
use log::warn;
use reqwest::{Client, Error};
use serde::{Deserialize, Serialize};
use serde::__private::de::Content::I16;
Expand Down Expand Up @@ -44,6 +46,48 @@ pub struct NetStatusCheck {
pub interval: u16,
}

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

impl NalConfig {
pub fn default() -> Self {
NalConfig {
net_type: Option::from(NetType::Sangfor),
login: LoginConfig { username: "".to_string(), password: "".to_string() },
check: NetStatusCheck { interval: 0 },
}
}
}

/// 初始化配置
pub fn init_config() -> NalConfig {
let result = File::open("./config.yml");
if result.is_err() {
//初始化配置
return NalConfig::default();
}

//缺少字段会导致序列化出错
let result1 = serde_yaml::from_reader(result.unwrap());
if result1.is_err() {
let string = result1.err().unwrap().to_string();
warn!("config serde_yaml error: {string}");
NalConfig::default()
} else {
let mut yaml: NalConfig = result1.unwrap_or(NalConfig::default());
if yaml.net_type.is_none() {
yaml.net_type = Option::from(NetType::Sangfor)
}
yaml
}
}



/// 获取没有代理的客户端
pub fn get_no_proxy_client() -> Client {
Expand Down
42 changes: 1 addition & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,12 @@ mod test;
mod util;


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

impl NalConfig {
pub fn default() -> Self {
NalConfig {
net_type: Option::from(NetType::Sangfor),
login: LoginConfig { username: "".to_string(), password: "".to_string() },
check: NetStatusCheck { interval: 0 },
}
}
}

/// 初始化配置
pub fn init_config() -> NalConfig {
let result = File::open("./config.yml");
if result.is_err() {
//初始化配置
return NalConfig::default();
}

//缺少字段会导致序列化出错
let result1 = serde_yaml::from_reader(result.unwrap());
if result1.is_err() {
let string = result1.err().unwrap().to_string();
warn!("config serde_yaml error: {string}");
NalConfig::default()
} else {
let mut yaml: NalConfig = result1.unwrap_or(NalConfig::default());
if yaml.net_type.is_none() {
yaml.net_type = Option::from(NetType::Sangfor)
}
yaml
}
}

#[tokio::main]
async fn main() {
util::logs::init("nal.log").expect("初始化日志出错");

let config = init_config();
let config = nal::init_config();
info!("config: {config:#?}");

/*let expression = "* 1 * * * * *";
Expand Down

0 comments on commit 9f6fad1

Please sign in to comment.