feat: 添加 axum 框架支持和 SQLite 数据库功能
- 添加 axum 框架支持并更新依赖版本 - 添加 axum、chrono、rand、serde 和 serde_json 依赖,更新 sqlx 和 tokio 配置,移除 warp 依赖。 - 添加授权数据数据库文件 - 添加数据库模块,实现 SQLite 连接、授权表创建及授权数据的增查功能。 - 添加随机Token生成器结构体及其实现,支持自定义字符集生成随机字符串。 - 重构项目以支持跨平台数据库路径配置并集成Token生成与验证功能
This commit is contained in:
111
src/db.rs
Normal file
111
src/db.rs
Normal file
@@ -0,0 +1,111 @@
|
||||
use serde::Serialize;
|
||||
use serde_json;
|
||||
use std::{fs, path::Path};
|
||||
use welds::{connections::sqlite::SqliteClient, prelude::*};
|
||||
|
||||
#[derive(WeldsModel, Clone, Serialize)]
|
||||
#[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,
|
||||
}
|
||||
|
||||
/// 包装类,内部持有 SQLite 连接
|
||||
pub struct Db {
|
||||
client: SqliteClient,
|
||||
}
|
||||
|
||||
impl Db {
|
||||
/// 初始化:建目录 -> 建文件 -> 建表 -> 返回 Self
|
||||
pub async fn new(
|
||||
db_path: impl AsRef<str>,
|
||||
) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let db_path = db_path.as_ref();
|
||||
|
||||
let parent = Path::new(db_path).parent().unwrap();
|
||||
if !parent.exists() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
if !Path::new(db_path).exists() {
|
||||
fs::File::create(db_path)?;
|
||||
}
|
||||
|
||||
let conn_str = format!("sqlite://{db_path}");
|
||||
let client = welds::connections::sqlite::connect(&conn_str).await?;
|
||||
|
||||
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?;
|
||||
|
||||
Ok(Db { client })
|
||||
}
|
||||
|
||||
/// 判断 token 是否存在
|
||||
pub async fn verify_token(
|
||||
&self,
|
||||
token: &str,
|
||||
) -> Result<bool, Box<dyn std::error::Error>> {
|
||||
let rows = Authorize::where_col(|p| p.key.equal(token))
|
||||
.limit(1)
|
||||
.run(&self.client)
|
||||
.await?;
|
||||
Ok(!rows.is_empty())
|
||||
}
|
||||
|
||||
/// 查询 token 的详细信息,返回 Option<Authorize> 如果存在
|
||||
pub async fn get_token_info(
|
||||
&self,
|
||||
token: &str,
|
||||
) -> Result<Option<String>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let rows: Vec<DbState<Authorize>> =
|
||||
Authorize::where_col(|p| p.key.equal(token))
|
||||
.limit(1)
|
||||
.run(&self.client)
|
||||
.await?;
|
||||
|
||||
if let Some(auth) = rows.into_inners().into_iter().next() {
|
||||
// 转 JSON
|
||||
let json_str = serde_json::to_string(&auth)?;
|
||||
Ok(Some(json_str))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// 插入新的授权数据
|
||||
pub async fn insert_authorize(
|
||||
&self,
|
||||
project: &str,
|
||||
key: &str,
|
||||
device_id: &str,
|
||||
expire: &str,
|
||||
insert_time: &str,
|
||||
) -> Result<i32, Box<dyn std::error::Error>> {
|
||||
let mut auth = Authorize::new();
|
||||
auth.project = project.to_string();
|
||||
auth.key = key.to_string();
|
||||
auth.device_id = device_id.to_string();
|
||||
auth.expire = expire.to_string();
|
||||
auth.insert_time = insert_time.to_string();
|
||||
|
||||
let _created = auth.save(&self.client).await?;
|
||||
Ok(auth.id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user