aboutsummaryrefslogtreecommitdiff
path: root/src/route.rs
blob: 032381d30f361980ea735c3989239a405ba10a9b (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
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
82
83
84
85
86
87
88
89
// This file is part of Locus <https://github.com/gemrest/locus>.
// Copyright (C) 2022-2022 Fuwn <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only

use std::{collections::HashMap, lazy::SyncLazy, sync::Mutex};

use tokio::time::Instant;

#[cfg(debug_assertions)]
pub const CACHE_RATE: u64 = 1;
#[cfg(not(debug_assertions))]
pub const CACHE_RATE: u64 = 60 * 5;

pub static ROUTES: SyncLazy<Mutex<HashMap<String, Route>>> =
  SyncLazy::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:  "".to_string(),
    }
  }
}

pub fn hits_from(route: &str) -> i32 {
  if let Ok(database) = crate::DATABASE.lock() {
    (*database)
      .get::<i32>(if route.is_empty() { "/" } else { route })
      .unwrap()
  } else {
    0
  }
}

pub fn track_mount(
  router: &mut windmark::Router,
  route: &str,
  description: &str,
  handler: windmark::handler::RouteResponse,
) {
  (*ROUTES.lock().unwrap()).insert(route.to_string(), Route::new(description));
  router.mount(route, handler);
}

pub fn cache(context: &windmark::returnable::RouteContext<'_>, response: &str) {
  static LAST_CACHED: SyncLazy<Mutex<Instant>> =
    SyncLazy::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 {}", context.url.path());
  }
}