mirror of https://github.com/rwf2/Rocket.git
77 lines
1.8 KiB
Rust
77 lines
1.8 KiB
Rust
#[macro_use] extern crate rocket;
|
|
|
|
#[cfg(test)] mod tests;
|
|
|
|
use rocket::Request;
|
|
use rocket::response::Redirect;
|
|
use rocket_contrib::templates::{Template, handlebars};
|
|
|
|
#[derive(serde::Serialize)]
|
|
struct TemplateContext {
|
|
title: &'static str,
|
|
name: Option<String>,
|
|
items: Vec<&'static str>,
|
|
// This key tells handlebars which template is the parent.
|
|
parent: &'static str,
|
|
}
|
|
|
|
#[get("/")]
|
|
fn index() -> Redirect {
|
|
Redirect::to("/hello/Unknown")
|
|
}
|
|
|
|
#[get("/hello/<name>")]
|
|
fn hello(name: String) -> Template {
|
|
Template::render("index", &TemplateContext {
|
|
title: "Hello",
|
|
name: Some(name),
|
|
items: vec!["One", "Two", "Three"],
|
|
parent: "layout",
|
|
})
|
|
}
|
|
|
|
#[get("/about")]
|
|
fn about() -> Template {
|
|
Template::render("about", &TemplateContext {
|
|
title: "About",
|
|
name: None,
|
|
items: vec!["Four", "Five", "Six"],
|
|
parent: "layout",
|
|
})
|
|
}
|
|
|
|
#[catch(404)]
|
|
fn not_found(req: &Request<'_>) -> Template {
|
|
let mut map = std::collections::HashMap::new();
|
|
map.insert("path", req.uri().path());
|
|
Template::render("error/404", &map)
|
|
}
|
|
|
|
use self::handlebars::{Helper, Handlebars, Context, RenderContext, Output, HelperResult, JsonRender};
|
|
|
|
fn wow_helper(
|
|
h: &Helper<'_, '_>,
|
|
_: &Handlebars,
|
|
_: &Context,
|
|
_: &mut RenderContext<'_, '_>,
|
|
out: &mut dyn Output
|
|
) -> HelperResult {
|
|
if let Some(param) = h.param(0) {
|
|
out.write("<b><i>")?;
|
|
out.write(¶m.value().render())?;
|
|
out.write("</b></i>")?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[launch]
|
|
fn rocket() -> rocket::Rocket {
|
|
rocket::ignite()
|
|
.mount("/", routes![index, hello, about])
|
|
.register(catchers![not_found])
|
|
.attach(Template::custom(|engines| {
|
|
engines.handlebars.register_helper("wow", Box::new(wow_helper));
|
|
}))
|
|
}
|