diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index eb54bda..7939530 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -8,5 +8,12 @@ jdbc:postgresql://home.hzer.xyz:5432/postgres $ProjectFileDir$ + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:D:\dev\code\mine\home-api\database.sqlite + $ProjectFileDir$ + \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index e740856..6469c4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,14 +1,3 @@ -[package] -name = "init" -version = "0.1.0" -edition = "2024" - -[dependencies] -actix-web = "4.11.0" -actix-files = "0.6.6" -actix-cors = "0.7.0" -log = "0.4" -env_logger = "0.11.8" -r2d2 = "0.8.10" -r2d2_sqlite = "0.30.0" -rusqlite = { version = "0.36", features = ["bundled"] } +[workspace] +resolver = "2" +members = ["packages/gotify-ws", "packages/home-api"] diff --git a/database.sqlite b/database.sqlite index a44e0e6..46cdc08 100644 Binary files a/database.sqlite and b/database.sqlite differ diff --git a/packages/gotify-ws/Cargo.toml b/packages/gotify-ws/Cargo.toml new file mode 100644 index 0000000..0b2ea66 --- /dev/null +++ b/packages/gotify-ws/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "gotify-ws" +version = "0.1.0" +edition = "2024" + +[dependencies] +env_logger = "0.11.8" +futures = "0.3.31" +log = "0.4.27" +tokio = { version = "1.47.0", features = ["full"] } +tokio-tungstenite = { version = "0.27.0", features = ["native-tls"] } diff --git a/packages/gotify-ws/src/main.rs b/packages/gotify-ws/src/main.rs new file mode 100644 index 0000000..62ca78d --- /dev/null +++ b/packages/gotify-ws/src/main.rs @@ -0,0 +1,26 @@ +mod utils; + +use futures::StreamExt; +use log::info; +use tokio_tungstenite::connect_async; +use utils::logger; + +#[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 { + info!("Received message: {msg:?}"); + } + } + Err(e) => { + info!("Failed to connect to Gotify server: {e}"); + } + } +} diff --git a/packages/gotify-ws/src/utils/logger/mod.rs b/packages/gotify-ws/src/utils/logger/mod.rs new file mode 100644 index 0000000..ee20910 --- /dev/null +++ b/packages/gotify-ws/src/utils/logger/mod.rs @@ -0,0 +1,5 @@ +use env_logger::{Builder, Env}; + +pub fn init_logger(){ + Builder::from_env(Env::default().default_filter_or("info")).init(); +} \ No newline at end of file diff --git a/packages/gotify-ws/src/utils/mod.rs b/packages/gotify-ws/src/utils/mod.rs new file mode 100644 index 0000000..7204c8c --- /dev/null +++ b/packages/gotify-ws/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod logger; \ No newline at end of file diff --git a/.env b/packages/home-api/.env similarity index 100% rename from .env rename to packages/home-api/.env diff --git a/packages/home-api/Cargo.toml b/packages/home-api/Cargo.toml new file mode 100644 index 0000000..73e4f48 --- /dev/null +++ b/packages/home-api/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "home-api" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/resources/ID/id.txt b/packages/home-api/resources/ID/id.txt similarity index 100% rename from resources/ID/id.txt rename to packages/home-api/resources/ID/id.txt diff --git a/resources/ID/id2.txt b/packages/home-api/resources/ID/id2.txt similarity index 100% rename from resources/ID/id2.txt rename to packages/home-api/resources/ID/id2.txt diff --git a/src/dto/location.rs b/packages/home-api/src/dto/location.rs similarity index 100% rename from src/dto/location.rs rename to packages/home-api/src/dto/location.rs diff --git a/src/dto/mod.rs b/packages/home-api/src/dto/mod.rs similarity index 100% rename from src/dto/mod.rs rename to packages/home-api/src/dto/mod.rs diff --git a/src/handler/location.rs b/packages/home-api/src/handler/location.rs similarity index 100% rename from src/handler/location.rs rename to packages/home-api/src/handler/location.rs diff --git a/src/handler/mod.rs b/packages/home-api/src/handler/mod.rs similarity index 100% rename from src/handler/mod.rs rename to packages/home-api/src/handler/mod.rs diff --git a/src/main.rs b/packages/home-api/src/main.rs similarity index 100% rename from src/main.rs rename to packages/home-api/src/main.rs diff --git a/src/test/mod.rs b/packages/home-api/src/test/mod.rs similarity index 100% rename from src/test/mod.rs rename to packages/home-api/src/test/mod.rs diff --git a/packages/home-api/src/test/sqlite.rs b/packages/home-api/src/test/sqlite.rs new file mode 100644 index 0000000..cb741d1 --- /dev/null +++ b/packages/home-api/src/test/sqlite.rs @@ -0,0 +1,69 @@ +mod sqlite { + use std::{ + collections::HashMap, + fs::{File, read_to_string}, + io::{BufRead, BufReader, Read}, + }; + + use r2d2::Pool; + use r2d2_sqlite::SqliteConnectionManager; + + #[test] + fn init() { + let manager = SqliteConnectionManager::file("database.sqlite"); + let pool = r2d2::Pool::new(manager).unwrap(); + + let file = File::open("./resources/ID/id.txt").unwrap(); + let reader = BufReader::new(file); + + check_id_column_exist(&pool); + + let mut id_map = HashMap::::new(); + + for line in reader.lines() { + match line { + Ok(line) => { + handle_duplicate_id(&line, &mut id_map); + } + Err(e) => { + println!("{}", e); + } + } + } + + // println!("id_map 中共有 {} 条记录", id_map.len()); + } + + fn check_id_column_exist(conn: &Pool) { + let conn: r2d2::PooledConnection = conn.get().unwrap(); + conn.execute( + "CREATE TABLE IF NOT EXISTS id_card ( + row_id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + id TEXT NOT NULL, + create_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + update_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )", + [], + ) + .unwrap(); + } + + /** + * 处理重复的id + */ + fn handle_duplicate_id(lineStr: &str, id_map: &mut HashMap) { + let line = lineStr.split("----").collect::>(); + + if line.len() != 2 { println!("the line {} is invalid", line); + return; + } + let (name, id) = (line[0], line[1]); + + if id_map.contains_key(id) { + println!("the id {} is duplicate, and the name is {}", id, name); + } else { + id_map.insert(id.to_string(), name.to_string()); + } + } +} diff --git a/src/utils/database.rs b/packages/home-api/src/utils/database.rs similarity index 100% rename from src/utils/database.rs rename to packages/home-api/src/utils/database.rs diff --git a/src/utils/mod.rs b/packages/home-api/src/utils/mod.rs similarity index 100% rename from src/utils/mod.rs rename to packages/home-api/src/utils/mod.rs diff --git a/src/utils/router.rs b/packages/home-api/src/utils/router.rs similarity index 100% rename from src/utils/router.rs rename to packages/home-api/src/utils/router.rs diff --git a/src/test/sqlite.rs b/src/test/sqlite.rs deleted file mode 100644 index 0f3e33f..0000000 --- a/src/test/sqlite.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod sqlite { - use std::{ - fs::{File, read_to_string}, - io::{BufRead, BufReader, Read}, - }; - - use r2d2_sqlite::SqliteConnectionManager; - - #[test] - fn init() { - let manager = SqliteConnectionManager::file("database.sqlite"); - let pool = r2d2::Pool::new(manager).unwrap(); - - let file = File::open("./resources/ID/id.txt").unwrap(); - let reader = BufReader::new(file); - - for line in reader.lines() { - match line { - Ok(line) => { - let line = line.split("----").collect::>(); - let (name, id) = (line[0], line[1]); - let conn = pool.get().unwrap(); - let mut stmt = conn.prepare("INSERT INTO id (name, id) VALUES (?1, ?2)").unwrap(); - stmt.execute((name, id)).unwrap(); - } - Err(e) => { - println!("{}", e); - } - } - } - } -}