-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from luoqiz/feat-refactor
优化、重构及添加多http回调功能
- Loading branch information
Showing
27 changed files
with
469 additions
and
99 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"cburl":["http://192.168.1.2:9999"],"wsurl":"","file_dir":""} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::{fs, sync::{Arc, Mutex, OnceLock}}; | ||
|
||
use rand::Rng; | ||
|
||
use crate::{service::message::{console_message_handler::ConsoleLogMessageHandler, http_message_handler::HttpMessageHandler, log_message_handler::LogMessageHandler}, wechat_config::WechatConfig}; | ||
|
||
use super::msg_event_mgr::MsgEventBus; | ||
|
||
|
||
// 全局参数结构 | ||
pub struct GlobalState { | ||
pub wechat_config: Arc<Mutex<WechatConfig>>, | ||
pub msg_event_bus: Arc<Mutex<MsgEventBus>>, | ||
} | ||
// 全局变量 | ||
pub static GLOBAL: OnceLock<Arc<GlobalState>> = OnceLock::new(); | ||
|
||
|
||
// 初始化全局变量 | ||
pub fn initialize_global() { | ||
|
||
// 初始化配置信息 | ||
let wechat_config: WechatConfig = init_config(); | ||
|
||
// 消息总线 | ||
let mut msg_event_bus = MsgEventBus::new(); | ||
|
||
log::info!("-------------------微信监听初始化--------------------------------"); | ||
let mut rng = rand::thread_rng(); | ||
|
||
// 前台日志处理器 | ||
let log_handler = Arc::new(LogMessageHandler { | ||
id: rng.gen::<u32>().to_string(), | ||
}); | ||
msg_event_bus.subscribe(log_handler); | ||
|
||
// 控制台日志处理器 | ||
// let console_log_handler = Arc::new(ConsoleLogMessageHandler { | ||
// id: rng.gen::<u32>().to_string(), | ||
// }); | ||
// msg_event_bus.subscribe(console_log_handler); | ||
|
||
// http 消息转发 | ||
let http_handler = Arc::new(HttpMessageHandler { | ||
id: rng.gen::<u32>().to_string(), | ||
}); | ||
msg_event_bus.subscribe(http_handler); | ||
|
||
let msg_event_bus_arc = Arc::new(Mutex::new(msg_event_bus)); | ||
|
||
let global_state: GlobalState = GlobalState { | ||
wechat_config: Arc::new(Mutex::new(wechat_config)), | ||
msg_event_bus: msg_event_bus_arc, | ||
}; | ||
let _ = GLOBAL.set(Arc::new(global_state)); | ||
} | ||
|
||
|
||
// 读取配置 | ||
fn init_config() -> WechatConfig { | ||
// 获取应用安装目录的路径 | ||
// let install_dir = resolve_path(&app, ".", None).map_err(|e| e.to_string())?; | ||
// 定义文件路径 | ||
let file_path = ".\\config.json5"; | ||
|
||
// 尝试创建并写入文件 | ||
let file_str = fs::read_to_string(&file_path).unwrap(); | ||
|
||
let wechat_config: WechatConfig = serde_json::from_str(&file_str).unwrap(); | ||
wechat_config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use async_trait::async_trait; | ||
|
||
use super::event_entity::{Event, EventHandler}; | ||
|
||
// 控制台日志打印 | ||
pub struct ConsoleLogMessageHandler { | ||
pub id: String, | ||
} | ||
|
||
#[async_trait] | ||
impl EventHandler for ConsoleLogMessageHandler { | ||
async fn handle(&self, event: Event) { | ||
if let Event::ClientMessage(ref msg) = event { | ||
println!("控制台日志处理器 {} -- 接收到信息: {:?}", self.id, msg); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use async_trait::async_trait; | ||
|
||
use crate::wcferry::wcf; | ||
|
||
#[derive(Clone)] | ||
pub enum Event { | ||
ClientMessage(wcf::WxMsg), | ||
} | ||
|
||
#[async_trait] | ||
pub trait EventHandler { | ||
async fn handle(&self, event: Event); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use async_trait::async_trait; | ||
|
||
use crate::service::global_service::GLOBAL; | ||
|
||
use serde_json::json; | ||
|
||
use super::event_entity::{Event, EventHandler}; | ||
|
||
/// 配置 http 回调地址后,将调用设置的url, | ||
pub struct HttpMessageHandler { | ||
pub id: String, | ||
} | ||
|
||
#[async_trait] | ||
impl EventHandler for HttpMessageHandler { | ||
async fn handle(&self, event: Event) { | ||
if let Event::ClientMessage(ref msg) = event { | ||
let global = GLOBAL.get().unwrap(); | ||
let k_config = global.wechat_config.try_lock().unwrap(); | ||
let cburl = k_config.cburl.clone(); | ||
if cburl.is_empty() { | ||
return; | ||
} | ||
|
||
for url in cburl { | ||
log::debug!("http服务 {} 回调地址为: {:?}", self.id, url.clone()); | ||
if !url.starts_with("http") { | ||
log::error!("http 转发消息失败,回调地址不合法"); | ||
continue; | ||
} | ||
|
||
let res = ureq::post(&url).send_json(json!(&msg)); | ||
match res { | ||
Ok(rsp) => { | ||
if rsp.status() != 200 { | ||
log::error!("转发消息失败,状态码: {}", rsp.status()); | ||
} | ||
log::debug!("{}", rsp.into_string().unwrap()); | ||
} | ||
Err(e) => { | ||
log::error!("转发消息失败:{}", e); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use async_trait::async_trait; | ||
|
||
use super::event_entity::{Event, EventHandler}; | ||
|
||
/// 日志打印 | ||
pub struct LogMessageHandler { | ||
pub id: String, | ||
} | ||
|
||
#[async_trait] | ||
impl EventHandler for LogMessageHandler { | ||
async fn handle(&self, event: Event) { | ||
if let Event::ClientMessage(ref msg) = event { | ||
log::info!("日志处理器 {} -- 接收到信息: {:?}", self.id, msg); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod event_entity; | ||
pub mod log_message_handler; | ||
pub mod console_message_handler; | ||
pub mod http_message_handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod global_service; | ||
pub mod msg_event_mgr; | ||
pub mod message; | ||
// pub mod wechat_event_service; | ||
// pub mod http_server_service; |
Oops, something went wrong.