first commit

This commit is contained in:
2025-08-14 20:54:55 +08:00
commit 479d699d24
6 changed files with 2420 additions and 0 deletions

69
src/main.rs Normal file
View File

@@ -0,0 +1,69 @@
use std::{fs, path::Path};
use warp::Filter;
use welds::prelude::*;
#[derive(WeldsModel)]
#[welds(table = "authorize")]
pub struct Authorize {
#[welds(primary_key)]
pub id: i32,
pub project: String,
pub key: String,
pub device_id: String,
pub expire: String,
pub insert_time: String,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let db_path = "C:/Users/admin/Desktop/database.db";
if let Some(parent_dir) = Path::new(db_path).parent() {
if !parent_dir.exists() {
fs::create_dir_all(parent_dir)?;
println!("已创建目录: {:?}", parent_dir);
}
}
if !Path::new(db_path).exists() {
fs::File::create(db_path)?;
println!("已创建空数据库文件: {}", db_path);
}
let connection_string = format!("sqlite://{db_path}");
let client = welds::connections::connect(connection_string).await?;
// 先执行建表 SQL如果表不存在
client
.execute(
r#"
CREATE TABLE IF NOT EXISTS authorize (
id INTEGER PRIMARY KEY AUTOINCREMENT,
project TEXT NOT NULL,
key TEXT NOT NULL,
device_id TEXT NOT NULL,
expire TEXT NOT NULL,
insert_time TEXT NOT NULL
)
"#,
&[],
)
.await?;
let mut authorize = Authorize::new();
authorize.id = 0;
authorize.project = "TestProject1".to_string();
authorize.key = "TestKey1".to_string();
authorize.device_id = "Device001".to_string();
authorize.expire = "2099-12-31".to_string();
authorize.insert_time = "2025-08-14 12:00:00".to_string();
authorize.save(client.as_ref()).await?;
// GET /hello/warp => 200 OK with body "Hello, warp!"
let hello =
warp::path!("hello" / String).map(|name| format!("Hello, {}!", name));
warp::serve(hello).run(([127, 0, 0, 1], 3030)).await;
Ok(())
}