From d2c27e17897f80929d2ba5fed1055eade27a6b08 Mon Sep 17 00:00:00 2001 From: alyx Date: Fri, 12 Jan 2024 12:17:57 -0500 Subject: Cleanup --- src/cache.rs | 148 +++++++++-------------------- src/config.rs | 248 ++++++++++++++++++++++++------------------------ src/ctx.rs | 274 ++++++++++++++++++++++++++--------------------------- src/deserialize.rs | 226 +++++++++++++++++++++---------------------- src/main.rs | 86 ++++++++--------- 5 files changed, 459 insertions(+), 523 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index 631f900..dbb3759 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -2,125 +2,67 @@ use std::{future::Future, time::*, collections::HashMap, hash::Hash}; use reqwest::StatusCode; #[derive(Debug)] pub struct AsyncCache { - func: F, - cache: HashMap, - interval: Duration + func: F, + cache: HashMap, + interval: Duration } impl AsyncCache where - for<'a> F: FnMut(&'a K) -> Fut + 'a, - K: Hash + PartialEq + Eq + Clone, - Fut: Future> + Send + Sync + for<'a> F: FnMut(&'a K) -> Fut + 'a, + K: Hash + PartialEq + Eq + Clone, + Fut: Future> + Send + Sync { - pub fn new(interval: Duration, func: F) -> Self { - Self{ - cache: HashMap::new(), - interval, func - } - } - - pub async fn get(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> { - if self.is_stale(&key) { - log::trace!(target: "lfm::cache", "MISS : interval = {:?}", self.interval); - self.renew(&key).await - } else { - log::trace!(target: "lfm::cache", "HIT : interval = {:?}", self.interval); - Ok(&self.cache.get(&key).unwrap().1) - } - } - - pub async fn renew(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> { - let val = (self.func)(&key).await?; - self.cache.insert(key.clone(), (Instant::now(), val)); - Ok(&self.cache.get(key).unwrap().1) + pub fn new(interval: Duration, func: F) -> Self { + Self{ + cache: HashMap::new(), + interval, func } + } - pub fn is_stale(&self, key: &K) -> bool { - if let Some((last_update, _)) = self.cache.get(key) { - let now = Instant::now(); - log::trace!(target: "lfm::cache", "Key exists, last update {:?} ago.", now - *last_update); - now > (*last_update + self.interval) - } - else { true } - } - - pub async fn get_opt(&self, key: &K) -> Option<&V> { - if self.is_stale(key) { - self.cache.get(key).map(|(_, v)| v) - } - else { None } + pub async fn get(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> { + if self.is_stale(&key) { + log::trace!(target: "lfm::cache", "MISS : interval = {:?}", self.interval); + self.renew(&key).await + } else { + log::trace!(target: "lfm::cache", "HIT : interval = {:?}", self.interval); + Ok(&self.cache.get(&key).unwrap().1) } + } - pub fn interval(&self) -> Duration { self.interval } -} + pub async fn renew(&mut self, key: &K) -> Result<&V, (StatusCode, &'static str)> { + let val = (self.func)(&key).await?; + self.cache.insert(key.clone(), (Instant::now(), val)); + Ok(&self.cache.get(key).unwrap().1) + } -impl AsyncCache -where - for<'a> F: FnMut(&'a K) -> Fut + 'a, - K: Hash + PartialEq + Eq + Clone, - V: Clone, - Fut: Future> + Send + Sync -{ - pub async fn get_owned(&mut self, key: &K) -> Result { - self.get(key).await.cloned() + pub fn is_stale(&self, key: &K) -> bool { + if let Some((last_update, _)) = self.cache.get(key) { + let now = Instant::now(); + log::trace!(target: "lfm::cache", "Key exists, last update {:?} ago.", now - *last_update); + now > (*last_update + self.interval) } -} -/* -pub struct AsyncCache { - func: F, - cache: HashMap, - interval: Duration -} + else { true } + } -impl AsyncCache -where - for<'a> F: FnMut(&'a K) -> Fut + 'a, - Fut: Future -{ - pub fn new(interval: Duration, mut func: F) -> Self { - Self{ - cache: HashMap::new(), - interval, func - } - } - - pub async fn get(&mut self, key: &K) -> &V { - if self.is_stale(key) { - self.renew().await - } else { - self.cache.get(key) - } - } - - pub async fn renew(&mut self, key: &K) -> &V { - self.cache.get_mut(key).0 = now; - self.cache.get_mut(key).1 = (self.func)(key).await; - self.cache.get(key) + pub async fn get_opt(&self, key: &K) -> Option<&V> { + if self.is_stale(key) { + self.cache.get(key).map(|(_, v)| v) } + else { None } + } - pub fn is_stale(&self, key: &K) -> bool { - let now = Instant::now(); - let last_update = self.cache.get(key).0; - now < (last_update + self.interval) - } - - pub fn get_opt(&self, key: &K) -> Option<&T> { - if self.is_stale(key) { - Some(self.cache.get(key)) - } - else { None } - } + pub fn interval(&self) -> Duration { self.interval } } -impl AsyncCache +impl AsyncCache where - F: for<'a> FnMut(&'a K) -> Fut + 'a, - Fut: Future, - V: Clone + for<'a> F: FnMut(&'a K) -> Fut + 'a, + K: Hash + PartialEq + Eq + Clone, + V: Clone, + Fut: Future> + Send + Sync { - pub async fn get_owned(&mut self, key: &K) -> V { - self.get(key).await.clone() - } + pub async fn get_owned(&mut self, key: &K) -> Result { + self.get(key).await.cloned() + } } -*/ diff --git a/src/config.rs b/src/config.rs index 8492f8b..49b515e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::BTreeSet; use std::sync::LazyLock; use std::sync::Arc; use std::future::Future; @@ -17,32 +17,32 @@ use duration_str as ds; static INTERNAL_THEMES: &[(&'static str, &'static str)] = &[("plain", include_str!("themes/plain.hbs"))]; pub static STATE: LazyLock> = LazyLock::new(|| { - State::new() + State::new() }); fn user_getter(username: &String) -> Pin, (StatusCode, &'static str)>> + Send + Sync)>> { - 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.lastfm_api_key)) - .send().await - .map_err(|e| {log::error!("Failed to get info for user `{username}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't connect to last.fm!")})?; - if userreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "User does not exist!")); } - - let userinfo = userreq.json::().await - .map_err(|e| {log::error!("Couldn't parse user.getInfo for `{username}`: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse user.getInfo!")})?.user; - - let tracksreq = STATE.http.get(format!("https://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&format=json&extended=1&limit=1&user={username}&api_key={}", STATE.lastfm_api_key)) - .send().await - .map_err(|e| {log::error!("Failed to get tracks for user `{username}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't connect to last.fm!")})?; - if tracksreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "User does not exist!")); } - if tracksreq.status() == StatusCode::FORBIDDEN { return Err((StatusCode::FORBIDDEN, "You need to unprivate your song history!")); } - - let tracksinfo = tracksreq.json::().await - .map_err(|e| {log::error!("Couldn't parse user.getRecentTracks for `{username}`: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse user.getRecentTracks!")})? - .recenttracks.track.into_iter().nth(0).ok_or((StatusCode::UNPROCESSABLE_ENTITY, "You need to listen to some songs first!"))?; - - Ok(Arc::new((userinfo, tracksinfo))) - }) + 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.lastfm_api_key)) + .send().await + .map_err(|e| {log::error!("Failed to get info for user `{username}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't connect to last.fm!")})?; + if userreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "User does not exist!")); } + + let userinfo = userreq.json::().await + .map_err(|e| {log::error!("Couldn't parse user.getInfo for `{username}`: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse user.getInfo!")})?.user; + + let tracksreq = STATE.http.get(format!("https://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&format=json&extended=1&limit=1&user={username}&api_key={}", STATE.lastfm_api_key)) + .send().await + .map_err(|e| {log::error!("Failed to get tracks for user `{username}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't connect to last.fm!")})?; + if tracksreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "User does not exist!")); } + if tracksreq.status() == StatusCode::FORBIDDEN { return Err((StatusCode::FORBIDDEN, "You need to unprivate your song history!")); } + + let tracksinfo = tracksreq.json::().await + .map_err(|e| {log::error!("Couldn't parse user.getRecentTracks for `{username}`: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse user.getRecentTracks!")})? + .recenttracks.track.into_iter().nth(0).ok_or((StatusCode::UNPROCESSABLE_ENTITY, "You need to listen to some songs first!"))?; + + Ok(Arc::new((userinfo, tracksinfo))) + }) } fn font_getter(fontname: &String) -> Pin, (StatusCode, &'static str)>> + Send + Sync)>> { @@ -76,117 +76,117 @@ type FontGetter = fn(&String) -> Pin, ( type FontCache = Arc, FontGetter>>>; #[derive(Debug)] enum Whitelist { - Exclusive{cache: UserCache, whitelist: HashSet}, - Open{default_cache: UserCache, whitelist_cache: UserCache, whitelist: HashSet} + Exclusive{cache: UserCache, whitelist: BTreeSet}, + Open{default_cache: UserCache, whitelist_cache: UserCache, whitelist: BTreeSet} } #[derive(Debug)] pub struct State { - lastfm_api_key: Arc, - google_api_key: Option>, - whitelist: Whitelist, - port: u16, + lastfm_api_key: Arc, + port: u16, + default_theme: Arc, + send_refresh_header: bool, - handlebars: Handlebars<'static>, - default_theme: Arc, - send_refresh_header: bool, - http: Client, + http: Client, - google_fonts_cache: FontCache, + handlebars: Handlebars<'static>, - default_refresh: Duration, - whitelist_refresh: Duration, + google_api_key: Option>, + google_fonts_cache: FontCache, + + whitelist: Whitelist, + default_refresh: Duration, + whitelist_refresh: Duration, } impl State { - fn new() -> Arc { - let duration_from_var = |v: &str, d: u64| -> Duration {var(v).map(|r| ds::parse(&r).expect("bad duration string")).unwrap_or_else(|_| Duration::from_secs(d))}; - let user_cache_from_duration = |d: Duration| -> UserCache { - Arc::new(RwLock::new(AsyncCache::new(d, user_getter as UserGetter))) + fn new() -> Arc { + let duration_from_var = |v: &str, d: u64| -> Duration {var(v).map(|r| ds::parse(&r).expect("bad duration string")).unwrap_or_else(|_| Duration::from_secs(d))}; + let user_cache_from_duration = |d: Duration| -> UserCache { + Arc::new(RwLock::new(AsyncCache::new(d, user_getter as UserGetter))) + }; + let default_refresh = duration_from_var("LFME_DEFAULT_REFRESH", 300); + let whitelist_refresh = duration_from_var("LFME_WHITELIST_REFRESH", 60); + let default_cache = user_cache_from_duration(default_refresh); + let whitelist_cache = user_cache_from_duration(whitelist_refresh); + Arc::new(State { + lastfm_api_key: var("LFME_LASTFM_API_KEY").expect("last.fm API key must be set").into(), + 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), + + http: Client::builder().https_only(true).user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"))).build().unwrap(), + + 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(), + google_fonts_cache: Arc::new(RwLock::new(AsyncCache::new(Duration::from_secs(86400), font_getter as FontGetter))), + + whitelist: { + let load_whitelist = || -> Option> { + var("LFME_WHITELIST").ok().map( + |w| w.split(",").map(|s| s.trim().to_string()).collect() + ) }; - let default_refresh = duration_from_var("LFME_DEFAULT_REFRESH", 300); - let whitelist_refresh = duration_from_var("LFME_WHITELIST_REFRESH", 60); - let default_cache = user_cache_from_duration(default_refresh); - let whitelist_cache = user_cache_from_duration(whitelist_refresh); - - Arc::new(State { - lastfm_api_key: var("LFME_LASTFM_API_KEY").expect("last.fm API key must be set").into(), - port: var("LFME_PORT").map(|p| p.parse().expect("cannot parse as a port number")).unwrap_or(9999), - send_refresh_header: !var("LFME_NO_REFRESH").map(|h| &h == "1").unwrap_or(false), - http: Client::builder().https_only(true).user_agent(concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"))).build().unwrap(), - - 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 - }, - default_theme: var("LFME_THEME_DEFAULT").map(Into::into).unwrap_or_else(|_| "plain".into()), - - google_api_key: var("LFME_GOOGLE_API_KEY").map(Into::into).ok(), - google_fonts_cache: Arc::new(RwLock::new(AsyncCache::new(Duration::from_secs(86400), font_getter as FontGetter))), - - whitelist: { - let load_whitelist = || -> Option> { - var("LFME_WHITELIST").ok().map( - |w| w.split(",").map(|s| s.trim().to_string()).collect() - ) - }; - - match var("LFME_WHITELIST_MODE").map(|m| m.to_ascii_lowercase()).unwrap_or_else(|_| "open".into()).as_str() { - "open" => { - Whitelist::Open{default_cache, whitelist_cache, whitelist: load_whitelist().unwrap_or_default()} - }, - "exclusive" => { - Whitelist::Exclusive{cache: whitelist_cache, whitelist: load_whitelist().expect("LFME_WHITELIST not set, unable to serve anyone")} - }, - m => { - panic!("Bad whitelist mode: `{m}`"); - } - } - }, - default_refresh: default_refresh + Duration::from_secs(1), - whitelist_refresh: whitelist_refresh + Duration::from_secs(1) - }) - } - pub fn port(&self) -> u16 { self.port } - pub fn send_refresh_header(&self) -> bool { self.send_refresh_header } - pub async fn get_fontinfo(&self, font: &String) -> Result, (StatusCode, &'static str)> { - self.google_fonts_cache.write().await.get_owned(font).await - } - pub fn has_google_api_key(&self) -> bool { self.google_api_key.is_some() } - pub async fn get_userinfo(&self, user: &String) -> (Result, (StatusCode, &'static str)>, Duration) { - match &self.whitelist { - Whitelist::Open{default_cache, whitelist_cache, whitelist} => { - if whitelist.contains(user) { - (whitelist_cache.write().await.get_owned(user).await, self.whitelist_refresh) - } - else { - (default_cache.write().await.get_owned(user).await, self.default_refresh) - } - }, - Whitelist::Exclusive{cache, whitelist} => { - if whitelist.contains(user) { - (cache.write().await.get_owned(user).await, self.whitelist_refresh) - } - else { (Err((StatusCode::FORBIDDEN, "User not in whitelist!")), self.default_refresh) } - } + match var("LFME_WHITELIST_MODE").map(|m| m.to_ascii_lowercase()).unwrap_or_else(|_| "open".into()).as_str() { + "open" => { + Whitelist::Open{default_cache, whitelist_cache, whitelist: load_whitelist().unwrap_or_default()} + }, + "exclusive" => { + Whitelist::Exclusive{cache: whitelist_cache, whitelist: load_whitelist().expect("LFME_WHITELIST not set, unable to serve anyone")} + }, + m => { + panic!("Bad whitelist mode: `{m}`"); + } + } + }, + default_refresh: default_refresh + Duration::from_secs(1), + whitelist_refresh: whitelist_refresh + Duration::from_secs(1) + }) + } + + pub fn port(&self) -> u16 { self.port } + pub fn send_refresh_header(&self) -> bool { self.send_refresh_header } + pub async fn get_fontinfo(&self, font: &String) -> Result, (StatusCode, &'static str)> { + self.google_fonts_cache.write().await.get_owned(font).await + } + pub fn has_google_api_key(&self) -> bool { self.google_api_key.is_some() } + pub async fn get_userinfo(&self, user: &String) -> (Result, (StatusCode, &'static str)>, Duration) { + match &self.whitelist { + Whitelist::Open{default_cache, whitelist_cache, whitelist} => { + if whitelist.contains(user) { + (whitelist_cache.write().await.get_owned(user).await, self.whitelist_refresh) + } + else { + (default_cache.write().await.get_owned(user).await, self.default_refresh) + } + }, + Whitelist::Exclusive{cache, whitelist} => { + if whitelist.contains(user) { + (cache.write().await.get_owned(user).await, self.whitelist_refresh) } + else { (Err((StatusCode::FORBIDDEN, "User not in whitelist!")), self.default_refresh) } + } } - pub fn handlebars(&self) -> &Handlebars { &self.handlebars } - pub fn default_theme(&self) -> Arc { self.default_theme.clone() } + } + 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 df910bd..681230e 100644 --- a/src/ctx.rs +++ b/src/ctx.rs @@ -6,151 +6,151 @@ use super::font::FontQuery; use super::config::STATE; pub mod model { - use std::sync::Arc; - use std::collections::BTreeMap; - - /// The theme representation of a user. - #[derive(serde::Serialize, Debug)] - pub struct User { - /// Their username. - pub name: Arc, - /// Their "Display Name". - pub realname: Arc, - - /// True if user subscribes to last.fm pro. - pub pro_subscriber: bool, - /// Total scrobbles. - pub scrobble_count: u64, - /// Number of artists in library. - pub artist_count: u64, - /// Number of tracks in library. - pub track_count: u64, - /// Number of albums in library. - pub album_count: u64, - - /// Link to user's profile picture. - pub image_url: Arc, - - /// Link to user's profile. - pub url: Arc - } - - /// The theme representation of an artist - #[derive(serde::Serialize, Debug)] - pub struct Artist { - /// The artist's name. - pub name: Arc, - - /// A link to their current image. - pub image_url: Arc, - /// A link to their last.fm page. - pub url: Arc - } + use std::sync::Arc; + use std::collections::BTreeMap; - /// The theme representation of a user's most recently scrobbled track. - #[derive(serde::Serialize, Debug)] - pub struct Scrobble { - /// The name of the track. - pub name: Arc, - /// The name of its album. - pub album: Arc, - /// The artist who made it. - pub artist: Artist, - - /// A link to the track image. - pub image_url: Arc, - /// True if the user is currently scrobbling it, false if it's just the most recently played track. - pub now_playing: bool, - /// A link to the track's last.fm page. - pub url: Arc, - - /// True if the user has loved the track. - pub loved: bool - } + /// The theme representation of a user. + #[derive(serde::Serialize, Debug)] + pub struct User { + /// Their username. + pub name: Arc, + /// Their "Display Name". + pub realname: Arc, - #[derive(serde::Serialize, Debug)] - #[serde(untagged)] - pub enum Font { - External { css: Arc, name: Arc }, - Name { name: Arc }, - } + /// Total scrobbles. + pub scrobble_count: u64, + /// Number of artists in library. + pub artist_count: u64, + /// Number of tracks in library. + pub track_count: u64, + /// Number of albums in library. + pub album_count: u64, - /// The context passed in to all themes. - /// - /// Serialized as untagged, so themes should check if `error` is set and decide whether to show an error state or try rendering user info. - #[derive(serde::Serialize, Debug)] - #[serde(untagged)] - pub enum Data { - /// Contains text explaining a potential error. - Error { error: &'static str }, - /// Contains data about a user and what they're listening to. - Data { user: User, scrobble: Scrobble, font: Option, query: BTreeMap, } - } + /// Link to user's profile picture. + pub image_url: Arc, + + /// Link to user's profile. + pub url: Arc + } + + /// The theme representation of an artist + #[derive(serde::Serialize, Debug)] + pub struct Artist { + /// The artist's name. + pub name: Arc, + + /// A link to their current image. + pub image_url: Arc, + /// A link to their last.fm page. + pub url: Arc + } + + /// The theme representation of a user's most recently scrobbled track. + #[derive(serde::Serialize, Debug)] + pub struct Scrobble { + /// The name of the track. + pub name: Arc, + /// The name of its album. + pub album: Arc, + /// The artist who made it. + pub artist: Artist, + + /// A link to the track image. + pub image_url: Arc, + /// True if the user is currently scrobbling it, false if it's just the most recently played track. + pub now_playing: bool, + /// A link to the track's last.fm page. + pub url: Arc, + + /// True if the user has loved the track. + pub loved: bool + } + + /// The user-specified font request parameters + #[derive(serde::Serialize, Debug)] + #[serde(untagged)] + pub enum Font { + /// A font that requires additional CSS to load properly. + External { css: Arc, name: Arc }, + /// A font that is w3c standard, or widely installed. + Name { name: Arc }, + } + + /// The context passed in to all themes. + /// + /// Serialized as untagged, so themes should check if `error` is set and decide whether to show an error state or try rendering user info. + #[derive(serde::Serialize, Debug)] + #[serde(untagged)] + pub enum Data { + /// Contains text explaining a potential error. + Error { error: &'static str }, + /// Contains data about a user and what they're listening to. + Data { user: User, scrobble: Scrobble, font: Option, query: BTreeMap, } + } } #[derive(Debug)] pub struct ResponseCtx(pub model::Data, pub StatusCode); impl ResponseCtx { - pub async fn create(api_result: Result, (StatusCode, &'static str)>, font_query: Option, query: BTreeMap) -> ResponseCtx { - match api_result { - Ok(a) => { - let (user, track) = a.as_ref(); - ResponseCtx(model::Data::Data { - user: model::User { - name: user.name.clone(), - realname: user.realname.clone(), - - pro_subscriber: user.subscriber, - scrobble_count: user.playcount, - artist_count: user.artist_count, - track_count: user.track_count, - album_count: user.track_count, - - image_url: user.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).unwrap_or_else(|| "".into()), - - url: user.url.clone() - }, - scrobble: model::Scrobble { - name: track.name.clone(), - album: track.album.name.clone(), - artist: model::Artist { - name: track.artist.name.clone(), - image_url: track.artist.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).unwrap_or_else(|| "".into()), - url: track.artist.url.clone().unwrap_or_else(|| "".into()) - }, - image_url: track.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).unwrap_or_else(|| "".into()), - now_playing: track.attr.nowplaying, - url: track.url.clone(), - loved: track.loved.unwrap_or(false) - }, - font: match font_query { - Some(FontQuery { google_font: Some(f), .. }) if STATE.has_google_api_key() => { - let css = match STATE.get_fontinfo(&f.to_string()).await { - Ok(css) => css, - Err((status, error)) => { return ResponseCtx(model::Data::Error {error}, status); } - }; - Some(model::Font::External { - css, - name: f - }) - }, - Some(FontQuery { include_font: Some(f), .. }) => Some( - model::Font::External { - css: format!( - "@font-face {{ font-family: 'included_font'; src: url('{}'); }}", - f.replace("\\", "\\\\") - .replace("'", "\\'")).into(), - name: "included_font".into() - }), - Some(FontQuery { font: Some(s), .. }) => Some(model::Font::Name { name: s }), - _ => None, - }, - query - }, StatusCode::OK) + pub async fn create(api_result: Result, (StatusCode, &'static str)>, font_query: Option, query: BTreeMap) -> ResponseCtx { + match api_result { + Ok(a) => { + let (user, track) = a.as_ref(); + ResponseCtx(model::Data::Data { + user: model::User { + name: user.name.clone(), + realname: user.realname.clone(), + + scrobble_count: user.playcount, + artist_count: user.artist_count, + track_count: user.track_count, + album_count: user.track_count, + + image_url: user.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).unwrap_or_else(|| "".into()), + + url: user.url.clone() + }, + scrobble: model::Scrobble { + name: track.name.clone(), + album: track.album.name.clone(), + artist: model::Artist { + name: track.artist.name.clone(), + image_url: track.artist.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).unwrap_or_else(|| "".into()), + url: track.artist.url.clone().unwrap_or_else(|| "".into()) + }, + image_url: track.images.iter().max_by(|a, b| a.size.cmp(&b.size)).map(|a| a.url.clone()).unwrap_or_else(|| "".into()), + now_playing: track.attr.nowplaying, + url: track.url.clone(), + loved: track.loved.unwrap_or(false) + }, + font: match font_query { + Some(FontQuery { google_font: Some(f), .. }) if STATE.has_google_api_key() => { + let css = match STATE.get_fontinfo(&f.to_string()).await { + Ok(css) => css, + Err((status, error)) => { return ResponseCtx(model::Data::Error {error}, status); } + }; + Some(model::Font::External { + css, + name: f + }) }, - Err((status, error)) => { - ResponseCtx(model::Data::Error {error}, status) - } - } + Some(FontQuery { include_font: Some(f), .. }) => Some( + model::Font::External { + css: format!( + "@font-face {{ font-family: 'included_font'; src: url('{}'); }}", + f.replace("\\", "\\\\") + .replace("'", "\\'")).into(), + name: "included_font".into() + }), + Some(FontQuery { font: Some(s), .. }) => Some(model::Font::Name { name: s }), + _ => None, + }, + query + }, StatusCode::OK) + }, + Err((status, error)) => { + ResponseCtx(model::Data::Error {error}, status) + } } + } } diff --git a/src/deserialize.rs b/src/deserialize.rs index ad67df7..b31fe35 100644 --- a/src/deserialize.rs +++ b/src/deserialize.rs @@ -5,170 +5,164 @@ use std::collections::HashMap; use std::sync::Arc; fn str_num<'de, D, T>(d: D) -> Result where D: Deserializer<'de>, T: From { - struct Visitor; - impl<'v> de::Visitor<'v> for Visitor { - type Value = u64; - fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "a value which can be interpreted as a uint") - } - fn visit_str(self, v: &str) -> Result - where - E: de::Error - { - v.parse().map_err(|_| de::Error::invalid_value(de::Unexpected::Str(v), &"a string which can be parsed as a uint")) - } - fn visit_u64(self, v: u64) -> Result - where - E: de::Error - { - Ok(v) - } + struct Visitor; + impl<'v> de::Visitor<'v> for Visitor { + type Value = u64; + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "a value which can be interpreted as a uint") } - d.deserialize_any(Visitor).map(Into::into) + fn visit_str(self, v: &str) -> Result + where E: de::Error + { + v.parse().map_err(|_| de::Error::invalid_value(de::Unexpected::Str(v), &"a string which can be parsed as a uint")) + } + fn visit_u64(self, v: u64) -> Result + where E: de::Error + { + Ok(v) + } + } + d.deserialize_any(Visitor).map(Into::into) } fn str_bool<'de, D, T>(d: D) -> Result where D: Deserializer<'de>, T: From{ - struct Visitor; - impl<'v> de::Visitor<'v> for Visitor { - type Value = bool; - fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "a value which can be interpreted as a uint") - } - fn visit_str(self, v: &str) -> Result - where - E: de::Error - { - match v.to_ascii_lowercase().as_str() { - "true" | "1" => Ok(true), - "false" | "0" => Ok(false), - _ => Err(de::Error::invalid_value(de::Unexpected::Str(v), &"a string which can be parsed as a bool")) - } - } - fn visit_bool(self, v: bool) -> Result - where - E: de::Error - { - Ok(v) - } + struct Visitor; + impl<'v> de::Visitor<'v> for Visitor { + type Value = bool; + fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "a value which can be interpreted as a uint") + } + fn visit_str(self, v: &str) -> Result + where E: de::Error + { + match v.to_ascii_lowercase().as_str() { + "true" | "1" => Ok(true), + "false" | "0" => Ok(false), + _ => Err(de::Error::invalid_value(de::Unexpected::Str(v), &"a string which can be parsed as a bool")) + } + } + fn visit_bool(self, v: bool) -> Result + where E: de::Error + { + Ok(v) } - d.deserialize_any(Visitor).map(Into::into) + } + d.deserialize_any(Visitor).map(Into::into) } #[derive(Deserialize, Debug)] pub struct TimeStamp { - #[serde(alias = "unixtime")] - #[serde(alias = "uts")] - #[serde(deserialize_with = "str_num")] - pub unix_timestamp: u64, - #[serde(rename = "#text")] - pub text: Value + #[serde(alias = "unixtime")] + #[serde(alias = "uts")] + #[serde(deserialize_with = "str_num")] + pub unix_timestamp: u64, + #[serde(rename = "#text")] + pub text: Value } #[derive(Deserialize, Debug)] pub struct Artist { - #[serde(rename = "mbid")] - pub uuid: Arc, - #[serde(alias = "#text")] - pub name: Arc, - - #[serde(default)] - #[serde(rename = "image")] - pub images: Vec, - #[serde(default)] - pub url: Option> + #[serde(rename = "mbid")] + pub uuid: Arc, + #[serde(alias = "#text")] + pub name: Arc, + + #[serde(default)] + #[serde(rename = "image")] + pub images: Vec, + #[serde(default)] + pub url: Option> } #[derive(Deserialize, Debug, Ord, PartialOrd, Eq, PartialEq)] #[serde(rename_all = "lowercase")] #[repr(u8)] pub enum ImageSize { - Small, - Medium, - Large, - ExtraLarge + Small, + Medium, + Large, + ExtraLarge } #[derive(Deserialize, Debug)] pub struct Image { - pub size: ImageSize, - #[serde(rename = "#text")] - pub url: Arc, + pub size: ImageSize, + #[serde(rename = "#text")] + pub url: Arc, } #[derive(Deserialize, Debug)] pub struct Album { - #[serde(rename = "mbid")] - pub uuid: Arc, - #[serde(rename = "#text")] - pub name: Arc, + #[serde(rename = "mbid")] + pub uuid: Arc, + #[serde(rename = "#text")] + pub name: Arc, } #[derive(Default, Deserialize, Debug)] pub struct TrackAttr { - #[serde(default)] - #[serde(deserialize_with = "str_bool")] - pub nowplaying: bool, - #[serde(flatten)] - pub rest: HashMap, Value>, + #[serde(default)] + #[serde(deserialize_with = "str_bool")] + pub nowplaying: bool, + #[serde(flatten)] + pub rest: HashMap, Value>, } #[derive(Deserialize, Debug)] pub struct Track { - pub artist: Artist, - #[serde(deserialize_with = "str_bool")] - pub streamable: bool, - #[serde(rename = "image")] - pub images: Vec, - #[serde(rename = "mbid")] - pub uuid: Arc, - pub album: Album, - pub name: Arc, - #[serde(rename = "@attr")] - #[serde(default)] - pub attr: TrackAttr, - pub url: Arc, - - #[serde(default)] - #[serde(deserialize_with = "str_bool")] - pub loved: Option, - #[serde(default)] - pub date: Option + pub artist: Artist, + #[serde(deserialize_with = "str_bool")] + pub streamable: bool, + #[serde(rename = "image")] + pub images: Vec, + #[serde(rename = "mbid")] + pub uuid: Arc, + pub album: Album, + pub name: Arc, + #[serde(rename = "@attr")] + #[serde(default)] + pub attr: TrackAttr, + pub url: Arc, + + #[serde(default)] + #[serde(deserialize_with = "str_bool")] + pub loved: Option, + #[serde(default)] + pub date: Option } #[derive(Deserialize, Debug)] pub struct RecentTracks { - pub track: Vec + pub track: Vec } #[derive(Deserialize, Debug)] pub struct GetRecentTracks { - pub recenttracks: RecentTracks + pub recenttracks: RecentTracks } #[derive(Deserialize, Debug)] pub struct User { - pub name: Arc, - #[serde(deserialize_with = "str_bool")] - pub subscriber: bool, - pub realname: Arc, - #[serde(deserialize_with = "str_num")] - pub playcount: u64, - #[serde(deserialize_with = "str_num")] - pub artist_count: u64, - #[serde(deserialize_with = "str_num")] - pub playlists: u64, - #[serde(deserialize_with = "str_num")] - pub track_count: u64, - #[serde(deserialize_with = "str_num")] - pub album_count: u64, - - #[serde(rename = "image")] - pub images: Vec, - - pub registered: TimeStamp, - pub url: Arc + pub name: Arc, + pub realname: Arc, + #[serde(deserialize_with = "str_num")] + pub playcount: u64, + #[serde(deserialize_with = "str_num")] + pub artist_count: u64, + #[serde(deserialize_with = "str_num")] + pub playlists: u64, + #[serde(deserialize_with = "str_num")] + pub track_count: u64, + #[serde(deserialize_with = "str_num")] + pub album_count: u64, + + #[serde(rename = "image")] + pub images: Vec, + + pub registered: TimeStamp, + pub url: Arc } #[derive(Deserialize, Debug)] pub struct GetUserInfo { - pub user: User + pub user: User } diff --git a/src/main.rs b/src/main.rs index 648603b..34dafda 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,54 +12,54 @@ use warp::Filter; #[derive(serde::Deserialize, Debug)] #[serde(rename = "kebab-case")] struct UserQuery { - #[serde(default)] - theme: Option>, - #[serde(flatten)] - #[serde(default)] - font: Option, - #[serde(flatten)] - rest: BTreeMap + #[serde(default)] + theme: Option>, + #[serde(flatten)] + #[serde(default)] + font: Option, + #[serde(flatten)] + rest: BTreeMap } #[tokio::main] async fn main() { - env_logger::Builder::new() - .filter_level(LevelFilter::Warn) - .parse_filters(&var("LFME_LOG_LEVEL").unwrap_or_default()) - .target( - var("LFME_LOG_FILE").ok() - .map( - |f| env_logger::Target::Pipe( - Box::new(File::options() - .append(true) - .open(f) - .expect("couldn't open LFME_LOG_FILE"))) - ) - .unwrap_or(env_logger::Target::Stderr) - ).init(); + env_logger::Builder::new() + .filter_level(LevelFilter::Warn) + .parse_filters(&var("LFME_LOG_LEVEL").unwrap_or_default()) + .target( + var("LFME_LOG_FILE").ok() + .map( + |f| env_logger::Target::Pipe( + Box::new(File::options() + .append(true) + .open(f) + .expect("couldn't open LFME_LOG_FILE"))) + ) + .unwrap_or(env_logger::Target::Stderr) + ).init(); - std::sync::LazyLock::force(&STATE); + std::sync::LazyLock::force(&STATE); - let user = warp::path!("user" / String) - .and(warp::query::()) - .then(|s, q: UserQuery| async move { - log::debug!(target: "lfm::server::user", "Handling request for user `{s}` with {q:?}"); - let (ctx, dur) = STATE.get_userinfo(&s).await; - let ResponseCtx(mut data, status) = ResponseCtx::create(ctx, q.font, q.rest).await; - - let theme = q.theme.filter(|a| STATE.handlebars().has_template(&a)).unwrap_or_else(|| STATE.default_theme()); - log::debug!(target: "lfm::server::user", "Using theme {theme}"); - warp::reply::with_header( - warp::reply::with_header( - warp::reply::with_status( - warp::reply::html( - STATE.handlebars().render(&theme, &data).unwrap() - ), status - ), "Refresh", dur.as_secs() - ), "X-Selected-Theme", theme.as_ref() - ) - }); + let user = warp::path!("user" / String) + .and(warp::query::()) + .then(|s, q: UserQuery| async move { + log::debug!(target: "lfm::server::user", "Handling request for user `{s}` with {q:?}"); + let (ctx, dur) = STATE.get_userinfo(&s).await; + let ResponseCtx(data, status) = ResponseCtx::create(ctx, q.font, q.rest).await; - warp::serve(user) - .bind(([127,0,0,1], STATE.port())).await; + let theme = q.theme.filter(|a| STATE.handlebars().has_template(&a)).unwrap_or_else(|| STATE.default_theme()); + log::debug!(target: "lfm::server::user", "Using theme {theme}"); + warp::reply::with_header( + warp::reply::with_header( + warp::reply::with_status( + warp::reply::html( + STATE.handlebars().render(&theme, &data).unwrap() + ), status + ), "Refresh", dur.as_secs() + ), "X-Selected-Theme", theme.as_ref() + ) + }); + + warp::serve(user) + .bind(([127,0,0,1], STATE.port())).await; } -- cgit v1.2.3-54-g00ecf