feat: 完善 db 的内容
This commit is contained in:
@@ -7,6 +7,9 @@ edition = "2024"
|
||||
env_logger = "0.11.8"
|
||||
futures = "0.3.31"
|
||||
log = "0.4.27"
|
||||
r2d2 = "0.8.10"
|
||||
r2d2_sqlite = "0.31.0"
|
||||
rusqlite = { version = "0.37.0", features = ["bundled"] }
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0.133"
|
||||
tokio = { version = "1.47.0", features = ["full"] }
|
||||
|
||||
@@ -1,48 +1,40 @@
|
||||
mod utils;
|
||||
mod model;
|
||||
mod utils;
|
||||
|
||||
use crate::model::ws::WsMessage;
|
||||
use futures::StreamExt;
|
||||
use log::info;
|
||||
use serde_json;
|
||||
use tokio_tungstenite::connect_async;
|
||||
use utils::logger;
|
||||
use crate::model::ws::WsMessage;
|
||||
|
||||
use utils::{logger, sql};
|
||||
use utils::sql::sqlite::SqliteDB;
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
logger::init_logger();
|
||||
const WS: &str = "wss://home.hzer.xyz/gotify/stream?token=CDIwYlYJuxWxVr5";
|
||||
|
||||
match connect_async(WS).await {
|
||||
Ok((stream, _)) => {
|
||||
info!("Connected to Gotify server {WS}");
|
||||
|
||||
let (_, mut read) = stream.split();
|
||||
while let Some(msg) = read.next().await {
|
||||
match msg {
|
||||
Ok(tokio_tungstenite::tungstenite::Message::Text(text)) => {
|
||||
info!("Received text message: {}", text);
|
||||
match serde_json::from_str::<WsMessage>(&text) {
|
||||
Ok(ws_msg) => {
|
||||
info!("Parsed message: {ws_msg:?}");
|
||||
}
|
||||
Err(e) => {
|
||||
info!("Failed to parse message: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(other_msg) => {
|
||||
info!("Received non-text message: {other_msg:?}");
|
||||
}
|
||||
Err(e) => {
|
||||
info!("Error receiving message: {e}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
info!("Failed to connect to Gotify server: {e}");
|
||||
logger::init_logger();
|
||||
const WS: &str = "wss://home.hzer.xyz/gotify/stream?token=CDIwYlYJuxWxVr5";
|
||||
let (stream, _) = connect_async(WS).await.unwrap();
|
||||
info!("Connected to Gotify server {WS}");
|
||||
|
||||
let (_, mut read) = stream.split();
|
||||
while let Some(msg) = read.next().await {
|
||||
match msg {
|
||||
Ok(msg) => {
|
||||
let str = msg.to_text().unwrap().to_string();
|
||||
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!("监听到心跳{}", &str)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
info!("Error receiving message: {e}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
pub struct WsMessage {
|
||||
id: u64,
|
||||
appid: u64,
|
||||
message: String,
|
||||
pub message: String,
|
||||
title: String,
|
||||
priority: u64,
|
||||
date: String,
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod logger;
|
||||
pub mod logger;
|
||||
pub mod sql;
|
||||
1
packages/gotify-ws/src/utils/sql/mod.rs
Normal file
1
packages/gotify-ws/src/utils/sql/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod sqlite;
|
||||
31
packages/gotify-ws/src/utils/sql/sqlite/mod.rs
Normal file
31
packages/gotify-ws/src/utils/sql/sqlite/mod.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
mod sql_line;
|
||||
|
||||
use log::warn;
|
||||
use r2d2::{Pool};
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use crate::utils::sql::sqlite::sql_line::{generate_sql_line};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SqliteDB {
|
||||
pub connection_pool: Pool<SqliteConnectionManager>,
|
||||
}
|
||||
|
||||
impl SqliteDB {
|
||||
pub fn new(address: &str) -> Option<Self> {
|
||||
let manager = SqliteConnectionManager::file(address);
|
||||
match Pool::builder().build(manager) {
|
||||
Ok(pool) => Some(SqliteDB {
|
||||
connection_pool: pool
|
||||
}),
|
||||
Err(e) => {
|
||||
warn!("Failed to create connection pool: {}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn create_table(&self) {
|
||||
let conn = self.connection_pool.get().unwrap();
|
||||
let line = generate_sql_line();
|
||||
conn.execute(line.get("create_table").unwrap(), []).expect(&format!("Failed to create db table: {:?}", self));
|
||||
}
|
||||
}
|
||||
15
packages/gotify-ws/src/utils/sql/sqlite/sql_line.rs
Normal file
15
packages/gotify-ws/src/utils/sql/sqlite/sql_line.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn generate_sql_line() -> HashMap<&'static str, &'static str> {
|
||||
let mut line = HashMap::new();
|
||||
line.insert("create_table", "
|
||||
CREATE TABLE IF NOT EXISTS gotify (
|
||||
id INTEGER PRIMARY KEY,
|
||||
appid INTEGER,
|
||||
message TEXT,
|
||||
title TEXT,
|
||||
priority INTEGER,
|
||||
date TEXT
|
||||
");
|
||||
line
|
||||
}
|
||||
Reference in New Issue
Block a user