2024-11-26 12:19:57 +08:00
|
|
|
use super::{settings, users};
|
2024-11-28 23:10:00 +08:00
|
|
|
use crate::common::config;
|
|
|
|
use crate::common::error::{AppResult, AppResultInto};
|
|
|
|
use crate::common::helpers;
|
2024-11-26 12:19:57 +08:00
|
|
|
use crate::security;
|
|
|
|
use crate::storage::sql;
|
2024-11-21 19:07:42 +08:00
|
|
|
use crate::AppState;
|
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
|
|
|
|
2024-11-28 23:10:00 +08:00
|
|
|
#[post("/sql", format = "application/json", data = "<sql_config>")]
|
|
|
|
pub async fn setup_sql(
|
|
|
|
sql_config: Json<config::SqlConfig>,
|
|
|
|
state: &State<Arc<AppState>>,
|
|
|
|
) -> AppResult<String> {
|
|
|
|
let mut config = config::Config::read().unwrap_or_default();
|
|
|
|
if config.init.sql {
|
|
|
|
return Err(status::Custom(
|
|
|
|
Status::BadRequest,
|
2024-11-30 02:15:46 +08:00
|
|
|
"数据库已经初始化".to_string(),
|
2024-11-28 23:10:00 +08:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let sql_config = sql_config.into_inner();
|
|
|
|
|
|
|
|
config.init.sql = true;
|
|
|
|
config.sql_config = sql_config.clone();
|
|
|
|
sql::Database::initial_setup(sql_config.clone())
|
|
|
|
.await
|
|
|
|
.into_app_result()?;
|
|
|
|
|
|
|
|
config::Config::write(config).into_app_result()?;
|
2024-11-30 14:03:32 +08:00
|
|
|
state.trigger_restart().await.into_app_result()?;
|
2024-11-28 23:10:00 +08:00
|
|
|
Ok("Database installation successful".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
pub struct StepAccountData {
|
2024-11-26 12:19:57 +08:00
|
|
|
username: String,
|
2024-11-21 19:07:42 +08:00
|
|
|
email: String,
|
|
|
|
password: String,
|
2024-11-20 19:53:18 +08:00
|
|
|
}
|
2024-11-28 23:10:00 +08:00
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
2024-11-21 19:07:42 +08:00
|
|
|
pub struct InstallReplyData {
|
|
|
|
token: String,
|
2024-11-26 12:19:57 +08:00
|
|
|
username: String,
|
2024-11-21 19:07:42 +08:00
|
|
|
password: String,
|
2024-11-20 19:53:18 +08:00
|
|
|
}
|
|
|
|
|
2024-11-28 23:10:00 +08:00
|
|
|
#[post("/administrator", format = "application/json", data = "<data>")]
|
|
|
|
pub async fn setup_account(
|
|
|
|
data: Json<StepAccountData>,
|
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-30 02:15:46 +08:00
|
|
|
if config.init.administrator {
|
2024-11-21 19:07:42 +08:00
|
|
|
return Err(status::Custom(
|
|
|
|
Status::BadRequest,
|
2024-11-30 02:15:46 +08:00
|
|
|
"管理员用户已设置".to_string(),
|
2024-11-21 19:07:42 +08:00
|
|
|
));
|
2024-11-20 19:53:18 +08:00
|
|
|
}
|
2024-11-28 23:10:00 +08:00
|
|
|
|
|
|
|
security::jwt::generate_key().into_app_result()?;
|
|
|
|
|
2024-11-21 19:07:42 +08:00
|
|
|
let data = data.into_inner();
|
2024-11-28 23:10:00 +08:00
|
|
|
|
2024-11-25 22:43:24 +08:00
|
|
|
let sql = {
|
2024-11-28 23:10:00 +08:00
|
|
|
state.sql_link(&config.sql_config).await.into_app_result()?;
|
2024-11-25 22:43:24 +08:00
|
|
|
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 = (
|
2024-11-26 12:19:57 +08:00
|
|
|
helpers::generate_random_string(20),
|
|
|
|
helpers::generate_random_string(20),
|
2024-11-25 22:43:24 +08:00
|
|
|
);
|
2024-11-20 19:53:18 +08:00
|
|
|
|
2024-11-26 12:19:57 +08:00
|
|
|
users::insert_user(
|
2024-11-21 19:07:42 +08:00
|
|
|
&sql,
|
2024-11-26 12:19:57 +08:00
|
|
|
users::RegisterData {
|
|
|
|
username: data.username.clone(),
|
2024-11-21 19:07:42 +08:00
|
|
|
email: data.email,
|
|
|
|
password: data.password,
|
2024-11-26 12:19:57 +08:00
|
|
|
role: "administrator".to_string(),
|
2024-11-21 19:07:42 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
2024-11-25 03:36:24 +08:00
|
|
|
.into_app_result()?;
|
|
|
|
|
2024-11-26 12:19:57 +08:00
|
|
|
users::insert_user(
|
2024-11-21 19:07:42 +08:00
|
|
|
&sql,
|
2024-11-26 12:19:57 +08:00
|
|
|
users::RegisterData {
|
|
|
|
username: system_credentials.0.clone(),
|
2024-11-25 22:43:24 +08:00
|
|
|
email: "author@lsy22.com".to_string(),
|
|
|
|
password: system_credentials.1.clone(),
|
2024-11-26 12:19:57 +08:00
|
|
|
role: "administrator".to_string(),
|
2024-11-21 19:07:42 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await
|
2024-11-25 03:36:24 +08:00
|
|
|
.into_app_result()?;
|
|
|
|
|
2024-11-26 12:19:57 +08:00
|
|
|
settings::insert_setting(
|
2024-11-25 22:43:24 +08:00
|
|
|
&sql,
|
|
|
|
"system".to_string(),
|
2024-11-26 12:19:57 +08:00
|
|
|
"settings".to_string(),
|
|
|
|
Json(json!(settings::SystemConfigure {
|
|
|
|
author_name: data.username.clone(),
|
|
|
|
..settings::SystemConfigure::default()
|
2024-11-25 22:43:24 +08:00
|
|
|
})),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.into_app_result()?;
|
2024-11-25 17:59:18 +08:00
|
|
|
|
2024-11-26 12:19:57 +08:00
|
|
|
let token = security::jwt::generate_jwt(
|
2024-11-28 23:10:00 +08:00
|
|
|
security::jwt::CustomClaims {
|
|
|
|
name: data.username,
|
|
|
|
},
|
2024-11-21 19:07:42 +08:00
|
|
|
Duration::days(7),
|
|
|
|
)
|
2024-11-25 03:36:24 +08:00
|
|
|
.into_app_result()?;
|
2024-11-30 02:15:46 +08:00
|
|
|
config.init.administrator = true;
|
2024-11-25 22:43:24 +08:00
|
|
|
config::Config::write(config).into_app_result()?;
|
2024-11-30 14:03:32 +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,
|
2024-11-26 12:19:57 +08:00
|
|
|
username: system_credentials.0,
|
2024-11-25 22:43:24 +08:00
|
|
|
password: system_credentials.1,
|
2024-11-21 19:07:42 +08:00
|
|
|
}),
|
|
|
|
))
|
|
|
|
}
|