mirror of https://github.com/rwf2/Rocket.git
53 lines
1.1 KiB
Rust
53 lines
1.1 KiB
Rust
|
#![feature(plugin)]
|
||
|
#![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 rocket::Request;
|
||
|
use rocket::response::Redirect;
|
||
|
use rocket_contrib::Template;
|
||
|
|
||
|
#[derive(Serialize)]
|
||
|
struct TemplateContext {
|
||
|
name: String,
|
||
|
items: Vec<String>
|
||
|
}
|
||
|
|
||
|
#[get("/")]
|
||
|
fn index() -> Redirect {
|
||
|
Redirect::to("/hello/Unknown")
|
||
|
}
|
||
|
|
||
|
#[get("/hello/<name>")]
|
||
|
fn get(name: String) -> Template {
|
||
|
let context = TemplateContext {
|
||
|
name: name,
|
||
|
items: vec!["One".to_owned(), "Two".to_owned(), "Three".to_owned()]
|
||
|
};
|
||
|
|
||
|
Template::render("index", &context)
|
||
|
}
|
||
|
|
||
|
#[error(404)]
|
||
|
fn not_found(req: &Request) -> Template {
|
||
|
let mut map = std::collections::HashMap::new();
|
||
|
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(errors![not_found])
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
rocket().launch();
|
||
|
}
|