pinkblackrose/src/main.rs

74 lines
1.7 KiB
Rust

#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[macro_use] extern crate serde_derive;
use rocket::Request;
use rocket::response::Redirect;
use rocket_contrib::templates::{Template, handlebars};
#[derive(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",
})
}
#[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(&param.value().render())?;
out.write("</b></i>")?;
}
Ok(())
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/", routes![index, hello])
.register(catchers![not_found])
.attach(Template::custom(|engines| {
engines.handlebars.register_helper("wow", Box::new(wow_helper));
}))
}
fn main() {
rocket().launch();
}