diff --git a/src/web/mod.rs b/src/web/mod.rs index 792484e..a20b404 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -107,16 +107,14 @@ async fn config_middleware( ) -> Result { let mut config = config.clone().as_ref().clone(); - fn set_from_cookie(config: &mut String, cookies: &CookieJar, name: &str) { - if let Some(cookie) = cookies.get(name) { - let value = cookie.value(); - *config = value.to_string(); + let settings_cookie = cookies.get("settings"); + if let Some(settings_cookie) = settings_cookie { + if let Ok(settings) = serde_json::from_str::(settings_cookie.value()) { + config.ui.stylesheet_url = settings.stylesheet_url; + config.ui.stylesheet_str = settings.stylesheet_str; } } - set_from_cookie(&mut config.ui.stylesheet_url, &cookies, "stylesheet-url"); - set_from_cookie(&mut config.ui.stylesheet_str, &cookies, "stylesheet-str"); - // modify the state req.extensions_mut().insert(config); diff --git a/src/web/settings.rs b/src/web/settings.rs index 1f34ed4..0fb0875 100644 --- a/src/web/settings.rs +++ b/src/web/settings.rs @@ -1,7 +1,7 @@ use axum::{ http::{header, StatusCode}, response::IntoResponse, Extension, Form}; use axum_extra::extract::{cookie::Cookie, CookieJar}; use maud::{html, Markup, PreEscaped, DOCTYPE}; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use crate::{config::Config, web::head_html}; @@ -60,19 +60,20 @@ pub async fn get( ( [(header::CONTENT_TYPE, "text/html; charset=utf-8")], html) } -#[derive(Deserialize)] +#[derive(Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub struct Settings { - stylesheet_url: String, - stylesheet_str: String, + pub stylesheet_url: String, + pub stylesheet_str: String, } pub async fn post( mut jar: CookieJar, Form(settings): Form, ) -> impl IntoResponse { - jar = jar.add(Cookie::new("stylesheet-url", settings.stylesheet_url)); - jar = jar.add(Cookie::new("stylesheet-str", settings.stylesheet_str)); + let mut settings_cookie = Cookie::new("settings", serde_json::to_string(&settings).unwrap()); + settings_cookie.make_permanent(); + jar = jar.add(settings_cookie); ( StatusCode::FOUND,