1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
use std::{
collections::HashMap,
future::IntoFuture,
sync::{LazyLock, Mutex},
};
#[cfg(debug_assertions)]
pub const CACHE_RATE: u64 = 1;
#[cfg(not(debug_assertions))]
pub const CACHE_RATE: u64 = 60 * 5;
pub static ROUTES: LazyLock<Mutex<HashMap<String, Route>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
#[derive(Debug)]
pub struct Route {
pub description: String,
pub text_cache: String,
}
impl Route {
pub fn new(description: &str) -> Self {
Self { description: description.to_string(), text_cache: String::new() }
}
}
pub fn hits_from(route: &str) -> i32 {
crate::DATABASE.lock().map_or(0, |database| {
(*database)
.get::<i32>(if route.is_empty() { "/" } else { route })
.unwrap_or(0)
})
}
pub fn track_mount<F, R>(
router: &mut windmark::router::Router,
route: &str,
description: &str,
handler: F,
) where
F: FnMut(windmark::context::RouteContext) -> R + Send + Sync + 'static,
R: IntoFuture<Output = windmark::response::Response> + Send + 'static,
<R as IntoFuture>::IntoFuture: Send,
{
if !description.starts_with('-') {
(*ROUTES.lock().unwrap())
.insert(route.to_string(), Route::new(description));
}
router.mount(route, handler);
}
#[cfg(test)]
mod tests {
use super::{ROUTES, track_mount};
#[test]
fn track_mount_preserves_internal_hyphens_in_public_descriptions() {
let mut router = windmark::router::Router::new();
ROUTES.lock().unwrap().clear();
track_mount(
&mut router,
"/finger",
"Finger-to-Gemini Gateway",
|_context| async {
windmark::response::Response::success("ok".to_string())
},
);
let stored_description = ROUTES
.lock()
.unwrap()
.get("/finger")
.map(|route| route.description.clone());
assert_eq!(
stored_description,
Some("Finger-to-Gemini Gateway".to_string())
);
}
}
|