Paul van Tilburg
09db743a9e
* The root location now shows all lists * The specific list locations show only the specified list * Each list can be individually edited * Use the card columns (masonry) layout when showing all lists * No longer support HTML previewing, just switch between editing and viewing (ala Github comments)
80 lines
2.3 KiB
Rust
80 lines
2.3 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::{Json, Template};
|
|
use std::collections::HashMap;
|
|
use std::sync::RwLock;
|
|
|
|
type ListStore = RwLock<Vec<List>>;
|
|
|
|
#[derive(Serialize)]
|
|
struct IndexTemplateContext<'a> {
|
|
lists: Vec<HashMap<&'a str, &'a str>>
|
|
}
|
|
|
|
#[get("/")]
|
|
fn index(lists: State<ListStore>) -> Template {
|
|
let lists = lists.read().unwrap();
|
|
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)
|
|
}
|
|
|
|
#[get("/lists", format = "application/json")]
|
|
fn lists(lists: State<ListStore>) -> Option<Json<Vec<List>>> {
|
|
let lists = lists.read().unwrap();
|
|
Some(Json(lists.clone()))
|
|
}
|
|
|
|
#[get("/lists/<list_id>", format = "text/html")]
|
|
fn list_show_html(list_id: String, lists: State<ListStore>) -> Option<String> {
|
|
let lists = lists.read().unwrap();
|
|
let list = lists.iter().find( |list| list.id == list_id )?;
|
|
Some(list.to_html())
|
|
}
|
|
|
|
#[get("/lists/<list_id>", format = "application/json")]
|
|
fn list_show_json(list_id: String, lists: State<ListStore>) -> Option<Json<List>> {
|
|
let lists = lists.read().unwrap();
|
|
let list = lists.iter().find( |list| list.id == list_id )?;
|
|
Some(Json(list.clone()))
|
|
}
|
|
|
|
#[put("/lists/<list_id>", format = "application/json", data = "<new_list>")]
|
|
fn list_update(list_id: String, new_list: Json<List>, lists: State<ListStore>) -> Option<Json<List>> {
|
|
let mut lists = lists.write().unwrap();
|
|
let list = lists.iter_mut().find( |list| list.id == list_id )?;
|
|
list.update_data(&new_list.data);
|
|
Some(Json(list.clone()))
|
|
}
|
|
|
|
fn rocket() -> Rocket {
|
|
let lists = list::List::load_all();
|
|
rocket::ignite()
|
|
.manage(RwLock::new(lists))
|
|
.mount("/", routes![index, lists, list_show_html, list_show_json, list_update, static_files::all])
|
|
.attach(Template::fairing())
|
|
}
|
|
|
|
fn main() {
|
|
rocket().launch();
|
|
}
|