rocket-pinboard/src/main.rs
Paul van Tilburg 23af7ab2be Complete rework the index view
* Manage the vector of loads lists in the state
* Use a struct as the context for the index template
* Rework the _layout and index templates; remove hardcoded examples
2017-12-23 22:20:57 +01:00

49 lines
1.1 KiB
Rust

#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate comrak;
extern crate glob;
extern crate inflector;
extern crate rocket;
extern crate rocket_contrib;
#[macro_use] extern crate serde_derive;
extern crate serde_json;
mod list;
mod static_files;
use list::List;
use rocket::{Rocket, State};
use rocket_contrib::Template;
use std::collections::HashMap;
#[derive(Serialize)]
struct IndexTemplateContext<'a> {
lists: Vec<HashMap<&'a str, &'a str>>
}
#[get("/")]
fn index(lists: State<Vec<List>>) -> Template {
let mut list_kvs = vec![];
for list in lists.iter() {
let mut list_kv = HashMap::new();
list_kv.insert("id", list.id.as_ref());
list_kv.insert("name", list.name.as_ref());
list_kvs.push(list_kv);
}
let context = IndexTemplateContext { lists: list_kvs };
Template::render("index", &context)
}
fn rocket() -> Rocket {
let lists = list::List::load_all();
rocket::ignite()
.manage(lists)
.mount("/", routes![index, static_files::all])
.attach(Template::fairing())
}
fn main() {
rocket().launch();
}