2017-12-06 15:29:10 +01:00
|
|
|
#![feature(plugin)]
|
|
|
|
#![plugin(rocket_codegen)]
|
|
|
|
|
2017-12-17 13:19:27 +01:00
|
|
|
extern crate comrak;
|
|
|
|
extern crate glob;
|
|
|
|
extern crate inflector;
|
2017-12-06 15:29:10 +01:00
|
|
|
extern crate rocket;
|
2017-12-06 16:47:55 +01:00
|
|
|
extern crate rocket_contrib;
|
2017-12-23 22:20:57 +01:00
|
|
|
#[macro_use] extern crate serde_derive;
|
|
|
|
extern crate serde_json;
|
2017-12-06 15:29:10 +01:00
|
|
|
|
2017-12-23 22:20:57 +01:00
|
|
|
mod list;
|
|
|
|
mod static_files;
|
2017-12-06 15:38:07 +01:00
|
|
|
|
2017-12-23 22:20:57 +01:00
|
|
|
use list::List;
|
|
|
|
use rocket::{Rocket, State};
|
2017-12-23 22:36:03 +01:00
|
|
|
use rocket_contrib::{Json, Template};
|
2017-12-17 13:22:01 +01:00
|
|
|
use std::collections::HashMap;
|
2017-12-27 17:33:28 +01:00
|
|
|
use std::sync::RwLock;
|
|
|
|
|
|
|
|
type ListStore = RwLock<Vec<List>>;
|
2017-12-17 13:22:01 +01:00
|
|
|
|
2017-12-23 22:20:57 +01:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct IndexTemplateContext<'a> {
|
|
|
|
lists: Vec<HashMap<&'a str, &'a str>>
|
|
|
|
}
|
2017-12-06 15:34:56 +01:00
|
|
|
|
2017-12-06 15:29:10 +01:00
|
|
|
#[get("/")]
|
2017-12-27 17:33:28 +01:00
|
|
|
fn index(lists: State<ListStore>) -> Template {
|
|
|
|
let lists = lists.read().unwrap();
|
2017-12-23 22:20:57 +01:00
|
|
|
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 };
|
2017-12-17 13:22:01 +01:00
|
|
|
Template::render("index", &context)
|
2017-12-06 15:29:10 +01:00
|
|
|
}
|
|
|
|
|
2017-12-23 22:36:03 +01:00
|
|
|
#[get("/lists/<list_id>", format = "text/html")]
|
2017-12-27 17:33:28 +01:00
|
|
|
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 )?;
|
2017-12-23 22:36:03 +01:00
|
|
|
Some(list.to_html())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/lists/<list_id>", format = "application/json")]
|
2017-12-27 17:33:28 +01:00
|
|
|
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);
|
|
|
|
println!("update list {} with list data {:?}", list_id, new_list.data);
|
2017-12-23 22:36:03 +01:00
|
|
|
Some(Json(list.clone()))
|
|
|
|
}
|
|
|
|
|
2017-12-06 15:38:07 +01:00
|
|
|
fn rocket() -> Rocket {
|
2017-12-17 13:19:27 +01:00
|
|
|
let lists = list::List::load_all();
|
2017-12-06 15:29:10 +01:00
|
|
|
rocket::ignite()
|
2017-12-27 17:33:28 +01:00
|
|
|
.manage(RwLock::new(lists))
|
|
|
|
.mount("/", routes![index, list_show_html, list_show_json, list_update, static_files::all])
|
2017-12-06 16:47:55 +01:00
|
|
|
.attach(Template::fairing())
|
2017-12-06 15:38:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
rocket().launch();
|
2017-12-06 15:29:10 +01:00
|
|
|
}
|