Rocket/examples/tera_templates/src/main.rs

51 lines
1.0 KiB
Rust
Raw Normal View History

#![feature(plugin, decl_macro)]
2017-05-16 22:14:46 +00:00
#![plugin(rocket_codegen)]
extern crate rocket_contrib;
extern crate rocket;
extern crate serde_json;
#[macro_use] extern crate serde_derive;
#[cfg(test)] mod tests;
use std::collections::HashMap;
2017-05-16 22:14:46 +00:00
use rocket::Request;
use rocket::response::Redirect;
use rocket_contrib::Template;
#[derive(Serialize)]
struct TemplateContext {
name: String,
items: Vec<&'static str>
2017-05-16 22:14:46 +00:00
}
#[get("/")]
fn index() -> Redirect {
2018-04-07 20:51:57 +00:00
Redirect::to(uri!(get: name = "Unknown"))
2017-05-16 22:14:46 +00:00
}
#[get("/hello/<name>")]
fn get(name: String) -> Template {
let context = TemplateContext { name, items: vec!["One", "Two", "Three"] };
2017-05-16 22:14:46 +00:00
Template::render("index", &context)
}
#[catch(404)]
2017-05-16 22:14:46 +00:00
fn not_found(req: &Request) -> Template {
let mut map = HashMap::new();
2017-05-16 22:14:46 +00:00
map.insert("path", req.uri().as_str());
Template::render("error/404", &map)
}
fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/", routes![index, get])
.attach(Template::fairing())
.catch(catchers![not_found])
2017-05-16 22:14:46 +00:00
}
fn main() {
rocket().launch();
}