From fd646281b7eb2696d5ab0a40806c0ed82e7c984a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Thu, 4 Sep 2025 08:02:02 +0200 Subject: [PATCH] refactor: use reqwest client --- src/main.rs | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index bb6c954..a3b9691 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,29 +1,32 @@ use anyhow::Result; use llm_readability::extractor; -use reqwest::Url; +use reqwest::{Client, Url}; use rss::Channel; use tokio::task::JoinSet; use warp::Filter; -async fn get_feed(url: String) -> Result { +async fn get_feed(url: String, client: &Client) -> Result { let url = urlencoding::decode(&url)?.into_owned(); - let content = reqwest::get(url).await?.bytes().await?; + let content = client.get(url).send().await?.bytes().await?; let channel = Channel::read_from(&content[..])?; Ok(channel) } -async fn complete(channel: Channel) -> Result> { +async fn complete(channel: Channel, client: &Client) -> Result> { let items: Vec = channel.items().into_iter().cloned().collect(); let mut set = JoinSet::new(); for mut item in items { - set.spawn(async move { + set.spawn({ + let client = client.clone(); + async move { if let Some(link) = item.link.clone() { - if let Ok(content) = get_content(link).await { + if let Ok(content) = get_content(link, &client.clone()).await { item.set_description(content); }; } item + } }); } @@ -34,8 +37,8 @@ async fn complete(channel: Channel) -> Result> { Ok(Box::new(new_channel)) } -async fn get_content(link: String) -> Result { - let response = reqwest::get(&link).await?; +async fn get_content(link: String, client: &Client) -> Result { + let response = client.get(&link).send().await?; let content = extractor::extract( &mut response.bytes().await?.as_ref(), &Url::parse(link.as_str())?, @@ -56,12 +59,16 @@ pub(crate) fn custom_reject(error: impl Into) -> warp::Rejection #[tokio::main] async fn main() { + let client = Client::new(); let path = warp::path!(String) - .and_then(|url| async move { - let feed = get_feed(url).await.map_err(custom_reject)?; - let updated = complete(feed).await.map_err(custom_reject)?; - Ok::(format!("{}", updated)) + .and_then(move |url| { + let client = client.clone(); + async move { + let feed = get_feed(url, &client).await.map_err(custom_reject)?; + let updated = complete(feed, &client).await.map_err(custom_reject)?; + Ok::(format!("{}", updated)) + } }) - .map(|reply| warp::reply::with_header(reply, "Content-Type", "application/rss+xml")); + .map(|reply| warp::reply::with_header(reply, "Content-Type", "application/rss+xml")); warp::serve(path).run(([127, 0, 0, 1], 3030)).await; }