From 31a0bdad439a4575c1686f690e9e72bd44dde472 Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 26 Aug 2024 15:22:29 +0300 Subject: Add HTTP fetcher cache It just does its thing in the background, potentially speeding up things. Maybe I could also use the underlying in-memory cache implementation (Moka) to speed up my database. I heard crates.io got some good results from that. --- src/indieauth/mod.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/indieauth/mod.rs') diff --git a/src/indieauth/mod.rs b/src/indieauth/mod.rs index 322a0e2..b3db77f 100644 --- a/src/indieauth/mod.rs +++ b/src/indieauth/mod.rs @@ -131,7 +131,7 @@ async fn authorization_endpoint_get( Host(host): Host, Query(request): Query, State(db): State, - State(http): State, + State(http): State, State(auth): State ) -> Response { let me: url::Url = format!("https://{host}/").parse().unwrap(); @@ -148,7 +148,10 @@ async fn authorization_endpoint_get( tracing::debug!("Sending request to {} to fetch metadata", request.client_id); let metadata_request = http.get(request.client_id.clone()) .header("Accept", "application/json, text/html"); - match metadata_request.send().await.and_then(|res| res.error_for_status()) { + match metadata_request.send().await + .and_then(|res| res.error_for_status() + .map_err(reqwest_middleware::Error::Reqwest)) + { Ok(response) if response.headers().typed_get::().to_owned().map(mime::Mime::from).map(|m| m.type_() == "text" && m.subtype() == "html").unwrap_or(false) => { let url = response.url().clone(); let text = response.text().await.unwrap(); @@ -847,7 +850,7 @@ pub fn router() -> axum::Router where S: Storage + FromRef + 'static, A: AuthBackend + FromRef, - reqwest::Client: FromRef, + reqwest_middleware::ClientWithMiddleware: FromRef, St: Clone + Send + Sync + 'static { use axum::routing::{Router, get, post}; -- cgit 1.4.1