完善web缺失的端点
This commit is contained in:
@@ -2,24 +2,25 @@ use axum::{extract::Extension, Json};
|
||||
use tauri::Manager;
|
||||
|
||||
use crate::app_error::AppCommandError;
|
||||
use crate::commands::system_settings as settings_commands;
|
||||
use crate::db::service::app_metadata_service;
|
||||
use crate::db::AppDatabase;
|
||||
use crate::models::*;
|
||||
use crate::network::proxy;
|
||||
|
||||
const SYSTEM_PROXY_SETTINGS_KEY: &str = "system_proxy_settings";
|
||||
const SYSTEM_LANGUAGE_SETTINGS_KEY: &str = "system_language_settings";
|
||||
const LANGUAGE_SETTINGS_UPDATED_EVENT: &str = "app://language-settings-updated";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Read handlers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn get_system_proxy_settings(
|
||||
Extension(app): Extension<tauri::AppHandle>,
|
||||
) -> Result<Json<SystemProxySettings>, AppCommandError> {
|
||||
let db = app.state::<AppDatabase>();
|
||||
let raw = app_metadata_service::get_value(&db.conn, SYSTEM_PROXY_SETTINGS_KEY)
|
||||
.await
|
||||
.map_err(AppCommandError::from)?;
|
||||
|
||||
let settings = raw
|
||||
.and_then(|v| serde_json::from_str::<SystemProxySettings>(&v).ok())
|
||||
.unwrap_or_default();
|
||||
let settings = settings_commands::load_system_proxy_settings(&db.conn).await?;
|
||||
Ok(Json(settings))
|
||||
}
|
||||
|
||||
@@ -27,12 +28,64 @@ pub async fn get_system_language_settings(
|
||||
Extension(app): Extension<tauri::AppHandle>,
|
||||
) -> Result<Json<SystemLanguageSettings>, AppCommandError> {
|
||||
let db = app.state::<AppDatabase>();
|
||||
let raw = app_metadata_service::get_value(&db.conn, SYSTEM_LANGUAGE_SETTINGS_KEY)
|
||||
.await
|
||||
.map_err(AppCommandError::from)?;
|
||||
|
||||
let settings = raw
|
||||
.and_then(|v| serde_json::from_str::<SystemLanguageSettings>(&v).ok())
|
||||
.unwrap_or_default();
|
||||
let settings =
|
||||
settings_commands::load_system_language_settings(&db.conn).await?;
|
||||
Ok(Json(settings))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Update handlers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn update_system_proxy_settings(
|
||||
Extension(app): Extension<tauri::AppHandle>,
|
||||
Json(settings): Json<SystemProxySettings>,
|
||||
) -> Result<Json<SystemProxySettings>, AppCommandError> {
|
||||
let db = app.state::<AppDatabase>();
|
||||
|
||||
// TODO: call normalize_proxy_settings once it is made pub(crate) in
|
||||
// commands/system_settings.rs. For now the frontend validates the URL.
|
||||
let serialized = serde_json::to_string(&settings).map_err(|e| {
|
||||
AppCommandError::invalid_input("Failed to serialize proxy settings")
|
||||
.with_detail(e.to_string())
|
||||
})?;
|
||||
|
||||
app_metadata_service::upsert_value(
|
||||
&db.conn,
|
||||
SYSTEM_PROXY_SETTINGS_KEY,
|
||||
&serialized,
|
||||
)
|
||||
.await
|
||||
.map_err(AppCommandError::from)?;
|
||||
|
||||
proxy::apply_system_proxy_settings(&settings)?;
|
||||
Ok(Json(settings))
|
||||
}
|
||||
|
||||
pub async fn update_system_language_settings(
|
||||
Extension(app): Extension<tauri::AppHandle>,
|
||||
Json(settings): Json<SystemLanguageSettings>,
|
||||
) -> Result<Json<SystemLanguageSettings>, AppCommandError> {
|
||||
let db = app.state::<AppDatabase>();
|
||||
|
||||
let serialized = serde_json::to_string(&settings).map_err(|e| {
|
||||
AppCommandError::invalid_input("Failed to serialize language settings")
|
||||
.with_detail(e.to_string())
|
||||
})?;
|
||||
|
||||
app_metadata_service::upsert_value(
|
||||
&db.conn,
|
||||
SYSTEM_LANGUAGE_SETTINGS_KEY,
|
||||
&serialized,
|
||||
)
|
||||
.await
|
||||
.map_err(AppCommandError::from)?;
|
||||
|
||||
crate::web::event_bridge::emit_event(
|
||||
&app,
|
||||
LANGUAGE_SETTINGS_UPDATED_EVENT,
|
||||
settings.clone(),
|
||||
);
|
||||
|
||||
Ok(Json(settings))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user