From 7ffe78fa2de1a10c193ce606dbdd4593d2137880 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Tue, 9 Jan 2024 00:36:00 +0300 Subject: [PATCH] Add root file serving & robots.txt --- .gitignore | 1 + src/routes/assets.rs | 32 ++++++++++++++++++++++++++++---- src/routes/markdown.rs | 18 +----------------- src/routes/mod.rs | 3 +-- src/routes/robots.txt | 2 ++ 5 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 src/routes/robots.txt diff --git a/.gitignore b/.gitignore index c626d8c..f01ecd7 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,4 @@ !*.md !*.toml +!*.txt diff --git a/src/routes/assets.rs b/src/routes/assets.rs index d485bab..764d009 100644 --- a/src/routes/assets.rs +++ b/src/routes/assets.rs @@ -16,9 +16,17 @@ use axum::{ }; use bytes::Bytes; -use crate::minify; +use super::markdown::PAGES; +use crate::{ + minify, + page::{ + text, + Page, + }, +}; -const ASSET_EXTENSIONS: &[&str] = &[".js", ".css", ".woff2", ".gif"]; +const ASSET_EXTENSIONS: &[&str] = &[".js", ".css", ".woff2", ".gif", ".txt"]; +const ROOT_EXTENSIONS: &[&str] = &[".txt"]; static ASSETS: LazyLock> = LazyLock::new(|| { let mut assets = HashMap::new(); @@ -37,15 +45,29 @@ static ASSETS: LazyLock> = LazyLock::new(|| { continue; } + let add_asset_prefix = |path: &str| { + if ROOT_EXTENSIONS + .iter() + .any(|extension| path.ends_with(extension)) + { + path.to_string() + } else { + format!("assets/{path}") + } + }; + if minify::is_minifiable(path) { let content = minify::generic(path, file.content()); log::info!("Minifying asset {path}"); - assets.insert(minify::insert_min(path), Bytes::from(content)); + assets.insert( + add_asset_prefix(&minify::insert_min(path)), + Bytes::from(content), + ); } log::info!("Adding asset {path}"); - assets.insert(path.to_string(), Bytes::from(file.content().to_vec())); + assets.insert(add_asset_prefix(path), Bytes::from(file.content().to_vec())); } assets @@ -63,6 +85,8 @@ pub async fn handler(Path(path): Path) -> Response { Bytes::clone(body), ) .into_response() + } else if let Some((metadata, body)) = PAGES.get(&path) { + text::create(Some(&metadata.title), Page::from_str(&path), body).into_response() } else { StatusCode::NOT_FOUND.into_response() } diff --git a/src/routes/markdown.rs b/src/routes/markdown.rs index 5a18241..618fb1d 100644 --- a/src/routes/markdown.rs +++ b/src/routes/markdown.rs @@ -3,19 +3,11 @@ use std::{ sync::LazyLock, }; -use axum::extract::Path; use chrono::NaiveDate; use maud::Markup; use serde::Deserialize; -use crate::{ - errors::not_found, - markdown, - page::{ - text, - Page, - }, -}; +use crate::markdown; #[derive(Deserialize, Debug)] pub struct Metadata { @@ -46,11 +38,3 @@ pub static PAGES: LazyLock> = LazyLock::new( )) })) }); - -pub async fn handler(Path(path): Path) -> Markup { - if let Some((metadata, body)) = PAGES.get(&path) { - text::create(Some(&metadata.title), Page::from_str(&path), body) - } else { - not_found::handler().await - } -} diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 65aa6ee..f6c4dcc 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -13,6 +13,5 @@ pub fn router() -> Router { .route("/", get(index::handler)) .route("/blog", get(blog::index_handler)) .route("/blog/:entry", get(blog::entry_handler)) - .route("/*page", get(markdown::handler)) - .route("/assets/*path", get(assets::handler)) + .route("/*path", get(assets::handler)) } diff --git a/src/routes/robots.txt b/src/routes/robots.txt new file mode 100644 index 0000000..f6e6d1d --- /dev/null +++ b/src/routes/robots.txt @@ -0,0 +1,2 @@ +User-Agent: * +Allow: /