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();
|
||||
|
||||
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<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]
|
||||
async fn get_count(
|
||||
async fn get_hits(
|
||||
State(db): State<SqlitePool>,
|
||||
period: Option<Path<String>>,
|
||||
period: Path<String>,
|
||||
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 > ?",
|
||||
|
|
Loading…
Reference in New Issue