split handler into registration fn and count-fetcher
This commit is contained in:
parent
f6e49ff98f
commit
a0a5f8a575
81
src/main.rs
81
src/main.rs
|
@ -21,11 +21,9 @@ async fn main() {
|
||||||
init();
|
init();
|
||||||
|
|
||||||
let pool = db().await;
|
let pool = db().await;
|
||||||
|
|
||||||
// the core application, defining the routes and handlers
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/count", get(get_count))
|
.route("/hit", get(count_hit))
|
||||||
.route("/count/:period", get(get_count))
|
.route("/hits/:period", get(get_hits))
|
||||||
.with_state(pool.clone())
|
.with_state(pool.clone())
|
||||||
.into_make_service();
|
.into_make_service();
|
||||||
let listener = mklistener().await;
|
let listener = mklistener().await;
|
||||||
|
@ -37,10 +35,36 @@ async fn main() {
|
||||||
pool.close().await;
|
pool.close().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn count_hit(State(db): State<SqlitePool>, 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]
|
#[axum::debug_handler]
|
||||||
async fn get_count(
|
async fn get_hits(
|
||||||
State(db): State<SqlitePool>,
|
State(db): State<SqlitePool>,
|
||||||
period: Option<Path<String>>,
|
period: Path<String>,
|
||||||
headers: HeaderMap,
|
headers: HeaderMap,
|
||||||
) -> String {
|
) -> String {
|
||||||
let now = chrono::Utc::now();
|
let now = chrono::Utc::now();
|
||||||
|
@ -57,47 +81,26 @@ async fn get_count(
|
||||||
};
|
};
|
||||||
let page = &page;
|
let page = &page;
|
||||||
|
|
||||||
let count = if let Some(Path(period)) = period {
|
let count = match period.as_str() {
|
||||||
// don't increment the counter, just fetch the requested period
|
"day" => {
|
||||||
match period.as_str() {
|
let then = now - chrono::Duration::try_hours(24).unwrap();
|
||||||
"day" => {
|
let then = then.to_rfc3339();
|
||||||
let then = now - chrono::Duration::try_hours(24).unwrap();
|
get_period_hits(&db, page, &then).await
|
||||||
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),
|
|
||||||
}
|
}
|
||||||
} else {
|
"week" => {
|
||||||
// increment the counter and return the all-time total
|
let then = now - chrono::Duration::try_days(7).unwrap();
|
||||||
let now = now.to_rfc3339();
|
let then = then.to_rfc3339();
|
||||||
let now = &now;
|
get_period_hits(&db, page, &then).await
|
||||||
sqlx::query!("insert into hits (page, accessed) values (?, ?)", page, now)
|
}
|
||||||
.execute(&db)
|
_ => sqlx::query_scalar!("select count(*) from hits where page = ?", page)
|
||||||
.await
|
|
||||||
.unwrap_or_default();
|
|
||||||
sqlx::query_scalar!("select count(*) from hits where page = ?", page)
|
|
||||||
.fetch_one(&db)
|
.fetch_one(&db)
|
||||||
.await
|
.await
|
||||||
.unwrap_or(1)
|
.unwrap_or(1),
|
||||||
};
|
};
|
||||||
|
|
||||||
dbg!(count);
|
|
||||||
|
|
||||||
format!("{count}")
|
format!("{count}")
|
||||||
}
|
}
|
||||||
|
|
||||||
//-************************************************************************
|
|
||||||
// DB fetching helpers
|
|
||||||
//-************************************************************************
|
|
||||||
|
|
||||||
async fn get_period_hits(db: &SqlitePool, page: &str, when: &str) -> i32 {
|
async fn get_period_hits(db: &SqlitePool, page: &str, when: &str) -> i32 {
|
||||||
sqlx::query_scalar!(
|
sqlx::query_scalar!(
|
||||||
"select count(*) from hits where page = ? and accessed > ?",
|
"select count(*) from hits where page = ? and accessed > ?",
|
||||||
|
|
Loading…
Reference in New Issue