deploy config

This commit is contained in:
lelgenio 2024-06-22 13:42:24 -03:00
parent da40e48b19
commit d0a7e7ec88
10 changed files with 231 additions and 39 deletions

View file

@ -1,7 +1,8 @@
use askama::Template;
use axum::extract::State;
use time::Date;
use crate::sources;
use crate::{sources, AppState};
#[derive(Template)]
#[template(path = "index.html")]
@ -22,18 +23,53 @@ impl TimeSince {
}
#[axum::debug_handler]
pub async fn get() -> HomeTemplate {
pub async fn get(state: State<AppState>) -> HomeTemplate {
let mut t = vec![];
for source in sources::sources() {
let url = source.url();
let Ok(res) = (reqwest::get(url)).await else {
tracing::error!("fetch error");
continue;
let mut cache = state.0.get_cache.lock().await;
let now = time::OffsetDateTime::now_utc();
let needs_update = match cache.get(&url) {
None => {
tracing::info!("Value is not present in cache");
true
}
Some((cached_time, _)) => {
let other_day = cached_time.to_julian_day() != now.to_julian_day();
let other_hour = cached_time.hour() != now.hour();
if other_day {
tracing::info!("Value is from another day");
}
if other_hour {
tracing::info!("Value is from another hour");
}
other_day || other_hour
}
};
let Ok(text) = res.text().await else {
tracing::error!("fetch decode text error");
if needs_update {
tracing::info!("Need update cache");
let Ok(res) = (reqwest::get(url.clone())).await else {
tracing::error!("fetch error");
continue;
};
let Ok(text) = res.text().await else {
tracing::error!("fetch decode text error");
continue;
};
tracing::info!("Cache updated");
cache.insert(url.clone(), (now, text));
}
let Some((_, text)) = cache.get(&url) else {
tracing::error!("filling cache error");
continue;
};