From bf0335f4debbedd61670fe31b5d6edbf60d176a3 Mon Sep 17 00:00:00 2001 From: ingalless Date: Tue, 30 Apr 2024 15:45:17 +0100 Subject: [PATCH] Use fileserver (#12) --- src/compiler.rs | 23 +++++++++++++++++++---- src/main.rs | 48 ++++++++++++++---------------------------------- src/views.rs | 4 ++-- 3 files changed, 35 insertions(+), 40 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index eab8692..f586872 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -77,8 +77,14 @@ impl Compiler { } } - let target_path = Path::new(&self.path).join(format!("{}.html", recipe.title)); - let write_result = std::fs::write(&target_path, views::recipe(&recipe).into_string()); + let target_path = Path::new(&self.path).join("recipes").join(&recipe.title); + let write_result = match std::fs::create_dir_all(&target_path) { + Ok(_) => std::fs::write( + &target_path.join("index.html"), + views::recipe(&recipe).into_string(), + ), + Err(_) => return Err(format!("Failed to write {}", recipe.title)), + }; match write_result { Ok(_) => println!("Wrote {}", target_path.to_str().unwrap()), Err(_) => { @@ -87,8 +93,17 @@ impl Compiler { } }; } - let tag_target_path = Path::new(&self.path).join("tags.html"); - let write_result = std::fs::write(&tag_target_path, views::tags(&tag_map).into_string()); + let tag_target_path = Path::new(&self.path).join("tags"); + let write_result = match std::fs::create_dir_all(&tag_target_path) { + Ok(_) => std::fs::write( + &tag_target_path.join("index.html"), + views::tags(&tag_map).into_string(), + ), + Err(_) => { + println!("Failed to write tags page"); + return Err("Failed to write tags page".into()); + } + }; match write_result { Ok(_) => println!("Wrote {}", tag_target_path.to_str().unwrap()), Err(_) => { diff --git a/src/main.rs b/src/main.rs index a18c808..27f8591 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,9 @@ -use std::{ - fs, - path::{Path, PathBuf}, -}; +use std::{fs, path::PathBuf}; use compiler::Compiler; use comrak::{markdown_to_html, Options}; use maud::Markup; -use rocket::http::{ContentType, Status}; +use rocket::{fs::FileServer, http::Status}; #[macro_use] extern crate rocket; @@ -31,23 +28,6 @@ impl Recipe { } } -#[get("/tags")] -fn tags() -> Result<(Status, (ContentType, String)), Status> { - let compiled_path = std::env::var("APP_COMPILED_PATH").unwrap_or("./compiled".into()); - let path = Path::new(&compiled_path).join("tags.html"); - let content = fs::read_to_string(path).unwrap_or(String::from("")); - Ok((Status::Ok, (ContentType::HTML, content))) -} - -#[get("/recipe/")] -fn read_recipe(recipe: &str) -> Result<(Status, (ContentType, String)), Status> { - let compiled_path = std::env::var("APP_COMPILED_PATH").unwrap_or("./compiled".into()); - let recipe_file = format!("{}.html", recipe); - let path = Path::new(&compiled_path).join(recipe_file); - let content = fs::read_to_string(path).unwrap_or(String::from("")); - Ok((Status::Ok, (ContentType::HTML, content))) -} - fn get_recipes(path: String) -> Result, String> { let dir = fs::read_dir(path); let entries = dir @@ -84,7 +64,7 @@ fn index() -> Result { fn rocket() -> _ { let recipes_path = std::env::var("APP_RECIPES_PATH").unwrap_or("./recipes".into()); let compiled_path = std::env::var("APP_COMPILED_PATH").unwrap_or("./compiled".into()); - let compiler = Compiler::new(compiled_path); + let compiler = Compiler::new(compiled_path.clone()); match get_recipes(recipes_path) { Ok(recipes) => compiler @@ -94,7 +74,9 @@ fn rocket() -> _ { println!("{}", e); } } - rocket::build().mount("/", routes![index, read_recipe, tags]) + rocket::build() + .mount("/", routes![index]) + .mount("/", FileServer::from(compiled_path)) } #[cfg(test)] @@ -124,21 +106,15 @@ mod test { let client = Client::tracked(rocket()).expect("valid rocket instance"); - let expected_compiled_path = tmp_compiled_dir.path().join("soy-salmon.html"); - let response = client.get("/").dispatch(); assert_eq!(response.status(), Status::Ok); let content = response.into_string(); assert_eq!(content.contains("No Nonsense Recipes"), true); assert_eq!( - content.contains("soy-salmon"), + content.contains("soy-salmon"), true ); - let compiled = - fs::read_to_string(expected_compiled_path).expect("Compiled file not created"); - assert_eq!(compiled.contains("Honey Soy Salmon"), true); - tmp_recipe_dir.close()?; tmp_compiled_dir.close()?; Ok(()) @@ -154,12 +130,16 @@ mod test { let client = Client::tracked(rocket()).expect("valid rocket instance"); - let expected_compiled_path = tmp_compiled_dir.path().join("soy-salmon.html"); + let expected_compiled_path = tmp_compiled_dir + .path() + .join("recipes") + .join("soy-salmon") + .join("index.html"); - let response = client.get("/recipe/soy-salmon").dispatch(); + let response = client.get("/recipes/soy-salmon").dispatch(); assert_eq!(response.status(), Status::Ok); let content = response.into_string(); - assert_eq!(content.contains("soy-salmon"), true); + assert_eq!(content.contains("Honey Soy Salmon"), true); let compiled = fs::read_to_string(expected_compiled_path).expect("Compiled file not created"); diff --git a/src/views.rs b/src/views.rs index a4a06eb..5ff7747 100644 --- a/src/views.rs +++ b/src/views.rs @@ -36,7 +36,7 @@ pub fn index(recipes: Vec) -> Markup { p { em { "Print friendly too!" } } ul { @for recipe in recipes { - li { a href=(format!("/recipe/{}", recipe.title)) { (recipe.title) } } + li { a href=(format!("/recipes/{}", recipe.title)) { (recipe.title) } } } } } @@ -50,7 +50,7 @@ pub fn tags(tags: &HashMap>) -> Markup { h2 { (tag) } ul { @for recipe in recipes { - li { a class="text-blue-500" href=(format!("/recipe/{}", recipe)) { (recipe) } } + li { a class="text-blue-500" href=(format!("/recipes/{}", recipe)) { (recipe) } } } } }