remove templates, tidy handler
This commit is contained in:
parent
1ea0379411
commit
b802372f7a
63
src/main.rs
63
src/main.rs
|
@ -24,7 +24,7 @@ async fn main() {
|
||||||
|
|
||||||
// the core application, defining the routes and handlers
|
// the core application, defining the routes and handlers
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.stripped_clone("/count/", get(get_count))
|
.route("/count", get(get_count))
|
||||||
.route("/count/:period", get(get_count))
|
.route("/count/:period", get(get_count))
|
||||||
.with_state(pool.clone())
|
.with_state(pool.clone())
|
||||||
.into_make_service();
|
.into_make_service();
|
||||||
|
@ -63,30 +63,17 @@ async fn get_count(
|
||||||
"day" => {
|
"day" => {
|
||||||
let then = now - chrono::Duration::try_hours(24).unwrap();
|
let then = now - chrono::Duration::try_hours(24).unwrap();
|
||||||
let then = then.to_rfc3339();
|
let then = then.to_rfc3339();
|
||||||
let then = &then;
|
get_period_hits(&db, page, &then).await
|
||||||
sqlx::query_scalar!(
|
|
||||||
"select count(*) from hits where page = ? and accessed > ?",
|
|
||||||
page,
|
|
||||||
then
|
|
||||||
)
|
|
||||||
.fetch_one(&db)
|
|
||||||
.await
|
|
||||||
.unwrap_or(1)
|
|
||||||
}
|
}
|
||||||
"week" => {
|
"week" => {
|
||||||
let then = now - chrono::Duration::try_days(7).unwrap();
|
let then = now - chrono::Duration::try_days(7).unwrap();
|
||||||
let then = then.to_rfc3339();
|
let then = then.to_rfc3339();
|
||||||
let then = &then;
|
get_period_hits(&db, page, &then).await
|
||||||
sqlx::query_scalar!(
|
}
|
||||||
"select count(*) from hits where page = ? and accessed > ?",
|
_ => sqlx::query_scalar!("select count(*) from hits where page = ?", page)
|
||||||
page,
|
|
||||||
then
|
|
||||||
)
|
|
||||||
.fetch_one(&db)
|
.fetch_one(&db)
|
||||||
.await
|
.await
|
||||||
.unwrap_or(1)
|
.unwrap_or(1),
|
||||||
}
|
|
||||||
_ => 2,
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// increment the counter and return the all-time total
|
// increment the counter and return the all-time total
|
||||||
|
@ -105,6 +92,21 @@ async fn get_count(
|
||||||
format!("{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
|
// li'l helpers
|
||||||
//-************************************************************************
|
//-************************************************************************
|
||||||
|
@ -149,8 +151,8 @@ async fn db() -> SqlitePool {
|
||||||
let count = sqlx::query_scalar!("select count(*) from hits")
|
let count = sqlx::query_scalar!("select count(*) from hits")
|
||||||
.fetch_one(&pool)
|
.fetch_one(&pool)
|
||||||
.await
|
.await
|
||||||
.expect("could not get customer count from DB");
|
.expect("could not get hit count from DB");
|
||||||
log::info!("Connected to DB, found {count} customers.");
|
log::info!("Connected to DB, found {count} total hits.");
|
||||||
|
|
||||||
pool
|
pool
|
||||||
}
|
}
|
||||||
|
@ -170,25 +172,6 @@ async fn mklistener() -> TcpListener {
|
||||||
TcpListener::bind(&addr).await.unwrap()
|
TcpListener::bind(&addr).await.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds both routes, with and without a trailing slash.
|
|
||||||
trait RouterPathStrip<S>
|
|
||||||
where
|
|
||||||
S: Clone + Send + Sync + 'static,
|
|
||||||
{
|
|
||||||
fn stripped_clone(self, path: &str, method_router: MethodRouter<S>) -> Self;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<S> RouterPathStrip<S> for Router<S>
|
|
||||||
where
|
|
||||||
S: Clone + Send + Sync + 'static,
|
|
||||||
{
|
|
||||||
fn stripped_clone(self, path: &str, method_router: MethodRouter<S>) -> Self {
|
|
||||||
assert!(path.ends_with('/'));
|
|
||||||
self.route(path, method_router.clone())
|
|
||||||
.route(path.trim_end_matches('/'), method_router)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn shutdown_signal() {
|
async fn shutdown_signal() {
|
||||||
use tokio::signal;
|
use tokio::signal;
|
||||||
let ctrl_c = async {
|
let ctrl_c = async {
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<title>{% block title %}{{ title }}{% endblock %}</title>
|
|
||||||
<link rel="stylesheet" href="/assets/css/index.css">
|
|
||||||
<link rel="stylesheet" href="/assets/css/theme-forgejo-auto.css">
|
|
||||||
|
|
||||||
{% block head %}{% endblock %}
|
|
||||||
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
<div id="header">
|
|
||||||
{% block header %}{% endblock %}
|
|
||||||
</div>
|
|
||||||
<div id="content">
|
|
||||||
{% block content %}{% endblock %}
|
|
||||||
</div>
|
|
||||||
<div id="footer">
|
|
||||||
{% block footer %}{% endblock %}
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
|
@ -1,10 +0,0 @@
|
||||||
{% macro get_or_default(val, def) %}
|
|
||||||
|
|
||||||
{% match val %}
|
|
||||||
{% when Some with (v) %}
|
|
||||||
{{v}}
|
|
||||||
{% else %}
|
|
||||||
{{def}}
|
|
||||||
{% endmatch %}
|
|
||||||
|
|
||||||
{% endmacro %}
|
|
|
@ -1,20 +0,0 @@
|
||||||
{% extends "base.html" %}
|
|
||||||
|
|
||||||
{% block title %}Thanks for Signing up for the Kitten Collective!{% endblock %}
|
|
||||||
|
|
||||||
{% block content %}
|
|
||||||
{% block header %}{% endblock %}
|
|
||||||
|
|
||||||
<h1>You did it!</h1>
|
|
||||||
|
|
||||||
<div id="signup_success">
|
|
||||||
<p>
|
|
||||||
{{ self.0 }}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<p>Now, head on over to <a href="/user/login?redirect_to=%2F{{ self.0.username|escape }}">the login page</a> and git
|
|
||||||
going!
|
|
||||||
</p>
|
|
||||||
|
|
||||||
{% endblock %}
|
|
Loading…
Reference in New Issue