aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-04-27 01:17:32 -0700
committerFuwn <[email protected]>2022-04-27 01:17:32 -0700
commitc81eab0c3b4fa8d8433505bd3d061f8de8e87d74 (patch)
tree09451b5ebec415295d63504e9eaffecb37277a68 /src
parentrefactor(modules): move search out of module (diff)
downloadlocus-c81eab0c3b4fa8d8433505bd3d061f8de8e87d74.tar.xz
locus-c81eab0c3b4fa8d8433505bd3d061f8de8e87d74.zip
refactor: restrict more
Diffstat (limited to 'src')
-rw-r--r--src/macros.rs16
-rw-r--r--src/main.rs15
-rw-r--r--src/modules/random.rs2
-rw-r--r--src/modules/search.rs5
-rw-r--r--src/modules/sitemap.rs2
-rw-r--r--src/route.rs7
6 files changed, 23 insertions, 24 deletions
diff --git a/src/macros.rs b/src/macros.rs
index c36a41f..3ac6463 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -22,13 +22,23 @@ pub static QUOTES: SyncLazy<Vec<String>> = SyncLazy::new(|| {
serde_json::from_str(include_str!("../content/json/quotes.json")).unwrap()
});
+#[derive(yarte::Template)]
+#[template(path = "main")]
+pub struct Main<'a> {
+ pub body: &'a str,
+ pub hits: &'a i32,
+ pub quote: &'a str,
+ pub commit: &'a str,
+ pub mini_commit: &'a str,
+}
+
#[macro_export]
macro_rules! success {
($body:expr, $context:ident) => {{
$crate::route::cache(&$context, &$body);
windmark::Response::Success(
- $crate::Main {
+ $crate::macros::Main {
body: &$body,
hits: &$crate::route::hits_from($context.url.path()),
quote: {
@@ -50,7 +60,7 @@ macro_rules! success {
#[macro_export]
macro_rules! mount_page {
($router:ident, $at:literal, $description:literal, $file:literal) => {
- (*$crate::ROUTES.lock().unwrap())
+ (*$crate::route::ROUTES.lock().unwrap())
.insert($at.to_string(), $crate::route::Route::new($description));
($router).mount(
@@ -69,7 +79,7 @@ macro_rules! mount_page {
#[macro_export]
macro_rules! mount_file {
($router:ident, $at:literal, $description:literal, $file:literal) => {
- (*$crate::ROUTES.lock().unwrap())
+ (*$crate::route::ROUTES.lock().unwrap())
.insert($at.to_string(), $crate::route::Route::new($description));
($router).mount(
diff --git a/src/main.rs b/src/main.rs
index 0f9dd55..dcefb64 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -36,12 +36,11 @@ mod route;
#[macro_use]
extern crate log;
-use std::{collections::HashMap, lazy::SyncLazy, sync::Mutex};
+use std::{lazy::SyncLazy, sync::Mutex};
use pickledb::PickleDb;
use tokio::time::Instant;
-const SEARCH_SIZE: usize = 10;
const ERROR_HANDLER_RESPONSE: &str =
"The requested resource could not be found at this time. You can try \
refreshing the page, if that doesn't change anything; contact Fuwn! \
@@ -63,18 +62,6 @@ static DATABASE: SyncLazy<Mutex<PickleDb>> = SyncLazy::new(|| {
}
})
});
-static ROUTES: SyncLazy<Mutex<HashMap<String, route::Route>>> =
- SyncLazy::new(|| Mutex::new(HashMap::new()));
-
-#[derive(yarte::Template)]
-#[template(path = "main")]
-struct Main<'a> {
- body: &'a str,
- hits: &'a i32,
- quote: &'a str,
- commit: &'a str,
- mini_commit: &'a str,
-}
fn time_mounts<T>(context: &str, timer: &mut Instant, mut mounter: T)
where T: FnMut() {
diff --git a/src/modules/random.rs b/src/modules/random.rs
index 8604d64..799376a 100644
--- a/src/modules/random.rs
+++ b/src/modules/random.rs
@@ -25,7 +25,7 @@ pub fn module(router: &mut windmark::Router) {
"Get redirected to a random route",
Box::new(|_| {
windmark::Response::TemporaryRedirect(
- (*crate::ROUTES.lock().unwrap())
+ (*crate::route::ROUTES.lock().unwrap())
.iter()
.collect::<Vec<_>>()
.choose(&mut rand::thread_rng())
diff --git a/src/modules/search.rs b/src/modules/search.rs
index 57f2c0c..15d0061 100644
--- a/src/modules/search.rs
+++ b/src/modules/search.rs
@@ -22,6 +22,7 @@ use tantivy::schema;
use tempfile::TempDir;
const SEARCH_INDEX_SIZE: usize = 10_000_000;
+const SEARCH_SIZE: usize = 10;
static INDEX_PATH: SyncLazy<Mutex<TempDir>> =
SyncLazy::new(|| Mutex::new(TempDir::new().unwrap()));
@@ -89,7 +90,7 @@ pub(super) fn module(router: &mut windmark::Router) {
)
.parse_query(&query.0.to_string())
.unwrap(),
- &tantivy::collector::TopDocs::with_limit(crate::SEARCH_SIZE),
+ &tantivy::collector::TopDocs::with_limit(SEARCH_SIZE),
)
.unwrap();
@@ -170,7 +171,7 @@ pub fn index() {
let time = tokio::time::Instant::now();
let mut new = 0;
- for (route_path, information) in &(*crate::ROUTES.lock().unwrap()) {
+ for (route_path, information) in &(*crate::route::ROUTES.lock().unwrap()) {
// Pretty inefficient, but I'll figure this out later.
(*INDEX_WRITER.lock().unwrap())
.delete_all_documents()
diff --git a/src/modules/sitemap.rs b/src/modules/sitemap.rs
index b0a29d2..d9f94a5 100644
--- a/src/modules/sitemap.rs
+++ b/src/modules/sitemap.rs
@@ -25,7 +25,7 @@ pub fn module(router: &mut windmark::Router) {
crate::success!(
format!(
"# SITEMAP\n\n{}",
- (*crate::ROUTES.lock().unwrap())
+ (*crate::route::ROUTES.lock().unwrap())
.iter()
.map(|(r, d)| format!("=> {} {}", r, d.description))
.collect::<Vec<_>>()
diff --git a/src/route.rs b/src/route.rs
index 285d049..032381d 100644
--- a/src/route.rs
+++ b/src/route.rs
@@ -16,17 +16,18 @@
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
-use std::{lazy::SyncLazy, sync::Mutex};
+use std::{collections::HashMap, lazy::SyncLazy, sync::Mutex};
use tokio::time::Instant;
-use crate::ROUTES;
-
#[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,