feat: 使用 diesel 替代 r2d2_sqlite
This commit is contained in:
@@ -4,13 +4,14 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
diesel = { version = "2.2.12", features = ["r2d2", "serde_json", "sqlite"] }
|
||||
env_logger = "0.11.8"
|
||||
futures = "0.3.31"
|
||||
log = "0.4.27"
|
||||
r2d2 = "0.8.10"
|
||||
r2d2_sqlite = "0.31.0"
|
||||
#r2d2_sqlite = "0.31.0"
|
||||
rusqlite = { version = "0.37.0", features = ["bundled"] }
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0.133"
|
||||
serde_json = "1.0.142"
|
||||
tokio = { version = "1.47.0", features = ["full"] }
|
||||
tokio-tungstenite = { version = "0.27.0", features = ["native-tls"] }
|
||||
|
||||
@@ -6,20 +6,20 @@ use futures::StreamExt;
|
||||
use log::info;
|
||||
use serde_json;
|
||||
use tokio_tungstenite::connect_async;
|
||||
use utils::{logger, sql};
|
||||
use utils::sql::sqlite::SqliteDB;
|
||||
use utils::{logger, sql};
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
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}");
|
||||
|
||||
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 (_, mut read) = stream.split();
|
||||
while let Some(msg) = read.next().await {
|
||||
match msg {
|
||||
Ok(msg) => {
|
||||
let str = msg.to_text().unwrap().to_string();
|
||||
let str = msg.to_text().unwrap();
|
||||
match serde_json::from_str::<WsMessage>(&str) {
|
||||
Ok(ws) => {
|
||||
info!("Got {} from Gotify", ws.message);
|
||||
@@ -27,7 +27,7 @@ async fn main() {
|
||||
db.create_table();
|
||||
}
|
||||
Err(_) => {
|
||||
info!("监听到心跳{}", &str)
|
||||
info!("监听到心跳{}", &msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
use diesel::Queryable;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize, Queryable)]
|
||||
pub struct WsMessage {
|
||||
id: u64,
|
||||
appid: u64,
|
||||
pub message: String,
|
||||
title: String,
|
||||
priority: u64,
|
||||
date: String,
|
||||
id: u64,
|
||||
appid: u64,
|
||||
pub message: String,
|
||||
title: String,
|
||||
priority: u64,
|
||||
date: String,
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
pub mod sqlite;
|
||||
pub mod sqlite;
|
||||
mod tests;
|
||||
@@ -1,21 +1,23 @@
|
||||
mod sql_line;
|
||||
|
||||
use crate::utils::sql::sqlite::sql_line::CREATE_TABLE;
|
||||
use diesel::RunQueryDsl;
|
||||
use diesel::r2d2::ConnectionManager;
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use log::warn;
|
||||
use r2d2::{Pool};
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use crate::utils::sql::sqlite::sql_line::{generate_sql_line};
|
||||
use r2d2::Pool;
|
||||
|
||||
#[derive(Debug)]
|
||||
// #[derive(Debug)]
|
||||
pub struct SqliteDB {
|
||||
pub connection_pool: Pool<SqliteConnectionManager>,
|
||||
pub connection_pool: Pool<ConnectionManager<SqliteConnection>>,
|
||||
}
|
||||
|
||||
impl SqliteDB {
|
||||
pub fn new(address: &str) -> Option<Self> {
|
||||
let manager = SqliteConnectionManager::file(address);
|
||||
let manager = ConnectionManager::<SqliteConnection>::new(address);
|
||||
match Pool::builder().build(manager) {
|
||||
Ok(pool) => Some(SqliteDB {
|
||||
connection_pool: pool
|
||||
connection_pool: pool,
|
||||
}),
|
||||
Err(e) => {
|
||||
warn!("Failed to create connection pool: {}", e);
|
||||
@@ -23,9 +25,15 @@ impl SqliteDB {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
let mut conn = self
|
||||
.connection_pool
|
||||
.get()
|
||||
.expect("无法连接,数据库连接池未初始化");
|
||||
|
||||
diesel::sql_query(CREATE_TABLE)
|
||||
.execute(&mut conn)
|
||||
.expect("创建表失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,6 @@
|
||||
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
|
||||
}
|
||||
pub static CREATE_TABLE: &'static str = r#"CREATE TABLE IF NOT EXISTS gotify (
|
||||
id INTEGER PRIMARY KEY,
|
||||
appid INTEGER,
|
||||
message TEXT,
|
||||
title TEXT,
|
||||
priority INTEGER, date TEXT)"#;
|
||||
|
||||
1
packages/gotify-ws/src/utils/sql/tests/mod.rs
Normal file
1
packages/gotify-ws/src/utils/sql/tests/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
mod sqlite;
|
||||
32
packages/gotify-ws/src/utils/sql/tests/sqlite.rs
Normal file
32
packages/gotify-ws/src/utils/sql/tests/sqlite.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
#[cfg(test)]
|
||||
mod sqlite_test {
|
||||
use std::fs::remove_file;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::utils::sql::sqlite::SqliteDB;
|
||||
|
||||
#[test]
|
||||
fn new_database() {
|
||||
let file_path: &str = "test.sqlite";
|
||||
SqliteDB::new(&file_path).unwrap();
|
||||
|
||||
let file = Path::new(&file_path);
|
||||
assert!(file.exists());
|
||||
|
||||
if file.exists() {
|
||||
remove_file(file).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_table() {
|
||||
let file_path: &str = "test.sqlite";
|
||||
let db = SqliteDB::new(&file_path).unwrap();
|
||||
db.create_table();
|
||||
drop(db.connection_pool);
|
||||
|
||||
if Path::new(&file_path).exists() {
|
||||
remove_file(file_path).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user