2024-11-11 01:38:58 +08:00
|
|
|
// main.rs
|
2024-11-09 00:05:18 +08:00
|
|
|
mod config;
|
2024-11-11 13:45:02 +08:00
|
|
|
mod sql;
|
|
|
|
use crate::sql::Database;
|
2024-11-09 00:05:18 +08:00
|
|
|
use once_cell::sync::Lazy;
|
2024-11-11 13:45:02 +08:00
|
|
|
use rocket::{get, http::Status, launch, response::status, routes, serde::json::Json};
|
|
|
|
use std::sync::Arc;
|
2024-11-11 01:38:58 +08:00
|
|
|
use tokio::sync::Mutex;
|
2024-11-09 00:05:18 +08:00
|
|
|
|
2024-11-11 13:45:02 +08:00
|
|
|
/* 修改全局变量的类型定义 */
|
|
|
|
static DB: Lazy<Arc<Mutex<Option<Database>>>> = Lazy::new(|| Arc::new(Mutex::new(None)));
|
2024-11-09 00:05:18 +08:00
|
|
|
|
2024-11-11 13:45:02 +08:00
|
|
|
/* 数据库连接函数 */
|
|
|
|
async fn init_db(database: config::Database) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let database = Database::init(database).await?;
|
|
|
|
*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 13:45:02 +08:00
|
|
|
/* 获取数据库的引用 */
|
2024-11-11 01:38:58 +08:00
|
|
|
async fn get_db() -> Result<Database, Box<dyn std::error::Error>> {
|
2024-11-11 13:45:02 +08:00
|
|
|
DB.lock()
|
|
|
|
.await
|
|
|
|
.clone()
|
|
|
|
.ok_or_else(|| "Database not initialized".into())
|
2024-11-09 00:05:18 +08:00
|
|
|
}
|
2024-11-11 13:45:02 +08:00
|
|
|
/* 用于测试数据库 */
|
2024-11-09 00:05:18 +08:00
|
|
|
#[get("/sql")]
|
2024-11-11 13:45:02 +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,
|
|
|
|
format!("Database error: {}", e),
|
|
|
|
)
|
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
|
|
|
|
|
|
|
Ok(Json(query_result))
|
2024-11-09 00:05:18 +08:00
|
|
|
}
|
2024-11-11 13:45:02 +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
|
|
|
|
.map(|_| status::Custom(Status::Ok, "Database connected successfully".into()))
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
status::Custom(
|
|
|
|
Status::InternalServerError,
|
|
|
|
format!("Failed to connect: {}", e),
|
|
|
|
)
|
|
|
|
})
|
2024-11-11 01:38:58 +08:00
|
|
|
}
|
2024-11-11 13:45:02 +08:00
|
|
|
/* 启动函数 */
|
2024-11-09 00:05:18 +08:00
|
|
|
#[launch]
|
|
|
|
async fn rocket() -> _ {
|
2024-11-11 13:45:02 +08:00
|
|
|
let config = config::Config::read("./src/config/config.toml").expect("Failed to read config");
|
|
|
|
init_db(config.database)
|
|
|
|
.await
|
|
|
|
.expect("Failed to connect to database");
|
|
|
|
rocket::build().mount("/api", routes![install, ssql])
|
|
|
|
}
|