Split the handlers off into a handler submodules

Also move the list handlers to a separate mount point.
This commit is contained in:
Paul van Tilburg 2017-12-30 22:06:48 +01:00
parent 783a71488e
commit 38801c5625
6 changed files with 68 additions and 56 deletions

23
src/handlers/home.rs Normal file
View File

@ -0,0 +1,23 @@
use list::ListStore;
use 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<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)
}

31
src/handlers/list.rs Normal file
View File

@ -0,0 +1,31 @@
use list::{List, ListStore};
use rocket::State;
use rocket_contrib::Json;
#[get("/", format = "application/json")]
fn index(lists: State<ListStore>) -> Option<Json<Vec<List>>> {
let lists = lists.read().unwrap();
Some(Json(lists.clone()))
}
#[get("/<list_id>", format = "text/html")]
fn 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("/<list_id>", format = "application/json")]
fn 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("/<list_id>", format = "application/json", data = "<new_list>")]
fn 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()))
}

3
src/handlers/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod home;
pub mod list;
pub mod static_files;

View File

@ -4,6 +4,7 @@ use inflector::Inflector;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use std::sync::RwLock;
use std::time::SystemTime;
/// Converts raw string data (in Markdown format) to HTML
@ -83,3 +84,5 @@ impl List {
lists
}
}
pub type ListStore = RwLock<Vec<List>>;

View File

@ -10,69 +10,21 @@ extern crate rocket_contrib;
extern crate serde_json;
mod list;
mod static_files;
mod handlers;
use list::List;
use rocket::{Rocket, State};
use rocket_contrib::{Json, Template};
use std::collections::HashMap;
use rocket::Rocket;
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 list_index(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, list_index, list_show_html, list_show_json, list_update, static_files::all])
.attach(Template::fairing())
.mount("/", routes![handlers::home::index,
handlers::static_files::all])
.mount("/lists", routes![handlers::list::index,
handlers::list::show_html, handlers::list::show_json,
handlers::list::update])
.attach(rocket_contrib::Template::fairing())
}
fn main() {