add basic logging

This commit is contained in:
xenofem 2022-04-16 00:03:09 -04:00
parent 69e44b9cbe
commit 7a3fd3f2ca
4 changed files with 54 additions and 1 deletions

View file

@ -2,6 +2,7 @@ use std::time::{Duration, Instant, SystemTime};
use futures::{sink::SinkExt, TryStreamExt};
use lazy_static::lazy_static;
use log::info;
use reqwest::Url;
use scraper::Selector;
use time::PrimitiveDateTime;
@ -50,6 +51,8 @@ impl PdfFetcher {
}
pub async fn fetch(&mut self) -> Result<pdf::file::File<Vec<u8>>, Error> {
info!("Fetching data PDF");
let cache_modtime = match tokio::fs::File::open(CACHED_PDF_PATH).await {
Ok(file) => Some(
file.metadata()
@ -65,6 +68,7 @@ impl PdfFetcher {
if let Some(instant) = self.last_checked {
if now - instant < *MIN_CHECK_INTERVAL {
return if cache_modtime.is_some() {
info!("Already checked origin recently, not rechecking");
self.cached_pdf()
} else {
Err(Error::TooSoon)
@ -111,6 +115,8 @@ impl PdfFetcher {
.map_or(true, |(cache, origin)| origin > cache);
if outdated {
info!("Cached PDF is outdated, downloading latest version");
let mut pdf_stream = self
.client
.get(pdf_url)
@ -124,6 +130,8 @@ impl PdfFetcher {
sink.send_all(&mut pdf_stream).await?;
<dyn futures::Sink<bytes::Bytes, Error = std::io::Error> + Unpin>::close(&mut sink)
.await?;
} else {
info!("Cached PDF is already up to date");
}
self.last_checked = Some(now);
self.cached_pdf()

View file

@ -1,6 +1,9 @@
use std::{sync::Arc, time::Duration};
use actix_web::{get, http::header::ContentType, web, App, HttpResponse, HttpServer, Responder};
use actix_web::{
get, http::header::ContentType, middleware::Logger, web, App, HttpResponse, HttpServer,
Responder,
};
use lazy_static::lazy_static;
use tokio::sync::RwLock;
@ -60,11 +63,14 @@ async fn start_updater() -> Result<web::Data<AppState>, Error> {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
simple_logger::init_with_level(log::Level::Info).unwrap();
let state = start_updater().await.expect("Failed to initialize state");
HttpServer::new(move || {
App::new()
.app_data(state.clone())
.wrap(Logger::default())
.service(csv)
.service(json)
.service(actix_files::Files::new("/", "./static/").index_file("index.html"))