feat: 初始化项目结构并添加位置上报相关功能
This commit is contained in:
11
packages/gotify-ws/Cargo.toml
Normal file
11
packages/gotify-ws/Cargo.toml
Normal file
@@ -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"] }
|
||||
26
packages/gotify-ws/src/main.rs
Normal file
26
packages/gotify-ws/src/main.rs
Normal file
@@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
5
packages/gotify-ws/src/utils/logger/mod.rs
Normal file
5
packages/gotify-ws/src/utils/logger/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
use env_logger::{Builder, Env};
|
||||
|
||||
pub fn init_logger(){
|
||||
Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||
}
|
||||
1
packages/gotify-ws/src/utils/mod.rs
Normal file
1
packages/gotify-ws/src/utils/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod logger;
|
||||
1
packages/home-api/.env
Normal file
1
packages/home-api/.env
Normal file
@@ -0,0 +1 @@
|
||||
DATABASE_URL=sqlite://mydatabase.db
|
||||
6
packages/home-api/Cargo.toml
Normal file
6
packages/home-api/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "home-api"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
64958
packages/home-api/resources/ID/id.txt
Normal file
64958
packages/home-api/resources/ID/id.txt
Normal file
File diff suppressed because it is too large
Load Diff
0
packages/home-api/resources/ID/id2.txt
Normal file
0
packages/home-api/resources/ID/id2.txt
Normal file
11
packages/home-api/src/dto/location.rs
Normal file
11
packages/home-api/src/dto/location.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
pub struct Location {
|
||||
latitude: f64,
|
||||
longitude: f64,
|
||||
altitude: f64,
|
||||
accuracy: f64,
|
||||
vertical_accuracy: f64,
|
||||
bearing: f64,
|
||||
speed: f64,
|
||||
elapsedMs: i64,
|
||||
provider: String,
|
||||
}
|
||||
1
packages/home-api/src/dto/mod.rs
Normal file
1
packages/home-api/src/dto/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod location;
|
||||
13
packages/home-api/src/handler/location.rs
Normal file
13
packages/home-api/src/handler/location.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
use crate::utils::database::SqliteState;
|
||||
use actix_web::{get, web};
|
||||
use log::info;
|
||||
|
||||
// 上报location位置
|
||||
#[get("/{user}/addr/post")]
|
||||
pub async fn location(
|
||||
user_id: web::Path<String>,
|
||||
db: web::Data<SqliteState>,
|
||||
) -> Result<String, actix_web::Error> {
|
||||
info!("user_id: {}", user_id);
|
||||
Ok(user_id.into_inner())
|
||||
}
|
||||
1
packages/home-api/src/handler/mod.rs
Normal file
1
packages/home-api/src/handler/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod location;
|
||||
22
packages/home-api/src/main.rs
Normal file
22
packages/home-api/src/main.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use crate::utils::database::init_database;
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
mod handler;
|
||||
mod utils;
|
||||
mod test;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
// 初始化 logger 内容
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
let db = init_database();
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(db.clone())
|
||||
.configure(utils::router::location)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
1
packages/home-api/src/test/mod.rs
Normal file
1
packages/home-api/src/test/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
mod sqlite;
|
||||
69
packages/home-api/src/test/sqlite.rs
Normal file
69
packages/home-api/src/test/sqlite.rs
Normal file
@@ -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::<String, String>::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<SqliteConnectionManager>) {
|
||||
let conn: r2d2::PooledConnection<SqliteConnectionManager> = 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<String, String>) {
|
||||
let line = lineStr.split("----").collect::<Vec<&str>>();
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
15
packages/home-api/src/utils/database.rs
Normal file
15
packages/home-api/src/utils/database.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use actix_web::web;
|
||||
use r2d2::{Pool, State};
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use std::sync::Mutex;
|
||||
use actix_web::web::Data;
|
||||
|
||||
pub struct SqliteState {
|
||||
pool: Pool<SqliteConnectionManager>,
|
||||
}
|
||||
|
||||
pub fn init_database() -> Data<SqliteState> {
|
||||
let db = SqliteConnectionManager::file("./database.sqlite");
|
||||
let pool = Pool::new(db).unwrap();
|
||||
Data::new(SqliteState { pool: pool.clone() })
|
||||
}
|
||||
2
packages/home-api/src/utils/mod.rs
Normal file
2
packages/home-api/src/utils/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod router;
|
||||
pub mod database;
|
||||
7
packages/home-api/src/utils/router.rs
Normal file
7
packages/home-api/src/utils/router.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
use actix_web::{web};
|
||||
|
||||
use crate::handler::location;
|
||||
|
||||
pub fn location(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(web::scope("/location").service(location::location));
|
||||
}
|
||||
Reference in New Issue
Block a user