From 83ba4fe37a1184b998be09b9cbe53a22c7ba9e3b Mon Sep 17 00:00:00 2001 From: alyx Date: Mon, 1 Apr 2024 20:27:44 -0400 Subject: Move caching to src/cache/; Finalize API parsing fixes Font and user cache code has been moved to special files, independant from src/config.rs API parsing changes have been properly tested, and last.fm API JSON is now trace-logged for debugging convenience. --- src/config.rs | 124 +++++++--------------------------------------------------- 1 file changed, 14 insertions(+), 110 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 3c11bc8..69328b0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,103 +7,46 @@ use std::time::*; use super::cache::AsyncCache; use super::deserialize::{GetRecentTracks, GetUserInfo, GetTrackInfo, Track, TrackStub, User}; -use super::font::{font_cache, FontCache}; -use reqwest::{Client, StatusCode}; use dotenv::var; use tokio::sync::RwLock; use handlebars::{Handlebars, handlebars_helper}; use duration_str as ds; -type CacheFuture = Pin> + Send + Sync)>>; -type CacheGetter = fn(&String) -> CacheFuture; -type Cache = Arc>>>; - -type UserFuture = CacheFuture>; -type UserGetter = CacheGetter>; -type UserCache = Cache>; - static INTERNAL_THEMES: &[(&str, &str)] = &[("plain", include_str!("themes/plain.hbs"))]; -pub static STATE: LazyLock> = LazyLock::new(|| { - State::new() +pub static CONFIG: LazyLock> = LazyLock::new(|| { + Config::new() }); -fn user_getter(username: &String) -> UserFuture { - 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 trackstub = 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().next().ok_or((StatusCode::UNPROCESSABLE_ENTITY, "You need to listen to some songs first!"))?; - - let trackreq = STATE.http.get(format!("https://ws.audioscrobbler.com/2.0/?method=track.getInfo&format=json&username={username}&api_key={}&track={}&artist={}", STATE.lastfm_api_key, trackstub.name, trackstub.artist.name)) - .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 trackreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "Track does not exist!")); } - - let trackinfo = trackreq.json::().await - .map_err(|e| {log::error!("Couldn't parse track.getInfo for `{}` by `{}` on behalf of {username}: {e}", trackstub.name, trackstub.artist.name); (StatusCode::INTERNAL_SERVER_ERROR, "Couldn't parse track.getInfo!")})?.track; - - Ok(Arc::new((userinfo, trackinfo, trackstub))) - }) -} - #[derive(Debug)] -enum Whitelist { - Exclusive{cache: UserCache, whitelist: BTreeSet}, - Open{default_cache: UserCache, whitelist_cache: UserCache, whitelist: BTreeSet} -} -#[derive(Debug)] -pub struct State { - lastfm_api_key: Arc, +pub struct Config { + pub(crate) lastfm_api_key: Arc, port: u16, default_theme: Arc, send_refresh_header: bool, - pub(crate) http: Client, - handlebars: Handlebars<'static>, - pub(crate) google_api_key: Option>, - google_fonts_cache: FontCache, + pub(crate )google_api_key: Option>, - whitelist: Whitelist, - default_refresh: Duration, - whitelist_refresh: Duration, + pub(crate) whitelist: BTreeSet, + pub(crate) whitelist_mode: String, + pub(crate) default_refresh: Duration, + pub(crate) whitelist_refresh: Duration, } -impl State { +impl Config { 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 { + Arc::new(Config { 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)); @@ -125,27 +68,9 @@ impl State { }, google_api_key: var("LFME_GOOGLE_API_KEY").map(Into::into).ok(), - google_fonts_cache: font_cache(), - - 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}`"); - } - } - }, + + 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()), default_refresh: default_refresh + Duration::from_secs(1), whitelist_refresh: whitelist_refresh + Duration::from_secs(1) }) @@ -153,28 +78,7 @@ impl State { 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() } } -- cgit v1.2.3-54-g00ecf