aboutsummaryrefslogtreecommitdiff
path: root/src/route.rs
blob: ddee2d999fcdf1ef88622512c37d6332fd4221e4 (plain) (blame)
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
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()
  })
}

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,
{
  (*ROUTES.lock().unwrap()).insert(route.to_string(), Route::new(description));
  router.mount(route, handler);
}