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
|
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.replacen('-', "", 1)));
}
router.mount(route, handler);
}
|