mirror of
https://github.com/RGBCube/Site
synced 2025-08-01 13:37:49 +00:00
Add root file serving & robots.txt
This commit is contained in:
parent
bb8def1915
commit
7ffe78fa2d
5 changed files with 33 additions and 23 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -24,3 +24,4 @@
|
||||||
|
|
||||||
!*.md
|
!*.md
|
||||||
!*.toml
|
!*.toml
|
||||||
|
!*.txt
|
||||||
|
|
|
@ -16,9 +16,17 @@ use axum::{
|
||||||
};
|
};
|
||||||
use bytes::Bytes;
|
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(|| {
|
static ASSETS: LazyLock<HashMap<String, Bytes>> = LazyLock::new(|| {
|
||||||
let mut assets = HashMap::new();
|
let mut assets = HashMap::new();
|
||||||
|
@ -37,15 +45,29 @@ static ASSETS: LazyLock<HashMap<String, Bytes>> = LazyLock::new(|| {
|
||||||
continue;
|
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) {
|
if minify::is_minifiable(path) {
|
||||||
let content = minify::generic(path, file.content());
|
let content = minify::generic(path, file.content());
|
||||||
|
|
||||||
log::info!("Minifying asset {path}");
|
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}");
|
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
|
assets
|
||||||
|
@ -63,6 +85,8 @@ pub async fn handler(Path(path): Path<String>) -> Response<Body> {
|
||||||
Bytes::clone(body),
|
Bytes::clone(body),
|
||||||
)
|
)
|
||||||
.into_response()
|
.into_response()
|
||||||
|
} else if let Some((metadata, body)) = PAGES.get(&path) {
|
||||||
|
text::create(Some(&metadata.title), Page::from_str(&path), body).into_response()
|
||||||
} else {
|
} else {
|
||||||
StatusCode::NOT_FOUND.into_response()
|
StatusCode::NOT_FOUND.into_response()
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,19 +3,11 @@ use std::{
|
||||||
sync::LazyLock,
|
sync::LazyLock,
|
||||||
};
|
};
|
||||||
|
|
||||||
use axum::extract::Path;
|
|
||||||
use chrono::NaiveDate;
|
use chrono::NaiveDate;
|
||||||
use maud::Markup;
|
use maud::Markup;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::{
|
use crate::markdown;
|
||||||
errors::not_found,
|
|
||||||
markdown,
|
|
||||||
page::{
|
|
||||||
text,
|
|
||||||
Page,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct Metadata {
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -13,6 +13,5 @@ pub fn router() -> Router {
|
||||||
.route("/", get(index::handler))
|
.route("/", get(index::handler))
|
||||||
.route("/blog", get(blog::index_handler))
|
.route("/blog", get(blog::index_handler))
|
||||||
.route("/blog/:entry", get(blog::entry_handler))
|
.route("/blog/:entry", get(blog::entry_handler))
|
||||||
.route("/*page", get(markdown::handler))
|
.route("/*path", get(assets::handler))
|
||||||
.route("/assets/*path", get(assets::handler))
|
|
||||||
}
|
}
|
||||||
|
|
2
src/routes/robots.txt
Normal file
2
src/routes/robots.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
User-Agent: *
|
||||||
|
Allow: /
|
Loading…
Add table
Add a link
Reference in a new issue