diff --git a/src/main.rs b/src/main.rs
index aae9592..d1b69b2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -24,7 +24,7 @@ async fn main() {
// the core application, defining the routes and handlers
let app = Router::new()
- .stripped_clone("/count/", get(get_count))
+ .route("/count", get(get_count))
.route("/count/:period", get(get_count))
.with_state(pool.clone())
.into_make_service();
@@ -63,30 +63,17 @@ async fn get_count(
"day" => {
let then = now - chrono::Duration::try_hours(24).unwrap();
let then = then.to_rfc3339();
- let then = &then;
- sqlx::query_scalar!(
- "select count(*) from hits where page = ? and accessed > ?",
- page,
- then
- )
- .fetch_one(&db)
- .await
- .unwrap_or(1)
+ get_period_hits(&db, page, &then).await
}
"week" => {
let then = now - chrono::Duration::try_days(7).unwrap();
let then = then.to_rfc3339();
- let then = &then;
- sqlx::query_scalar!(
- "select count(*) from hits where page = ? and accessed > ?",
- page,
- then
- )
+ get_period_hits(&db, page, &then).await
+ }
+ _ => sqlx::query_scalar!("select count(*) from hits where page = ?", page)
.fetch_one(&db)
.await
- .unwrap_or(1)
- }
- _ => 2,
+ .unwrap_or(1),
}
} else {
// increment the counter and return the all-time total
@@ -105,6 +92,21 @@ async fn get_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 > ?",
+ page,
+ when
+ )
+ .fetch_one(db)
+ .await
+ .unwrap_or(1)
+}
+
//-************************************************************************
// li'l helpers
//-************************************************************************
@@ -149,8 +151,8 @@ async fn db() -> SqlitePool {
let count = sqlx::query_scalar!("select count(*) from hits")
.fetch_one(&pool)
.await
- .expect("could not get customer count from DB");
- log::info!("Connected to DB, found {count} customers.");
+ .expect("could not get hit count from DB");
+ log::info!("Connected to DB, found {count} total hits.");
pool
}
@@ -170,25 +172,6 @@ async fn mklistener() -> TcpListener {
TcpListener::bind(&addr).await.unwrap()
}
-/// Adds both routes, with and without a trailing slash.
-trait RouterPathStrip
- {{ self.0 }}
-
-where
- S: Clone + Send + Sync + 'static,
-{
- fn stripped_clone(self, path: &str, method_router: MethodRouter) -> Self;
-}
-
-impl RouterPathStrip for Router
-where
- S: Clone + Send + Sync + 'static,
-{
- fn stripped_clone(self, path: &str, method_router: MethodRouter) -> Self {
- assert!(path.ends_with('/'));
- self.route(path, method_router.clone())
- .route(path.trim_end_matches('/'), method_router)
- }
-}
-
async fn shutdown_signal() {
use tokio::signal;
let ctrl_c = async {
diff --git a/templates/base.html b/templates/base.html
deleted file mode 100644
index ce647f6..0000000
--- a/templates/base.html
+++ /dev/null
@@ -1,27 +0,0 @@
-
-
-
-
-
-
- You did it!
-
-
Now, head on over to the login page and git - going! -
- -{% endblock %}