Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Use fileserver #12

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {
Expand All @@ -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(_) => {
Expand Down
48 changes: 14 additions & 34 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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/<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<Vec<Recipe>, String> {
let dir = fs::read_dir(path);
let entries = dir
Expand Down Expand Up @@ -84,7 +64,7 @@ fn index() -> Result<Markup, Status> {
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
Expand All @@ -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)]
Expand Down Expand Up @@ -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("<a href=\"/recipe/soy-salmon\">soy-salmon</a>"),
content.contains("<a href=\"/recipes/soy-salmon\">soy-salmon</a>"),
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(())
Expand All @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn index(recipes: Vec<Recipe>) -> 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) } }
}
}
}
Expand All @@ -50,7 +50,7 @@ pub fn tags(tags: &HashMap<String, Vec<String>>) -> 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) } }
}
}
}
Expand Down
Loading