2024-11-22 13:13:04 +08:00
|
|
|
mod auth;
|
2024-11-19 00:20:31 +08:00
|
|
|
mod config;
|
|
|
|
mod database;
|
2024-11-20 19:53:18 +08:00
|
|
|
mod routes;
|
2024-11-22 13:13:04 +08:00
|
|
|
mod utils;
|
2024-11-19 00:20:31 +08:00
|
|
|
use database::relational;
|
2024-11-25 03:36:24 +08:00
|
|
|
use rocket::Shutdown;
|
2024-11-19 00:20:31 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::Mutex;
|
2024-11-25 03:36:24 +08:00
|
|
|
mod error;
|
|
|
|
use error::{CustomErrorInto, CustomResult};
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-25 03:36:24 +08:00
|
|
|
pub struct AppState {
|
2024-11-20 19:53:18 +08:00
|
|
|
db: Arc<Mutex<Option<relational::Database>>>,
|
|
|
|
configure: Arc<Mutex<config::Config>>,
|
2024-11-25 03:36:24 +08:00
|
|
|
shutdown: Arc<Mutex<Option<Shutdown>>>,
|
|
|
|
restart_progress: Arc<Mutex<bool>>,
|
2024-11-09 00:05:18 +08:00
|
|
|
}
|
2024-11-11 19:31:40 +08:00
|
|
|
|
2024-11-20 19:53:18 +08:00
|
|
|
impl AppState {
|
2024-11-25 03:36:24 +08:00
|
|
|
pub fn new(config: config::Config) -> Self {
|
|
|
|
Self {
|
|
|
|
db: Arc::new(Mutex::new(None)),
|
|
|
|
configure: Arc::new(Mutex::new(config)),
|
|
|
|
shutdown: Arc::new(Mutex::new(None)),
|
|
|
|
restart_progress: Arc::new(Mutex::new(false)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn sql_get(&self) -> CustomResult<relational::Database> {
|
2024-11-20 19:53:18 +08:00
|
|
|
self.db
|
|
|
|
.lock()
|
|
|
|
.await
|
|
|
|
.clone()
|
2024-11-25 03:36:24 +08:00
|
|
|
.ok_or("数据库未连接".into_custom_error())
|
2024-11-20 19:53:18 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 03:36:24 +08:00
|
|
|
pub async fn sql_link(&self, config: &config::SqlConfig) -> CustomResult<()> {
|
2024-11-22 13:13:04 +08:00
|
|
|
let database = relational::Database::link(config).await?;
|
2024-11-20 19:53:18 +08:00
|
|
|
*self.db.lock().await = Some(database);
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-11-18 01:09:28 +08:00
|
|
|
|
2024-11-25 03:36:24 +08:00
|
|
|
pub async fn set_shutdown(&self, shutdown: Shutdown) {
|
|
|
|
*self.shutdown.lock().await = Some(shutdown);
|
|
|
|
}
|
2024-11-22 13:13:04 +08:00
|
|
|
|
2024-11-25 03:36:24 +08:00
|
|
|
pub async fn trigger_restart(&self) -> CustomResult<()> {
|
|
|
|
*self.restart_progress.lock().await = true;
|
|
|
|
|
|
|
|
self.shutdown
|
|
|
|
.lock()
|
|
|
|
.await
|
|
|
|
.take()
|
|
|
|
.ok_or("未能获取rocket的shutdown".into_custom_error())?
|
|
|
|
.notify();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rocket::main]
|
|
|
|
async fn main() -> CustomResult<()> {
|
|
|
|
let config = config::Config::read()?;
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-25 03:36:24 +08:00
|
|
|
let state = AppState::new(config.clone());
|
2024-11-18 19:14:37 +08:00
|
|
|
|
|
|
|
if config.info.install {
|
2024-11-25 03:36:24 +08:00
|
|
|
state.sql_link(&config.sql_config).await?;
|
2024-11-18 19:14:37 +08:00
|
|
|
}
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-25 03:36:24 +08:00
|
|
|
let state = Arc::new(state);
|
|
|
|
|
|
|
|
let rocket_builder = rocket::build().manage(state.clone());
|
|
|
|
|
|
|
|
let rocket_builder = if !config.info.install {
|
|
|
|
rocket_builder.mount("/", rocket::routes![routes::install::install])
|
|
|
|
} else {
|
|
|
|
rocket_builder.mount("/auth/token", routes::jwt_routes())
|
|
|
|
};
|
|
|
|
|
|
|
|
let rocket = rocket_builder.ignite().await?;
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-25 03:36:24 +08:00
|
|
|
rocket
|
|
|
|
.state::<Arc<AppState>>()
|
|
|
|
.ok_or("未能获取AppState".into_custom_error())?
|
|
|
|
.set_shutdown(rocket.shutdown())
|
|
|
|
.await;
|
|
|
|
|
|
|
|
rocket.launch().await?;
|
|
|
|
|
|
|
|
let restart_progress = *state.restart_progress.lock().await;
|
|
|
|
if restart_progress {
|
|
|
|
let current_exe = std::env::current_exe()?;
|
|
|
|
let _ = std::process::Command::new(current_exe).spawn();
|
|
|
|
}
|
|
|
|
std::process::exit(0);
|
2024-11-21 01:04:59 +08:00
|
|
|
}
|