1
Fork 0
mirror of https://github.com/RGBCube/Site synced 2025-07-31 13:07:46 +00:00

Add root file serving & robots.txt

This commit is contained in:
RGBCube 2024-01-09 00:36:00 +03:00
parent bb8def1915
commit 7ffe78fa2d
No known key found for this signature in database
5 changed files with 33 additions and 23 deletions

1
.gitignore vendored
View file

@ -24,3 +24,4 @@
!*.md
!*.toml
!*.txt

View file

@ -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<HashMap<String, Bytes>> = LazyLock::new(|| {
let mut assets = HashMap::new();
@ -37,15 +45,29 @@ static ASSETS: LazyLock<HashMap<String, Bytes>> = 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<String>) -> Response<Body> {
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()
}

View file

@ -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<HashMap<String, (Metadata, Markup)>> = LazyLock::new(
))
}))
});
pub async fn handler(Path(path): Path<String>) -> Markup {
if let Some((metadata, body)) = PAGES.get(&path) {
text::create(Some(&metadata.title), Page::from_str(&path), body)
} else {
not_found::handler().await
}
}

View file

@ -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))
}

2
src/routes/robots.txt Normal file
View file

@ -0,0 +1,2 @@
User-Agent: *
Allow: /