aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/modules/api/sydney.rs4
-rw-r--r--src/modules/router/module.rs41
-rw-r--r--src/modules/static.rs1
-rw-r--r--src/modules/uptime.rs2
-rw-r--r--src/response.rs2
-rw-r--r--src/route.rs39
6 files changed, 43 insertions, 46 deletions
diff --git a/src/modules/api/sydney.rs b/src/modules/api/sydney.rs
index 6125e59..f2b7e99 100644
--- a/src/modules/api/sydney.rs
+++ b/src/modules/api/sydney.rs
@@ -26,7 +26,7 @@ pub fn module(router: &mut windmark::router::Router) {
router,
"/api/sydney/version",
"Sydney's version",
- move |context| {
+ move |_context| {
async move {
let mut content = "0.0.0".to_string();
@@ -52,8 +52,6 @@ pub fn module(router: &mut windmark::router::Router) {
}
}
- crate::route::cache(&context, &content);
-
windmark::response::Response::success(content)
.with_mime("text/plain")
.clone()
diff --git a/src/modules/router/module.rs b/src/modules/router/module.rs
index 76083b8..a51812b 100644
--- a/src/modules/router/module.rs
+++ b/src/modules/router/module.rs
@@ -16,8 +16,47 @@
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
+use std::sync::{LazyLock, Mutex};
+
+use tokio::time::Instant;
use windmark::context::HookContext;
+use crate::route::{CACHE_RATE, ROUTES};
+
+fn cache(context: &windmark::context::HookContext, response: &str) {
+ static LAST_CACHED: LazyLock<Mutex<Instant>> =
+ LazyLock::new(|| Mutex::new(Instant::now()));
+
+ if (*LAST_CACHED.lock().unwrap()).elapsed()
+ >= std::time::Duration::from_secs(CACHE_RATE)
+ || (*ROUTES.lock().unwrap())
+ .get(context.url.path())
+ .is_some_and(|r| r.text_cache.is_empty())
+ {
+ (*LAST_CACHED.lock().unwrap()) = Instant::now();
+
+ if let Some(route) = (*ROUTES.lock().unwrap()).get_mut(context.url.path()) {
+ route.text_cache = response.to_string();
+ info!("cache set for {}", context.url.path());
+ } else {
+ warn!(
+ "cache could not be set for {} as it is not being tracked",
+ context.url.path()
+ );
+ }
+
+ trace!("recache for {}", {
+ let path = context.url.path();
+
+ if path.is_empty() {
+ "/"
+ } else {
+ path
+ }
+ });
+ }
+}
+
pub fn module(router: &mut windmark::router::Router) {
router.set_pre_route_callback(move |context: HookContext| {
info!(
@@ -36,6 +75,8 @@ pub fn module(router: &mut windmark::router::Router) {
context.url.to_string(),
);
+ cache(&context, &response.content);
+
if let Some(language) =
windmark::utilities::queries_from_url(&context.url).get("translate")
{
diff --git a/src/modules/static.rs b/src/modules/static.rs
index 94f2220..6290244 100644
--- a/src/modules/static.rs
+++ b/src/modules/static.rs
@@ -26,7 +26,6 @@ macro_rules! batch_mount {
let content =
include_str!(concat!("../../content/static/", $file, ".gmi"));
- $crate::route::cache(&context, &content);
$crate::response::success(&content, &context)
});
)*
diff --git a/src/modules/uptime.rs b/src/modules/uptime.rs
index 64e1804..ebe942f 100644
--- a/src/modules/uptime.rs
+++ b/src/modules/uptime.rs
@@ -47,8 +47,6 @@ pub fn module(router: &mut windmark::router::Router) {
},
);
- crate::route::cache(&context, &response);
-
windmark::response::Response::success(response)
},
);
diff --git a/src/response.rs b/src/response.rs
index b368ba1..ac33adf 100644
--- a/src/response.rs
+++ b/src/response.rs
@@ -37,8 +37,6 @@ pub fn success(
body: &impl ToString,
context: &windmark::context::RouteContext,
) -> Response {
- crate::route::cache(context, &body.to_string());
-
Response::success(
Main {
body: &body.to_string(),
diff --git a/src/route.rs b/src/route.rs
index a983374..8d7695d 100644
--- a/src/route.rs
+++ b/src/route.rs
@@ -22,9 +22,6 @@ use std::{
sync::{LazyLock, Mutex},
};
-use tokio::time::Instant;
-use windmark::context::RouteContext;
-
#[cfg(debug_assertions)]
pub const CACHE_RATE: u64 = 1;
#[cfg(not(debug_assertions))]
@@ -61,44 +58,10 @@ pub fn track_mount<F, R>(
description: &str,
handler: F,
) where
- F: FnMut(RouteContext) -> R + Send + Sync + 'static,
+ F: FnMut(windmark::context::RouteContext) -> R + Send + Sync + 'static,
R: IntoFuture<Output = windmark::response::Response> + Send + 'static,
<R as IntoFuture>::IntoFuture: Send,
{
(*ROUTES.lock().unwrap()).insert(route.to_string(), Route::new(description));
router.mount(route, handler);
}
-
-pub fn cache(context: &RouteContext, response: &str) {
- static LAST_CACHED: LazyLock<Mutex<Instant>> =
- LazyLock::new(|| Mutex::new(Instant::now()));
-
- if (*LAST_CACHED.lock().unwrap()).elapsed()
- >= std::time::Duration::from_secs(CACHE_RATE)
- || (*ROUTES.lock().unwrap())
- .get(context.url.path())
- .is_some_and(|r| r.text_cache.is_empty())
- {
- (*LAST_CACHED.lock().unwrap()) = Instant::now();
-
- if let Some(route) = (*ROUTES.lock().unwrap()).get_mut(context.url.path()) {
- route.text_cache = response.to_string();
- info!("cache set for {}", context.url.path());
- } else {
- warn!(
- "cache could not be set for {} as it is not being tracked",
- context.url.path()
- );
- }
-
- trace!("recache for {}", {
- let path = context.url.path();
-
- if path.is_empty() {
- "/"
- } else {
- path
- }
- });
- }
-}