2024-11-25 22:43:24 +08:00
|
|
|
use super::{configure, person};
|
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-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 17:59:18 +08:00
|
|
|
use serde_json::json;
|
2024-11-25 22:43:24 +08:00
|
|
|
use std::sync::Arc;
|
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-21 19:07:42 +08:00
|
|
|
let data = data.into_inner();
|
2024-11-25 22:43:24 +08:00
|
|
|
let sql = {
|
|
|
|
config.info.install = true;
|
|
|
|
config.sql_config = data.sql_config.clone();
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-25 22:43:24 +08:00
|
|
|
relational::Database::initial_setup(data.sql_config.clone())
|
|
|
|
.await
|
|
|
|
.into_app_result()?;
|
|
|
|
auth::jwt::generate_key().into_app_result()?;
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-25 22:43:24 +08:00
|
|
|
state.sql_link(&data.sql_config).await.into_app_result()?;
|
|
|
|
state.sql_get().await.into_app_result()?
|
|
|
|
};
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-25 22:43:24 +08:00
|
|
|
let system_credentials = (
|
|
|
|
utils::generate_random_string(20),
|
|
|
|
utils::generate_random_string(20),
|
|
|
|
);
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-25 22:43:24 +08:00
|
|
|
person::insert(
|
2024-11-21 19:07:42 +08:00
|
|
|
&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-25 22:43:24 +08:00
|
|
|
person::insert(
|
2024-11-21 19:07:42 +08:00
|
|
|
&sql,
|
|
|
|
person::RegisterData {
|
2024-11-25 22:43:24 +08:00
|
|
|
name: system_credentials.0.clone(),
|
|
|
|
email: "author@lsy22.com".to_string(),
|
|
|
|
password: system_credentials.1.clone(),
|
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-25 22:43:24 +08:00
|
|
|
configure::insert_configure(
|
|
|
|
&sql,
|
|
|
|
"system".to_string(),
|
|
|
|
"config".to_string(),
|
|
|
|
Json(json!(configure::SystemConfigure {
|
|
|
|
author_name: data.name.clone(),
|
|
|
|
..configure::SystemConfigure::default()
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.into_app_result()?;
|
2024-11-25 17:59:18 +08:00
|
|
|
|
2024-11-21 01:04:59 +08:00
|
|
|
let token = auth::jwt::generate_jwt(
|
2024-11-25 22:43:24 +08:00
|
|
|
auth::jwt::CustomClaims { name: data.name },
|
2024-11-21 19:07:42 +08:00
|
|
|
Duration::days(7),
|
|
|
|
)
|
2024-11-25 03:36:24 +08:00
|
|
|
.into_app_result()?;
|
|
|
|
|
2024-11-25 22:43:24 +08:00
|
|
|
config::Config::write(config).into_app_result()?;
|
2024-11-25 03:36:24 +08:00
|
|
|
state.trigger_restart().await.into_app_result()?;
|
2024-11-25 22:43:24 +08:00
|
|
|
|
2024-11-21 19:07:42 +08:00
|
|
|
Ok(status::Custom(
|
|
|
|
Status::Ok,
|
|
|
|
Json(InstallReplyData {
|
2024-11-25 22:43:24 +08:00
|
|
|
token,
|
|
|
|
name: system_credentials.0,
|
|
|
|
password: system_credentials.1,
|
2024-11-21 19:07:42 +08:00
|
|
|
}),
|
|
|
|
))
|
|
|
|
}
|