move settings to a single cookie and fix it to be permanent
This commit is contained in:
parent
ae7ab53ab1
commit
fdf8163fbb
@ -107,16 +107,14 @@ async fn config_middleware(
|
||||
) -> Result<Response, StatusCode> {
|
||||
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::Settings>(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);
|
||||
|
||||
|
@ -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<Settings>,
|
||||
) -> 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,
|
||||
|
Loading…
Reference in New Issue
Block a user