Rocket - Blog with FromParam - selectively accept pathes



examples/rocket/blog-with-fromparam/src/main.rs
#[macro_use]
extern crate rocket;

#[cfg(test)]
mod tests;

use rocket::fs::relative;
use rocket::response::content;

use rocket::request::FromParam;
#[derive(Debug)]
struct MyPath {
    filepath: std::path::PathBuf,
}

impl<'r> FromParam<'r> for MyPath {
    type Error = &'r str;

    fn from_param(param: &'r str) -> Result<Self, Self::Error> {
        rocket::info!("from_param: {:?}", param);
        let mut filepath = std::path::PathBuf::from(relative!("pages")).join(param);
        filepath.set_extension("md");
        rocket::info!("filepath: {:?}", filepath);

        if filepath.exists() {
            Ok(Self { filepath })
        } else {
            Err("bad")
        }
    }
}

#[get("/")]
fn index() -> content::RawHtml<String> {
    let html = String::from(
        r#"
    <a href="/blog/main">main</a><br>
    <a href="/blog/missing">missing</a><br>
    "#,
    );
    content::RawHtml(html)
}

#[get("/blog/<slug>")]
fn blog(slug: MyPath) -> content::RawHtml<String> {
    rocket::info!("slug: {:?}", slug);

    let content = std::fs::read_to_string(slug.filepath).unwrap();

    let html = format!("content: {}", content);

    content::RawHtml(html)
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![index, blog])
}

examples/rocket/blog-with-fromparam/src/tests.rs
use rocket::http::Status;
use rocket::local::blocking::Client;

#[test]
fn check_index() {
    let client = Client::tracked(super::rocket()).unwrap();
    let response = client.get("/").dispatch();

    assert_eq!(response.status(), Status::Ok);
    assert_eq!(
        response.headers().get_one("Content-Type").unwrap(),
        "text/html; charset=utf-8"
    );
    let html = response.into_string().unwrap();
    assert!(html.contains(r#"<a href="/blog/main">main</a><br>"#));
    assert!(html.contains(r#"<a href="/blog/missing">missing</a><br>"#));
}

#[test]
fn check_main() {
    let client = Client::tracked(super::rocket()).unwrap();
    let response = client.get("/blog/main").dispatch();

    assert_eq!(response.status(), Status::Ok);
    assert_eq!(
        response.headers().get_one("Content-Type").unwrap(),
        "text/html; charset=utf-8"
    );
    let html = response.into_string().unwrap();
    //println!("{html}");
    assert!(html.contains(r#"content: Main page"#));
}

#[test]
fn check_about() {
    let client = Client::tracked(super::rocket()).unwrap();
    let response = client.get("/blog/about").dispatch();

    assert_eq!(response.status(), Status::Ok);
    assert_eq!(
        response.headers().get_one("Content-Type").unwrap(),
        "text/html; charset=utf-8"
    );
    let html = response.into_string().unwrap();
    assert!(html.contains(r#"content: About page"#));
}

#[test]
fn check_missing_page() {
    let client = Client::tracked(super::rocket()).unwrap();
    let response = client.get("/blog/other").dispatch();

    assert_eq!(response.status(), Status::UnprocessableEntity);
    assert_eq!(
        response.headers().get_one("Content-Type").unwrap(),
        "text/html; charset=utf-8"
    );
    let html = response.into_string().unwrap();
    println!("{html}");
    assert!(html.contains(r#"<h1>422: Unprocessable Entity</h1>"#));
}

examples/rocket/blog-with-fromparam/pages/about.md
About page

examples/rocket/blog-with-fromparam/pages/main.md
Main page