2024-11-21 19:07:42 +08:00
|
|
|
use crate::auth;
|
2024-11-21 01:04:59 +08:00
|
|
|
use crate::database::relational;
|
2024-11-25 03:36:24 +08:00
|
|
|
use crate::error::{AppResult, AppResultInto};
|
2024-11-25 17:59:18 +08:00
|
|
|
use super::{person, configure};
|
2024-11-21 19:07:42 +08:00
|
|
|
use crate::AppState;
|
|
|
|
use crate::{config, utils};
|
2024-11-21 01:04:59 +08:00
|
|
|
use chrono::Duration;
|
2024-11-21 19:07:42 +08:00
|
|
|
use rocket::{http::Status, post, response::status, serde::json::Json, State};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2024-11-25 03:36:24 +08:00
|
|
|
use std::sync::Arc;
|
2024-11-25 17:59:18 +08:00
|
|
|
use serde_json::json;
|
2024-11-20 19:53:18 +08:00
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
2024-11-21 19:07:42 +08:00
|
|
|
pub struct InstallData {
|
|
|
|
name: String,
|
|
|
|
email: String,
|
|
|
|
password: String,
|
|
|
|
sql_config: config::SqlConfig,
|
2024-11-20 19:53:18 +08:00
|
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize)]
|
2024-11-21 19:07:42 +08:00
|
|
|
pub struct InstallReplyData {
|
|
|
|
token: String,
|
|
|
|
name: String,
|
|
|
|
password: String,
|
2024-11-20 19:53:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/install", format = "application/json", data = "<data>")]
|
2024-11-21 01:04:59 +08:00
|
|
|
pub async fn install(
|
2024-11-20 19:53:18 +08:00
|
|
|
data: Json<InstallData>,
|
2024-11-25 03:36:24 +08:00
|
|
|
state: &State<Arc<AppState>>,
|
2024-11-21 19:07:42 +08:00
|
|
|
) -> AppResult<status::Custom<Json<InstallReplyData>>> {
|
2024-11-25 17:59:18 +08:00
|
|
|
let mut config = config::Config::read().unwrap_or_default();
|
2024-11-20 19:53:18 +08:00
|
|
|
if config.info.install {
|
2024-11-21 19:07:42 +08:00
|
|
|
return Err(status::Custom(
|
|
|
|
Status::BadRequest,
|
|
|
|
"Database already initialized".to_string(),
|
|
|
|
));
|
2024-11-20 19:53:18 +08:00
|
|
|
}
|
|
|
|
|
2024-11-25 17:59:18 +08:00
|
|
|
config.info.install = true;
|
|
|
|
config.sql_config = data.sql_config.clone();
|
|
|
|
|
2024-11-21 19:07:42 +08:00
|
|
|
let data = data.into_inner();
|
2024-11-20 19:53:18 +08:00
|
|
|
|
|
|
|
relational::Database::initial_setup(data.sql_config.clone())
|
|
|
|
.await
|
2024-11-25 03:36:24 +08:00
|
|
|
.into_app_result()?;
|
2024-11-21 11:47:41 +08:00
|
|
|
|
2024-11-21 19:07:42 +08:00
|
|
|
let _ = auth::jwt::generate_key();
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-25 17:59:18 +08:00
|
|
|
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-25 03:36:24 +08:00
|
|
|
state.sql_link(&data.sql_config).await.into_app_result()?;
|
|
|
|
let sql = state.sql_get().await.into_app_result()?;
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-21 19:07:42 +08:00
|
|
|
let system_name = utils::generate_random_string(20);
|
|
|
|
let system_password = utils::generate_random_string(20);
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-21 19:07:42 +08:00
|
|
|
let _ = person::insert(
|
|
|
|
&sql,
|
|
|
|
person::RegisterData {
|
|
|
|
name: data.name.clone(),
|
|
|
|
email: data.email,
|
|
|
|
password: data.password,
|
2024-11-25 03:36:24 +08:00
|
|
|
level: "administrators".to_string(),
|
2024-11-21 19:07:42 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
2024-11-25 03:36:24 +08:00
|
|
|
.into_app_result()?;
|
|
|
|
|
2024-11-21 19:07:42 +08:00
|
|
|
let _ = person::insert(
|
|
|
|
&sql,
|
|
|
|
person::RegisterData {
|
|
|
|
name: system_name.clone(),
|
|
|
|
email: String::from("author@lsy22.com"),
|
2024-11-25 03:36:24 +08:00
|
|
|
password: system_password.clone(),
|
|
|
|
level: "administrators".to_string(),
|
2024-11-21 19:07:42 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
2024-11-25 03:36:24 +08:00
|
|
|
.into_app_result()?;
|
|
|
|
|
2024-11-25 17:59:18 +08:00
|
|
|
let mut system_configure = configure::SystemConfigure::default();
|
|
|
|
system_configure.author_name = data.name.clone();
|
|
|
|
|
|
|
|
configure::insert_configure(&sql, "system".to_string(), "configure".to_string(), Json(json!(system_configure))).await.into_app_result()?;
|
|
|
|
|
|
|
|
|
2024-11-21 01:04:59 +08:00
|
|
|
let token = auth::jwt::generate_jwt(
|
2024-11-21 19:07:42 +08:00
|
|
|
auth::jwt::CustomClaims {
|
|
|
|
name: data.name.clone(),
|
|
|
|
},
|
|
|
|
Duration::days(7),
|
|
|
|
)
|
2024-11-25 03:36:24 +08:00
|
|
|
.into_app_result()?;
|
|
|
|
|
|
|
|
config::Config::write(config.clone()).into_app_result()?;
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-25 03:36:24 +08:00
|
|
|
state.trigger_restart().await.into_app_result()?;
|
2024-11-21 19:07:42 +08:00
|
|
|
Ok(status::Custom(
|
|
|
|
Status::Ok,
|
|
|
|
Json(InstallReplyData {
|
|
|
|
token: token,
|
|
|
|
name: system_name,
|
|
|
|
password: system_password,
|
|
|
|
}),
|
|
|
|
))
|
|
|
|
}
|