feat: Make feeds path customizable

This commit is contained in:
Semubico 2025-07-03 16:14:15 +03:00
parent 8bc7556cf8
commit b178103560
2 changed files with 7 additions and 5 deletions

View file

@ -1,6 +1,7 @@
bind = "127.0.0.1:8085"
item_tpl = "./assets/item.html"
page_tpl = "./assets/index.html"
feeds_path = "./feeds/"
[feeds.tc]
url = "https://www.youtube.com/feeds/videos.xml?channel_id=UCy0tKL1T7wFoYcxCe0xjN6Q"

View file

@ -3,12 +3,13 @@ use reqwest;
#[derive(Debug, Clone, serde::Deserialize)]
struct FeedConfig {
interval_secs: Option<u64>,
url: String
url: String,
}
#[derive(Debug, Clone, serde::Deserialize)]
struct AppConfig {
bind: String,
feeds_path: String,
item_tpl: String,
page_tpl: String,
feeds: std::collections::HashMap<String, FeedConfig>
@ -23,7 +24,7 @@ struct AppState<'a> {
fn read_config() -> anyhow::Result<AppConfig> {
let config_path = std::env::var("RSS_CONFIG").unwrap_or("./config.toml".into());
let cfg = std::fs::read_to_string(&config_path)?;
let mut cfg: AppConfig = toml::from_str(&cfg)?;
let cfg: AppConfig = toml::from_str(&cfg)?;
Ok(cfg)
}
@ -51,9 +52,9 @@ async fn file_needs_update(
Ok(time.elapsed()?.as_secs() >= expiration_timeout.as_secs())
}
async fn get_rss(name: &str, cfg: &FeedConfig) -> anyhow::Result<feed_rs::model::Feed> {
async fn get_rss(name: &str, cfg: &FeedConfig, feed_folder: &str) -> anyhow::Result<feed_rs::model::Feed> {
let filename = std::path::PathBuf::from(format!("./feeds/{}.last_feed", name));
let filename = std::path::PathBuf::from(format!("./{}/{}.last_feed", &feed_folder, name));
let data;
if file_needs_update(&filename, std::time::Duration::from_secs(cfg.interval_secs.unwrap_or(0))).await.unwrap_or(true) {
data = reqwest::get(cfg.url.clone()).await?.text().await?;
@ -71,7 +72,7 @@ async fn render_rss_feeds(axum::extract::State(state): axum::extract::State<std:
let mut res = Vec::new();
for (name, cfg) in state.config.feeds.iter() {
let feed = match get_rss(&name, cfg).await {
let feed = match get_rss(&name, cfg, &state.config.feeds_path).await {
Err(e) => {
res.push(format!("Error fetching feed: {}", &name));
log::error!("Error fetching feed: {}", e);