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/font.rs | 51 --------------------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 src/font.rs (limited to 'src/font.rs') diff --git a/src/font.rs b/src/font.rs deleted file mode 100644 index 6454770..0000000 --- a/src/font.rs +++ /dev/null @@ -1,51 +0,0 @@ -use std::sync::Arc; -use std::time::Duration; - -use tokio::sync::RwLock; -use reqwest::StatusCode; - -use super::cache::{CacheFuture, CacheGetter, Cache, AsyncCache}; -use crate::STATE; - -#[derive(serde::Deserialize, Debug, Default)] -#[serde(default)] -#[serde(rename = "kebab-case")] -pub struct FontQuery { - pub font: Option>, - pub include_font: Option>, - pub google_font: Option>, -// pub small_font: Option<()> -} - -pub type FontFuture = CacheFuture>; -pub type FontGetter = CacheGetter>; -pub type FontCache = Cache>; - -fn font_getter(fontname: &String) -> FontFuture { - let fontname = urlencoding::encode(fontname.as_ref()).to_string(); - Box::pin(async move { - let Some(google_api_key) = STATE.google_api_key.clone() - else { - unreachable!(); - }; - - let fontreq = STATE.http.get(format!("https://www.googleapis.com/webfonts/v1/webfonts?key={}&family={fontname}", google_api_key)) - .send().await - .map_err(|e| {log::error!("Failed to get info for font `{fontname}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't connect to Google Fonts!")})?; - if fontreq.status() == StatusCode::NOT_FOUND { return Err((StatusCode::NOT_FOUND, "Font does not exist!")); } - if fontreq.status() == StatusCode::FORBIDDEN { - log::error!("Invalid Google API key in config!"); - return Err((StatusCode::SERVICE_UNAVAILABLE, "This instance is not configured to support Google Fonts properly, please use a different font.")); - } - - let cssreq = STATE.http.get(format!("https://fonts.googleapis.com/css2?family={fontname}")) - .send().await - .map_err(|e| {log::error!("Failed to get CSS for font `{fontname}`: {e}"); (StatusCode::SERVICE_UNAVAILABLE, "Couldn't download font CSS!")})?; - - Ok(cssreq.text().await.unwrap().into()) - }) -} - -pub fn font_cache() -> FontCache { - Arc::new(RwLock::new(AsyncCache::new(Duration::from_secs(86400), font_getter as FontGetter))) -} -- cgit v1.2.3-54-g00ecf