From 71e9776aa2e0c07ba8a5a6ccdeb5ef3ff5989e3b Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 23 Jul 2024 16:53:15 +0200 Subject: [PATCH] add raw post endpoint --- src/main.rs | 1 + src/routes/blog.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/main.rs b/src/main.rs index 3b8b0f2..5db24d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,6 +58,7 @@ async fn main() -> Result<()> { .service(blog::index) .service(blog::rss) .service(blog::page) + .service(blog::rawpage) .service(contrib::page) .service(cours::page) .service(cv::page) diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 252dc72..ea5ef2b 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -1,5 +1,6 @@ use std::{ collections::hash_map::DefaultHasher, + fs, hash::{Hash, Hasher}, }; @@ -197,6 +198,24 @@ pub async fn page(path: web::Path<(String,)>, config: web::Data) -> impl )) } +#[get("/blog/raw/{id}")] +pub async fn rawpage(path: web::Path<(String,)>, config: web::Data) -> impl Responder { + let filename = format!( + "{}/{BLOG_DIR}/{POST_DIR}/{}.md", + config.locations.data_dir, + path.into_inner().0 + ); + + match fs::read_to_string(filename) { + Ok(text) => HttpResponse::Ok() + .content_type(ContentType::plaintext()) + .body(text), + Err(_) => HttpResponse::NotFound() + .content_type(ContentType::plaintext()) + .body("post not found"), + } +} + fn build_post(file: &str, config: Config) -> String { let mut post = None; let (infos, toc) = get_post(