2024-11-12 20:25:43 +08:00
// main.rs
2024-11-09 00:05:18 +08:00
2024-11-11 19:31:40 +08:00
/**
* 主 程 序 入 口 , 提 供 数 据 库 连 接 和 相 关 API接口 。
*
* 接 口 :
* - GET / api / install : 测 试 数 据 库 连 接
* - GET / api / sql : 执 行 SQL查询并返回结果
* /
mod config ; // 配置模块
2024-11-16 23:03:55 +08:00
mod database ;
mod secret ;
use chrono ::Duration ;
// 数据库模块
2024-11-12 20:25:43 +08:00
use database ::relational ; // 引入关系型数据库
2024-11-11 19:31:40 +08:00
use once_cell ::sync ::Lazy ; // 用于延迟初始化
use rocket ::{ get , http ::Status , launch , response ::status , routes , serde ::json ::Json } ; // 引入Rocket框架相关功能
2024-11-16 23:03:55 +08:00
use std ::sync ::Arc ;
// 引入Arc用于线程安全的引用计数
2024-11-11 19:31:40 +08:00
use tokio ::sync ::Mutex ; // 引入Mutex用于异步锁
// 全局数据库连接变量
2024-11-12 20:25:43 +08:00
static DB : Lazy < Arc < Mutex < Option < relational ::Database > > > > = Lazy ::new ( | | Arc ::new ( Mutex ::new ( None ) ) ) ;
2024-11-09 00:05:18 +08:00
2024-11-11 19:31:40 +08:00
/**
* 初 始 化 数 据 库 连 接
* /
2024-11-12 20:25:43 +08:00
async fn init_db ( database : config ::DbConfig ) -> Result < ( ) , Box < dyn std ::error ::Error > > {
let database = relational ::Database ::init ( database ) . await ? ; // 初始化数据库
2024-11-11 19:31:40 +08:00
* DB . lock ( ) . await = Some ( database ) ; // 保存数据库实例
2024-11-11 01:38:58 +08:00
Ok ( ( ) )
2024-11-09 00:05:18 +08:00
}
2024-11-11 19:31:40 +08:00
/**
* 获 取 数 据 库 的 引 用
* /
2024-11-12 20:25:43 +08:00
async fn get_db ( ) -> Result < relational ::Database , Box < dyn std ::error ::Error > > {
2024-11-11 13:45:02 +08:00
DB . lock ( )
. await
. clone ( )
2024-11-11 19:31:40 +08:00
. ok_or_else ( | | " Database not initialized " . into ( ) ) // 返回错误信息
2024-11-09 00:05:18 +08:00
}
2024-11-11 19:31:40 +08:00
/**
* 测 试 数 据 库 接 口
2024-11-12 20:25:43 +08:00
* /
2024-11-09 00:05:18 +08:00
#[ get( " /sql " ) ]
2024-11-11 19:31:40 +08:00
async fn ssql ( ) -> Result < Json < Vec < std ::collections ::HashMap < String , String > > > , status ::Custom < String > > {
2024-11-11 01:38:58 +08:00
let db = get_db ( ) . await . map_err ( | e | {
2024-11-11 13:45:02 +08:00
status ::Custom (
Status ::InternalServerError ,
2024-11-11 19:31:40 +08:00
format! ( " Database error: {} " , e ) , // 数据库错误信息
2024-11-11 13:45:02 +08:00
)
2024-11-11 01:38:58 +08:00
} ) ? ;
2024-11-11 13:45:02 +08:00
let query_result = db
. get_db ( )
. query ( " SELECT * FROM info " . to_string ( ) )
2024-11-11 01:38:58 +08:00
. await
2024-11-11 13:45:02 +08:00
. map_err ( | e | status ::Custom ( Status ::InternalServerError , format! ( " Query error: {} " , e ) ) ) ? ;
2024-11-11 01:38:58 +08:00
2024-11-11 19:31:40 +08:00
Ok ( Json ( query_result ) ) // 返回查询结果
2024-11-09 00:05:18 +08:00
}
2024-11-11 19:31:40 +08:00
/**
* 数 据 库 安 装 接 口
* /
2024-11-11 01:38:58 +08:00
#[ get( " /install " ) ]
async fn install ( ) -> status ::Custom < String > {
2024-11-11 13:45:02 +08:00
get_db ( )
. await
2024-11-11 19:31:40 +08:00
. map ( | _ | status ::Custom ( Status ::Ok , " Database connected successfully " . into ( ) ) ) // 连接成功
2024-11-11 13:45:02 +08:00
. unwrap_or_else ( | e | {
status ::Custom (
Status ::InternalServerError ,
2024-11-11 19:31:40 +08:00
format! ( " Failed to connect: {} " , e ) , // 连接失败信息
2024-11-11 13:45:02 +08:00
)
} )
2024-11-11 01:38:58 +08:00
}
2024-11-11 19:31:40 +08:00
/**
* 启 动 Rocket应用
* /
2024-11-09 00:05:18 +08:00
#[ launch ]
async fn rocket ( ) -> _ {
2024-11-12 20:25:43 +08:00
let config = config ::Config ::read ( ) . expect ( " Failed to read config " ) ; // 读取配置
init_db ( config . db_config )
2024-11-11 13:45:02 +08:00
. await
2024-11-11 19:31:40 +08:00
. expect ( " Failed to connect to database " ) ; // 初始化数据库连接
2024-11-17 17:17:40 +08:00
rocket ::build ( ) . mount ( " / " , routes! [ install , ssql ] ) // 挂载API路由
2024-11-11 13:45:02 +08:00
}
2024-11-16 23:03:55 +08:00
// fn main(){
// // secret::generate_key().expect("msg");
// // 创建claims
// let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as i64;
// // 创建 Claims
// let claims = secret::CustomClaims {
// user_id: String::from("lsy"),
// device_ua: String::from("lsy"),
// };
// let t=String::from("eyJhbGciOiJFZERTQSJ9.eyJleHAiOjE3MzE3NTczMDMsIm5iZiI6MTczMTc1NzI4MywiaWF0IjoxNzMxNzU3MjgzLCJ1c2VyX2lkIjoibHN5IiwiZGV2aWNlX3VhIjoibHN5In0.C8t5XZFSKnnDVmc6WkY-gzGNSAP7lNAjP9yBjhdvIRO7r_QjDnfcm0INIqCt5cyvnRlE2rFJIx_axOfLx2QJAw");
// // 生成JWT
// let token = secret::generate_jwt(claims,Duration::seconds(20)).expect("msg");
// println!("{}", token);
// // 验证JWT
// let a=secret::validate_jwt(&t).expect("msg");
// println!("\n\n{}",a.user_id)
// }