aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFuwn <[email protected]>2022-04-14 18:29:03 -0700
committerFuwn <[email protected]>2022-04-14 18:29:03 -0700
commit97b08663354d5f2c494151bcd97d40475ee8d3b7 (patch)
treeec53d4348bbfa03c4faa9e83fcf2378e8947b9a3
parentfeat: route fixing (diff)
downloadlocus-97b08663354d5f2c494151bcd97d40475ee8d3b7.tar.xz
locus-97b08663354d5f2c494151bcd97d40475ee8d3b7.zip
feat: sitemap
-rw-r--r--Cargo.toml2
-rw-r--r--src/macros.rs4
-rw-r--r--src/main.rs32
-rw-r--r--src/modules.rs11
4 files changed, 43 insertions, 6 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 00ed2c8..0725425 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -22,7 +22,7 @@ codegen-units = 1
[dependencies]
# Gemini
-windmark = { version = "0.1.12", features = ["logger", "auto-deduce-mime"] }
+windmark = { version = "0.1.13", features = ["logger", "auto-deduce-mime"] }
# Loggging
log = "0.4.16"
diff --git a/src/macros.rs b/src/macros.rs
index 4882475..997f7d5 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -19,6 +19,8 @@
#[macro_export]
macro_rules! mount_page {
($router:ident, $at:literal, $file:literal) => {
+ (*crate::ROUTES.lock().unwrap()).push($at.to_string());
+
($router).mount(
$at,
Box::new(|context| {
@@ -45,6 +47,8 @@ macro_rules! mount_page {
#[macro_export]
macro_rules! mount_file {
($router:ident, $at:literal, $file:literal) => {
+ (*crate::ROUTES.lock().unwrap()).push($at.to_string());
+
($router).mount(
$at,
Box::new(|_| {
diff --git a/src/main.rs b/src/main.rs
index 28f8ff3..5404af9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -61,6 +61,8 @@ static DATABASE: SyncLazy<Mutex<PickleDb>> = SyncLazy::new(|| {
}
})
});
+static ROUTES: SyncLazy<Mutex<Vec<String>>> =
+ SyncLazy::new(|| Mutex::new(vec![]));
#[derive(Template)]
#[template(path = "main")]
@@ -82,6 +84,15 @@ fn hits_from_route(route: &str) -> i32 {
}
}
+fn track_mount(
+ router: &mut Router,
+ route: &str,
+ handler: windmark::handler::RouteResponse,
+) {
+ (*ROUTES.lock().unwrap()).push(route.to_string());
+ router.mount(route, handler);
+}
+
#[windmark::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
std::env::set_var("RUST_LOG", "windmark,locus=trace");
@@ -132,7 +143,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
)
}));
router.set_fix_path(true);
- router.mount(
+ track_mount(
+ &mut router,
"/uptime",
Box::new(move |context| {
success!(
@@ -171,5 +183,23 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
time_mount.elapsed().as_nanos() as f64 / 1_000_000.0
);
+ track_mount(
+ &mut router,
+ "/sitemap",
+ Box::new(|context| {
+ success!(
+ format!(
+ "# SITEMAP\n\n{}",
+ (*ROUTES.lock().unwrap())
+ .iter()
+ .map(|r| format!("=> {}", r))
+ .collect::<Vec<_>>()
+ .join("\n")
+ ),
+ context
+ )
+ }),
+ );
+
router.run().await
}
diff --git a/src/modules.rs b/src/modules.rs
index 0067266..8189fb3 100644
--- a/src/modules.rs
+++ b/src/modules.rs
@@ -25,7 +25,7 @@ use std::{
use rand::seq::SliceRandom;
use windmark::Response;
-use crate::{success, Main, QUOTES};
+use crate::{success, track_mount, Main, QUOTES};
#[allow(clippy::too_many_lines)]
pub fn multi_blog(router: &mut windmark::Router) {
@@ -103,7 +103,8 @@ pub fn multi_blog(router: &mut windmark::Router) {
let blog_clone = blogs.clone();
- router.mount(
+ track_mount(
+ router,
"/blog",
Box::new(move |context| {
success!(
@@ -135,7 +136,8 @@ pub fn multi_blog(router: &mut windmark::Router) {
let entries_clone = entries.clone();
let fixed_blog_name_clone = fixed_blog_name.clone();
- router.mount(
+ track_mount(
+ router,
&format!("/blog/{}", fixed_blog_name),
Box::new(move |context| {
success!(
@@ -168,7 +170,8 @@ pub fn multi_blog(router: &mut windmark::Router) {
);
for (title, contents) in entries {
- router.mount(
+ track_mount(
+ router,
&format!("/blog/{}/{}", fixed_blog_name, title.to_lowercase()),
Box::new(move |context| success!(contents, context)),
);