Upgrade to Rocket 0.4.0

This commit is contained in:
Paul van Tilburg 2018-12-18 15:47:34 +01:00
parent 900ace01d3
commit bc18307da9
5 changed files with 777 additions and 204 deletions

944
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -13,14 +13,13 @@ license = "MIT"
comrak = "0.2.5"
glob = "0.2.11"
Inflector = "*"
rocket = "0.3.6"
rocket_codegen = "0.3.6"
rocket = "0.4.0"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
toml = "0.4"
[dependencies.rocket_contrib]
version = "*"
version = "0.4.0"
default-features = false
features = ["json", "tera_templates"]
features = ["json", "tera_templates"]

View File

@ -1,8 +1,8 @@
use rocket::State;
use rocket_contrib::Template;
use std::collections::HashMap;
use super::super::Config;
use super::super::NoteStore;
use rocket::State;
use rocket_contrib::templates::Template;
use std::collections::HashMap;
#[derive(Serialize)]
struct IndexTemplateContext<'a> {
@ -12,7 +12,7 @@ struct IndexTemplateContext<'a> {
}
#[get("/")]
fn index(notes: State<NoteStore>, config: State<Config>) -> Template {
pub fn index(notes: State<NoteStore>, config: State<Config>) -> Template {
let notes = notes.read().unwrap();
let mut note_kvs = vec![];
for note in notes.iter() {

View File

@ -4,27 +4,31 @@ use super::super::NoteStore;
use super::super::models::note::Note;
#[get("/", format = "application/json")]
fn index(notes: State<NoteStore>) -> Option<Json<Vec<Note>>> {
pub fn index(notes: State<NoteStore>) -> Option<Json<Vec<Note>>> {
let notes = notes.read().unwrap();
Some(Json(notes.clone()))
}
#[get("/<note_id>", format = "text/html")]
fn show_html(note_id: String, notes: State<NoteStore>) -> Option<String> {
pub fn show_html(note_id: String, notes: State<NoteStore>) -> Option<String> {
let notes = notes.read().unwrap();
let note = notes.iter().find(|note| note.id == note_id)?;
Some(note.to_html())
}
#[get("/<note_id>", format = "application/json")]
fn show_json(note_id: String, notes: State<NoteStore>) -> Option<Json<Note>> {
#[get("/<note_id>", format = "application/json", rank = 2)]
pub fn show_json(note_id: String, notes: State<NoteStore>) -> Option<Json<Note>> {
let notes = notes.read().unwrap();
let note = notes.iter().find(|note| note.id == note_id)?;
Some(Json(note.clone()))
}
#[put("/<note_id>", format = "application/json", data = "<new_note>")]
fn update(note_id: String, new_note: Json<Note>, notes: State<NoteStore>) -> Option<Json<Note>> {
pub fn update(
note_id: String,
new_note: Json<Note>,
notes: State<NoteStore>,
) -> Option<Json<Note>> {
let mut notes = notes.write().unwrap();
let note = notes.iter_mut().find(|note| note.id == note_id)?;
note.update_data(&new_note.data);

View File

@ -1,9 +1,9 @@
#![feature(plugin)]
#![plugin(rocket_codegen)]
#![feature(proc_macro_hygiene, decl_macro)]
extern crate comrak;
extern crate glob;
extern crate inflector;
#[macro_use]
extern crate rocket;
extern crate rocket_contrib;
#[macro_use]
@ -23,7 +23,7 @@ use std::sync::RwLock;
type NoteStore = RwLock<Vec<models::note::Note>>;
#[derive(Debug, Deserialize)]
struct Config {
pub struct Config {
title: String,
}
@ -54,7 +54,7 @@ fn rocket(notes_path: Option<&str>) -> Rocket {
handlers::note::update
],
)
.attach(rocket_contrib::Template::fairing())
.attach(rocket_contrib::templates::Template::fairing())
}
fn main() {