feat: 添加正则相关处理, 心跳信息单独处理

This commit is contained in:
2025-08-02 08:32:38 +08:00
parent 709b743676
commit b1c4fb838f
5 changed files with 42 additions and 8 deletions

15
.idea/dataSources.xml generated
View File

@@ -37,5 +37,20 @@
</library>
</libraries>
</data-source>
<data-source source="LOCAL" name="db" uuid="e5f6bceb-da4b-4064-a6b3-f34e6193e88c">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:D:\dev\code\mine\home-api\db.sqlite</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
<libraries>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/xerial/sqlite-jdbc/3.45.1.0/sqlite-jdbc-3.45.1.0.jar</url>
</library>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar</url>
</library>
</libraries>
</data-source>
</component>
</project>

View File

@@ -2,6 +2,7 @@
<dictionary name="project">
<words>
<w>gotify</w>
<w>hzer</w>
</words>
</dictionary>
</component>

View File

@@ -15,3 +15,4 @@ serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.142"
tokio = { version = "1.47.0", features = ["full"] }
tokio-tungstenite = { version = "0.27.0", features = ["native-tls"] }
regex = "1.11.1"

View File

@@ -1,33 +1,39 @@
mod model;
mod tests;
mod utils;
use crate::model::ws::WsMessage;
use futures::StreamExt;
use log::info;
use serde_json;
use log::{info, warn};
use tokio_tungstenite::connect_async;
use utils::logger;
use utils::sql::sqlite::SqliteDB;
use utils::{logger, sql};
#[tokio::main]
async fn main() {
logger::init_logger();
const ADDR: &str = "wss://home.hzer.xyz/gotify/stream?token=CDIwYlYJuxWxVr5";
let (stream, _) = connect_async(ADDR).await.unwrap();
info!("Connected to Gotify server {ADDR}");
let db = SqliteDB::new("db.sqlite").unwrap();
db.create_table();
let (_, mut read) = stream.split();
while let Some(msg) = read.next().await {
match msg {
Ok(msg) => {
let str = msg.to_text().unwrap();
match serde_json::from_str::<WsMessage>(&str) {
info!("Got message: {}", str);
if str.trim().is_empty() {
info!("监听到心跳{}", &msg);
continue;
}
match serde_json::from_str::<WsMessage>(str) {
Ok(ws) => {
info!("Got {} from Gotify", ws.message);
let db = SqliteDB::new("db.sqlite").unwrap();
db.create_table();
}
Err(_) => {
info!("监听到心跳{}", &msg)
Err(e) => {
warn!("转换json失败{} -- {e}", &msg)
}
}
}

View File

@@ -0,0 +1,11 @@
#[cfg(test)]
mod tests {
use regex::Regex;
#[test]
fn validate_block_str() {
let regex = Regex::new(r"^\s+$").unwrap();
assert_eq!(regex.is_match(" "), true);
assert_eq!(" ".trim().is_empty(), true)
}
}