From a0a5f8a5751b769947157519f7c2b7c6923cc491 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 17 Mar 2024 15:44:50 -0700 Subject: [PATCH] split handler into registration fn and count-fetcher --- src/main.rs | 81 +++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/src/main.rs b/src/main.rs index bd92044..1b2aa75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,11 +21,9 @@ async fn main() { init(); let pool = db().await; - - // the core application, defining the routes and handlers let app = Router::new() - .route("/count", get(get_count)) - .route("/count/:period", get(get_count)) + .route("/hit", get(count_hit)) + .route("/hits/:period", get(get_hits)) .with_state(pool.clone()) .into_make_service(); let listener = mklistener().await; @@ -37,10 +35,36 @@ async fn main() { pool.close().await; } +async fn count_hit(State(db): State, headers: HeaderMap) -> String { + let now = chrono::Utc::now(); + let referer = headers.get(REFERER); + let page = if let Some(referer) = referer { + let p = referer.to_str().unwrap_or("/").to_string(); + if let Ok(path) = Url::parse(&p) { + path.path().to_string() + } else { + return "".to_string(); + } + } else { + return "".to_string(); + }; + let page = &page; + + let now = now.to_rfc3339(); + let now = &now; + + sqlx::query!("insert into hits (page, accessed) values (?, ?)", page, now) + .execute(&db) + .await + .unwrap_or_default(); + + "".to_string() +} + #[axum::debug_handler] -async fn get_count( +async fn get_hits( State(db): State, - period: Option>, + period: Path, headers: HeaderMap, ) -> String { let now = chrono::Utc::now(); @@ -57,47 +81,26 @@ async fn get_count( }; let page = &page; - let count = if let Some(Path(period)) = period { - // don't increment the counter, just fetch the requested period - match period.as_str() { - "day" => { - let then = now - chrono::Duration::try_hours(24).unwrap(); - let then = then.to_rfc3339(); - get_period_hits(&db, page, &then).await - } - "week" => { - let then = now - chrono::Duration::try_days(7).unwrap(); - let then = then.to_rfc3339(); - get_period_hits(&db, page, &then).await - } - _ => sqlx::query_scalar!("select count(*) from hits where page = ?", page) - .fetch_one(&db) - .await - .unwrap_or(1), + let count = match period.as_str() { + "day" => { + let then = now - chrono::Duration::try_hours(24).unwrap(); + let then = then.to_rfc3339(); + get_period_hits(&db, page, &then).await } - } else { - // increment the counter and return the all-time total - let now = now.to_rfc3339(); - let now = &now; - sqlx::query!("insert into hits (page, accessed) values (?, ?)", page, now) - .execute(&db) - .await - .unwrap_or_default(); - sqlx::query_scalar!("select count(*) from hits where page = ?", page) + "week" => { + let then = now - chrono::Duration::try_days(7).unwrap(); + let then = then.to_rfc3339(); + get_period_hits(&db, page, &then).await + } + _ => sqlx::query_scalar!("select count(*) from hits where page = ?", page) .fetch_one(&db) .await - .unwrap_or(1) + .unwrap_or(1), }; - dbg!(count); - format!("{count}") } -//-************************************************************************ -// DB fetching helpers -//-************************************************************************ - async fn get_period_hits(db: &SqlitePool, page: &str, when: &str) -> i32 { sqlx::query_scalar!( "select count(*) from hits where page = ? and accessed > ?",