From 3b704a24a771febd4963c8211dbc87a9691f550a Mon Sep 17 00:00:00 2001 From: Nils Pukropp Date: Sat, 9 May 2026 11:12:47 +0200 Subject: [PATCH] added partial settings updates --- backend/src/handlers/config.rs | 27 +++++++++++++++++++++++---- backend/src/models.rs | 20 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/backend/src/handlers/config.rs b/backend/src/handlers/config.rs index be6c1ea..379e47a 100644 --- a/backend/src/handlers/config.rs +++ b/backend/src/handlers/config.rs @@ -2,7 +2,12 @@ use axum::{Json, extract::State, http::HeaderMap, response::IntoResponse}; use std::{fs, sync::Arc}; use tracing::error; -use crate::{AppState, auth::check_auth, error::AppError, models::SiteConfig}; +use crate::{ + AppState, + auth::check_auth, + error::AppError, + models::{SiteConfig, SiteConfigPatch}, +}; pub async fn get_config(State(state): State>) -> impl IntoResponse { let config_path = state.data_dir.join("config.json"); @@ -17,12 +22,26 @@ pub async fn get_config(State(state): State>) -> impl IntoResponse pub async fn update_config( State(state): State>, headers: HeaderMap, - Json(payload): Json, + Json(patch): Json, ) -> Result, AppError> { check_auth(&headers, &state.admin_token)?; let config_path = state.data_dir.join("config.json"); - let config_str = serde_json::to_string_pretty(&payload).map_err(|e| { + let mut config: SiteConfig = fs::read_to_string(&config_path) + .ok() + .and_then(|c| serde_json::from_str(&c).ok()) + .unwrap_or_default(); + + if let Some(v) = patch.title { config.title = v; } + if let Some(v) = patch.subtitle { config.subtitle = v; } + if let Some(v) = patch.welcome_title { config.welcome_title = v; } + if let Some(v) = patch.welcome_subtitle { config.welcome_subtitle = v; } + if let Some(v) = patch.footer { config.footer = v; } + if let Some(v) = patch.favicon { config.favicon = v; } + if let Some(v) = patch.theme { config.theme = v; } + if let Some(v) = patch.custom_css { config.custom_css = v; } + + let config_str = serde_json::to_string_pretty(&config).map_err(|e| { error!("Serialization error: {}", e); AppError::Internal("Serialization error".to_string(), Some(e.to_string())) })?; @@ -32,5 +51,5 @@ pub async fn update_config( AppError::Internal("Write error".to_string(), Some(e.to_string())) })?; - Ok(Json(payload)) + Ok(Json(config)) } diff --git a/backend/src/models.rs b/backend/src/models.rs index 6ebe68d..df201c0 100644 --- a/backend/src/models.rs +++ b/backend/src/models.rs @@ -29,6 +29,26 @@ impl Default for SiteConfig { } } +#[derive(Deserialize, Default)] +pub struct SiteConfigPatch { + #[serde(default)] + pub title: Option, + #[serde(default)] + pub subtitle: Option, + #[serde(default)] + pub welcome_title: Option, + #[serde(default)] + pub welcome_subtitle: Option, + #[serde(default)] + pub footer: Option, + #[serde(default)] + pub favicon: Option, + #[serde(default)] + pub theme: Option, + #[serde(default)] + pub custom_css: Option, +} + #[derive(Serialize, Deserialize, Clone, Default)] pub struct PostMeta { pub date: NaiveDate,