feat: 添加授权数据目录忽略和数据库查询功能

- 添加 authorize_data/ 目录到 .gitignore 文件中以忽略该目录的版本控制
- 更新授权数据库文件内容
- 添加通过项目和设备ID查询数据库存在的功能,并将InsertArgs结构体设为公共可见。
- 添加创建令牌时检查项目和设备ID是否已存在的逻辑,并返回相应的状态信息。
This commit is contained in:
2025-08-18 11:03:17 +08:00
parent 92a5259636
commit c241e16883
4 changed files with 52 additions and 13 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
/target /target
authorize_data/

Binary file not shown.

View File

@@ -14,7 +14,7 @@ pub struct Authorize {
pub insert_time: String, pub insert_time: String,
} }
pub(crate) struct InsertArgs { pub struct InsertArgs {
pub project: String, pub project: String,
pub token: String, pub token: String,
pub device_id: String, pub device_id: String,
@@ -64,6 +64,24 @@ impl Db {
Ok(Db { client }) Ok(Db { client })
} }
/// 通过project和device_id查询是否存在数据库
pub async fn exists_project(
&self,
project: &str,
device_id: &str,
) -> Result<Option<Authorize>, Box<dyn std::error::Error>> {
let row = Authorize::all()
.where_col(|a| a.project.equal(project))
.where_col(|a| a.device_id.equal(device_id))
.limit(1)
.run(&self.client)
.await?
.into_inners()
.into_iter()
.next();
Ok(row)
}
/// 判断 token 是否存在 /// 判断 token 是否存在
pub async fn verify_token( pub async fn verify_token(
&self, &self,

View File

@@ -67,7 +67,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
async fn create_token( async fn create_token(
State(state): State<AppState>, State(state): State<AppState>,
Query(args): Query<CreateToken>, Query(args): Query<CreateToken>,
) -> (StatusCode, Json<CreateResult>) { ) -> (StatusCode, Json<CreateTokenInfo>) {
let exists = state
.db
.exists_project(&args.project, &args.device_id)
.await
.unwrap();
match exists {
Some(info) => {
return (
StatusCode::OK,
Json(CreateTokenInfo {
code: 404,
project: info.project,
device_id: info.device_id,
token: info.token,
msg: "token已存在请勿重复创建".to_owned(),
}),
)
}
None => (),
}
let str_time = get_current_datetime(); let str_time = get_current_datetime();
let exp_time = add_day(&str_time, 7).unwrap(); let exp_time = add_day(&str_time, 7).unwrap();
@@ -85,11 +105,11 @@ async fn create_token(
( (
StatusCode::OK, StatusCode::OK,
Json(CreateResult { Json(CreateTokenInfo {
code: 200, code: 200,
project: args.project, project: args.project,
device_id: args.device_id, device_id: args.device_id,
token: token, token,
msg: "token创建成功".to_owned(), msg: "token创建成功".to_owned(),
}), }),
) )
@@ -150,15 +170,6 @@ struct CreateToken {
device_id: String, device_id: String,
} }
#[derive(Serialize)]
struct CreateResult {
code: i16,
project: String,
device_id: String,
token: String,
msg: String,
}
#[derive(Clone)] #[derive(Clone)]
struct AppState { struct AppState {
db: Arc<db::Db>, db: Arc<db::Db>,
@@ -197,3 +208,12 @@ enum TokenResponse {
Success(TokenInfo), Success(TokenInfo),
Error(QueryError), Error(QueryError),
} }
#[derive(Serialize)]
struct CreateTokenInfo {
code: i16,
project: String,
device_id: String,
token: String,
msg: String,
}