feat: 初始化项目结构并添加位置上报相关功能

This commit is contained in:
2025-07-28 23:29:21 +08:00
parent 86e4f1acdf
commit f87786fd3a
22 changed files with 128 additions and 46 deletions

View 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"] }

View 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}");
}
}
}

View File

@@ -0,0 +1,5 @@
use env_logger::{Builder, Env};
pub fn init_logger(){
Builder::from_env(Env::default().default_filter_or("info")).init();
}

View File

@@ -0,0 +1 @@
pub mod logger;

1
packages/home-api/.env Normal file
View File

@@ -0,0 +1 @@
DATABASE_URL=sqlite://mydatabase.db

View File

@@ -0,0 +1,6 @@
[package]
name = "home-api"
version = "0.1.0"
edition = "2024"
[dependencies]

File diff suppressed because it is too large Load Diff

View File

View 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,
}

View File

@@ -0,0 +1 @@
pub mod location;

View 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())
}

View File

@@ -0,0 +1 @@
pub mod location;

View 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
}

View File

@@ -0,0 +1 @@
mod sqlite;

View 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());
}
}
}

View 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() })
}

View File

@@ -0,0 +1,2 @@
pub mod router;
pub mod database;

View 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));
}