first commit
This commit is contained in:
69
src/main.rs
Normal file
69
src/main.rs
Normal 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(())
|
||||
}
|
||||
Reference in New Issue
Block a user