From 52aeff65a82d949dd3ce33d1a5998e00ad4c379e Mon Sep 17 00:00:00 2001 From: alyx Date: Tue, 2 Apr 2024 11:51:14 -0400 Subject: Theming refactor Theming has been broken off into a seperate space, so that it'll be easier to add lua support later. --- src/config.rs | 45 +++++++++++++-------------------------------- src/ctx.rs | 2 ++ src/lib.rs | 1 + src/main.rs | 26 ++++++++++++++++---------- src/themes/plain.hbs | 39 --------------------------------------- src/theming.rs | 22 ++++++++++++++++++++++ src/theming/hbs.rs | 35 +++++++++++++++++++++++++++++++++++ src/theming/hbs/plain.hbs | 39 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 128 insertions(+), 81 deletions(-) delete mode 100644 src/themes/plain.hbs create mode 100644 src/theming.rs create mode 100644 src/theming/hbs.rs create mode 100644 src/theming/hbs/plain.hbs diff --git a/src/config.rs b/src/config.rs index b242534..d3e2b8a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,25 +4,24 @@ use std::sync::Arc; use std::time::*; use dotenv::var; -use handlebars::{Handlebars, handlebars_helper}; use duration_str as ds; -static INTERNAL_THEMES: &[(&str, &str)] = &[("plain", include_str!("themes/plain.hbs"))]; - pub static CONFIG: LazyLock> = LazyLock::new(|| { Config::new() }); #[derive(Debug)] pub struct Config { + pub(crate) google_api_key: Option>, pub(crate) lastfm_api_key: Arc, + port: u16, - default_theme: Arc, send_refresh_header: bool, - handlebars: Handlebars<'static>, - - pub(crate )google_api_key: Option>, + pub(crate) default_theme: Arc, + pub(crate) theme_dir: Option>, + pub(crate) theme_ext: Arc, + pub(crate) theme_debug: bool, pub(crate) whitelist: BTreeSet, pub(crate) whitelist_mode: String, @@ -37,31 +36,15 @@ impl Config { let whitelist_refresh = duration_from_var("LFME_WHITELIST_REFRESH", 60); Arc::new(Config { lastfm_api_key: var("LFME_LASTFM_API_KEY").expect("last.fm API key must be set").into(), + google_api_key: var("LFME_GOOGLE_API_KEY").map(Into::into).ok(), + port: var("LFME_PORT").map(|p| p.parse().expect("cannot parse as a port number")).unwrap_or(9999), - default_theme: var("LFME_THEME_DEFAULT").map(Into::into).unwrap_or_else(|_| "plain".into()), send_refresh_header: !var("LFME_NO_REFRESH").map(|h| &h == "1").unwrap_or(false), - handlebars: { - let mut hb = Handlebars::new(); - handlebars_helper!(url_encode: |s: String| urlencoding::encode(&s)); - - hb.register_helper("url-encode", Box::new(url_encode)); - - for (key, fulltext) in INTERNAL_THEMES { - log::info!(target: "lfm::config::theme", "Registering internal theme `{key}`"); - hb.register_template_string(key, fulltext).unwrap(); - } - hb.set_dev_mode(var("LFME_THEME_DEV").map(|h| &h == "1").unwrap_or(false)); - - 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 - }, - - google_api_key: var("LFME_GOOGLE_API_KEY").map(Into::into).ok(), + default_theme: var("LFME_THEME_DEFAULT").map(Into::into).unwrap_or_else(|_| "plain".into()), + theme_dir: var("LFME_THEME_DIR").ok().map(Into::into), + theme_ext: var("LFME_THEME_EXT").unwrap_or_else(|_| ".hbs".into()).into(), + theme_debug: var("LFME_THEME_DEV").map(|h| &h == "1").unwrap_or(false), whitelist: var("LFME_WHITELIST").ok().map(|w| w.split(',').map(|s| s.trim().to_string()).collect()).unwrap_or_default(), whitelist_mode: var("LFME_WHITELIST_MODE").map(|m| m.to_ascii_lowercase()).unwrap_or_else(|_| "open".into()), @@ -70,9 +53,7 @@ impl Config { }) } + pub fn has_google_api_key(&self) -> bool { self.google_api_key.is_some() } pub fn port(&self) -> u16 { self.port } pub fn send_refresh_header(&self) -> bool { self.send_refresh_header } - pub fn has_google_api_key(&self) -> bool { self.google_api_key.is_some() } - pub fn handlebars(&self) -> &Handlebars { &self.handlebars } - pub fn default_theme(&self) -> Arc { self.default_theme.clone() } } diff --git a/src/ctx.rs b/src/ctx.rs index 50e01bb..cfb10e3 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -104,6 +104,8 @@ pub mod model { } } } +pub use model::Root as Ctx; + #[derive(Debug)] pub struct ResponseCtx(pub model::Root, pub StatusCode); diff --git a/src/lib.rs b/src/lib.rs index 07cd1c7..35f862d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ pub mod deserialize; pub mod cache; pub mod config; pub mod ctx; +pub mod theming; pub use config::CONFIG; pub use ctx::ResponseCtx; diff --git a/src/main.rs b/src/main.rs index d71f05c..dd31e58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,17 +48,23 @@ async fn main() { let (ctx, dur) = lfm_embed::cache::user::get_userinfo(&s).await; let ResponseCtx(data, status) = ResponseCtx::create(ctx, q.font, q.rest).await; - let theme = q.theme.filter(|a| CONFIG.handlebars().has_template(a)).unwrap_or_else(|| CONFIG.default_theme()); + let (theme, res) = lfm_embed::theming::render_theme(q.theme.as_deref(), &data); log::debug!(target: "lfm_embed::server::user", "Using theme {theme}"); - warp::reply::with_header( - warp::reply::with_header( - warp::reply::with_status( - warp::reply::html( - CONFIG.handlebars().render(&theme, &data).unwrap() - ), status - ), "Refresh", dur.as_secs() - ), "X-Selected-Theme", theme.as_ref() - ) + match res { + Err(status) => + Box::new(warp::reply::with_status(warp::reply::html("

Internal Server Error.

"), status)) + as Box, + Ok(contents) => + Box::new(warp::reply::with_header( + warp::reply::with_header( + warp::reply::with_status( + warp::reply::html( + contents + ), status + ), "Refresh", dur.as_secs() + ), "X-Selected-Theme", theme + )) as Box + } }); warp::serve(user) diff --git a/src/themes/plain.hbs b/src/themes/plain.hbs deleted file mode 100644 index 975d6c5..0000000 --- a/src/themes/plain.hbs +++ /dev/null @@ -1,39 +0,0 @@ - - - - - {{#if error}}Error!{{else}}@{{user.name}}'s Last.fm Stats{{/if}} - - - - - {{#if error}} -

{{error}}

- {{else}} - -

@{{user.name}}{{#if scrobble.now_playing}} - is scrobbling{{else}}'s last scrobble was - {{/if}} -

- {{scrobble.name}} -

- {{#if scrobble.album}}from {{scrobble.album}}{{/if}} -

- by {{scrobble.artist.name}}. -

- {{/if}} - - diff --git a/src/theming.rs b/src/theming.rs new file mode 100644 index 0000000..62e32b4 --- /dev/null +++ b/src/theming.rs @@ -0,0 +1,22 @@ +mod hbs; + +use reqwest::StatusCode; + +use crate::CONFIG; + +pub fn render_theme(name: Option<&str>, ctx: &crate::ctx::Ctx) -> (String, Result) { + let mut theme = ""; + let mut res = None; + + if let Some(name) = name { + theme = name; + res = hbs::render_theme(name, ctx) + } + + res = res.or_else(|| { log::debug!("Falling back to default theme!"); theme = &CONFIG.default_theme; None }) + .or_else(|| hbs::render_theme(theme, ctx)); + + let res = res.unwrap_or_else(|| { log::error!("Couldn't load requested theme or default theme `{}`!", CONFIG.default_theme); Err(StatusCode::INTERNAL_SERVER_ERROR)}); + + (theme.into(), res) +} diff --git a/src/theming/hbs.rs b/src/theming/hbs.rs new file mode 100644 index 0000000..73c72ae --- /dev/null +++ b/src/theming/hbs.rs @@ -0,0 +1,35 @@ +use std::sync::LazyLock; + +use handlebars::*; +use reqwest::StatusCode; + +use crate::CONFIG; + +static INTERNAL_THEMES: &[(&str, &str)] = &[("plain", include_str!("hbs/plain.hbs"))]; + +static HANDLEBARS: LazyLock = LazyLock::new(|| { + let mut hb = Handlebars::new(); + handlebars_helper!(url_encode: |s: String| urlencoding::encode(&s)); + + hb.register_helper("url-encode", Box::new(url_encode)); + + for (key, fulltext) in INTERNAL_THEMES { + log::info!("Registering internal theme `{key}`"); + hb.register_template_string(key, fulltext).unwrap(); + } + hb.set_dev_mode(CONFIG.theme_debug); + + if let Some(themes_dir) = CONFIG.theme_dir.as_ref() { + log::info!("Registering theme dir `{themes_dir}`"); + hb.register_templates_directory(&CONFIG.theme_ext, themes_dir.as_ref()).unwrap(); + } + + hb +}); + +pub fn render_theme(name: &str, ctx: &crate::ctx::Ctx) -> Option> { + let templ = HANDLEBARS.get_template(name)?; + let ctx = Context::wraps(ctx).unwrap(); + let render = templ.renders(&HANDLEBARS, &ctx, &mut RenderContext::new(Some(&name.into()))).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR); + Some(render) +} diff --git a/src/theming/hbs/plain.hbs b/src/theming/hbs/plain.hbs new file mode 100644 index 0000000..975d6c5 --- /dev/null +++ b/src/theming/hbs/plain.hbs @@ -0,0 +1,39 @@ + + + + + {{#if error}}Error!{{else}}@{{user.name}}'s Last.fm Stats{{/if}} + + + + + {{#if error}} +

{{error}}

+ {{else}} + +

@{{user.name}}{{#if scrobble.now_playing}} + is scrobbling{{else}}'s last scrobble was + {{/if}} +

+ {{scrobble.name}} +

+ {{#if scrobble.album}}from {{scrobble.album}}{{/if}} +

+ by {{scrobble.artist.name}}. +

+ {{/if}} + + -- cgit v1.2.3-54-g00ecf