From 9edafd94b00a73662d51824dbcba0a018e2140cf Mon Sep 17 00:00:00 2001 From: alyx Date: Thu, 10 Aug 2023 20:45:01 -0400 Subject: Create plain theme, do some escaping, fix some busted envvars, document themes. --- src/config.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index aad298f..ab92108 100644 --- a/src/config.rs +++ b/src/config.rs @@ -11,17 +11,17 @@ use super::deserialize::{GetRecentTracks, GetUserInfo, Track, User}; use reqwest::{Client, StatusCode}; use dotenv::var; use tokio::sync::RwLock; -use handlebars::Handlebars; +use handlebars::{Handlebars, handlebars_helper}; use duration_str as ds; -static INTERNAL_THEMES: &[(&'static str, &'static str)] = &[("plain", "")]; +static INTERNAL_THEMES: &[(&'static str, &'static str)] = &[("plain", include_str!("themes/plain.hbs"))]; pub static STATE: LazyLock> = LazyLock::new(|| { State::new() }); fn getter(username: &String) -> Pin, (StatusCode, &'static str)>> + Send + Sync)>> { - let username = username.clone(); + let username = urlencoding::encode(username.as_ref()).to_string(); Box::pin(async move { let userreq = STATE.http.get(format!("https://ws.audioscrobbler.com/2.0/?method=user.getInfo&format=json&user={username}&api_key={}", STATE.api_key)) .send().await @@ -84,6 +84,15 @@ impl State { handlebars: { let mut hb = Handlebars::new(); + + handlebars_helper!(html_escape: |s: String| htmlize::escape_text(s)); + handlebars_helper!(html_attr_escape: |s: String| htmlize::escape_attribute(s)); + handlebars_helper!(url_encode: |s: String| urlencoding::encode(&s)); + + hb.register_helper("html-escape", Box::new(html_escape)); + hb.register_helper("html-attr-escape", Box::new(html_attr_escape)); + hb.register_helper("url-encode", Box::new(html_attr_escape)); + for (key, fulltext) in INTERNAL_THEMES { log::info!(target: "lfm::config::theme", "Registering internal theme `{key}`"); hb.register_template_string(key, fulltext).unwrap(); @@ -92,7 +101,7 @@ impl State { if let Ok(themes_dir) = var("LFME_THEME_DIR") { log::info!(target: "lfm::config::theme", "Registering theme dir `{themes_dir}`"); - hb.register_templates_directory(&var("LFME_THEME_EXT").unwrap_or_else(|_| "hbs".into()), themes_dir).unwrap(); + hb.register_templates_directory(&var("LFME_THEME_EXT").unwrap_or_else(|_| ".hbs".into()), themes_dir).unwrap(); } hb @@ -118,8 +127,8 @@ impl State { } } }, - default_refresh: default_refresh + Duration::from_secs(5), - whitelist_refresh: whitelist_refresh + Duration::from_secs(5) + default_refresh: default_refresh + Duration::from_secs(1), + whitelist_refresh: whitelist_refresh + Duration::from_secs(1) }) } -- cgit v1.2.3-54-g00ecf