Add handlers for retrieving the list in JSON and rendered HTML format
This commit is contained in:
parent
23af7ab2be
commit
950e031c35
16
src/main.rs
16
src/main.rs
|
@ -14,7 +14,7 @@ mod static_files;
|
|||
|
||||
use list::List;
|
||||
use rocket::{Rocket, State};
|
||||
use rocket_contrib::Template;
|
||||
use rocket_contrib::{Json, Template};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Serialize)]
|
||||
|
@ -35,11 +35,23 @@ fn index(lists: State<Vec<List>>) -> Template {
|
|||
Template::render("index", &context)
|
||||
}
|
||||
|
||||
#[get("/lists/<list_id>", format = "text/html")]
|
||||
fn list_show_html(list_id: String, lists: State<Vec<List>>) -> Option<String> {
|
||||
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<Vec<List>>) -> Option<Json<List>> {
|
||||
let list = lists.iter().find( |&list| list.id == list_id )?;
|
||||
Some(Json(list.clone()))
|
||||
}
|
||||
|
||||
fn rocket() -> Rocket {
|
||||
let lists = list::List::load_all();
|
||||
rocket::ignite()
|
||||
.manage(lists)
|
||||
.mount("/", routes![index, static_files::all])
|
||||
.mount("/", routes![index, list_show_html, list_show_json, static_files::all])
|
||||
.attach(Template::fairing())
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue