Port to Rocket 0.5.0-rc1
The rocket-contrib crate has been dropped by upstream. The serde and serde_json crates are only necessary for the tests.
This commit is contained in:
parent
e79940fe11
commit
c3ae8ca62a
File diff suppressed because it is too large
Load Diff
13
Cargo.toml
13
Cargo.toml
|
@ -14,13 +14,10 @@ edition = "2018"
|
|||
comrak = "0.7.0"
|
||||
glob = "0.3.0"
|
||||
Inflector = "*"
|
||||
rocket = "0.4.3"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_json = "1.0"
|
||||
rocket = { version = "0.5.0-rc.1", features = ["json"] }
|
||||
rocket_dyn_templates = { version = "0.1.0-rc.1", features = ["tera"] }
|
||||
toml = "0.5"
|
||||
|
||||
[dependencies.rocket_contrib]
|
||||
version = "0.4.3"
|
||||
default-features = false
|
||||
features = ["json", "tera_templates"]
|
||||
[dev-dependencies]
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
[development]
|
||||
[debug]
|
||||
address = "0.0.0.0"
|
||||
port = 3000
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
use super::super::Config;
|
||||
use super::super::NoteStore;
|
||||
use rocket::{get, State};
|
||||
use rocket_contrib::templates::Template;
|
||||
use serde_derive::Serialize;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rocket::serde::Serialize;
|
||||
use rocket::{get, State};
|
||||
use rocket_dyn_templates::Template;
|
||||
|
||||
use super::super::{Config, NoteStore};
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
struct IndexTemplateContext<'a> {
|
||||
app_version: &'a str,
|
||||
notes: Vec<HashMap<&'a str, &'a str>>,
|
||||
|
@ -13,7 +15,7 @@ struct IndexTemplateContext<'a> {
|
|||
}
|
||||
|
||||
#[get("/")]
|
||||
pub(crate) fn index(notes: State<NoteStore>, config: State<Config>) -> Template {
|
||||
pub(crate) async fn index(notes: &State<NoteStore>, config: &State<Config>) -> Template {
|
||||
let notes = notes.read().unwrap();
|
||||
let mut note_kvs = vec![];
|
||||
for note in notes.iter() {
|
||||
|
@ -34,17 +36,17 @@ pub(crate) fn index(notes: State<NoteStore>, config: State<Config>) -> Template
|
|||
mod tests {
|
||||
use rocket;
|
||||
use rocket::http::{Accept, Status};
|
||||
use rocket::local::Client;
|
||||
use rocket::local::blocking::Client;
|
||||
|
||||
#[test]
|
||||
fn index() {
|
||||
let client = Client::new(crate::rocket(Some("test"))).unwrap();
|
||||
let client = Client::untracked(crate::rocket(Some("test"))).unwrap();
|
||||
|
||||
// Try to get the index and verify the body
|
||||
let mut res = client.get("/").header(Accept::HTML).dispatch();
|
||||
let res = client.get("/").header(Accept::HTML).dispatch();
|
||||
assert_eq!(res.status(), Status::Ok);
|
||||
|
||||
let body = res.body_string().unwrap();
|
||||
let body = res.into_string().unwrap();
|
||||
println!("body: {}", body);
|
||||
assert!(body.contains("<span id=\"note-name\">Test</span>"));
|
||||
}
|
||||
|
|
|
@ -1,3 +1,2 @@
|
|||
pub mod home;
|
||||
pub mod note;
|
||||
pub mod static_files;
|
||||
|
|
|
@ -1,33 +1,34 @@
|
|||
use rocket::serde::json::Json;
|
||||
use rocket::{get, put, State};
|
||||
|
||||
use super::super::models::note::Note;
|
||||
use super::super::NoteStore;
|
||||
use rocket::{get, put, State};
|
||||
use rocket_contrib::json::Json;
|
||||
|
||||
#[get("/", format = "application/json")]
|
||||
pub(crate) fn index(notes: State<NoteStore>) -> Option<Json<Vec<Note>>> {
|
||||
pub(crate) async fn index(notes: &State<NoteStore>) -> Option<Json<Vec<Note>>> {
|
||||
let notes = notes.read().unwrap();
|
||||
Some(Json(notes.clone()))
|
||||
}
|
||||
|
||||
#[get("/<note_id>", format = "text/html")]
|
||||
pub(crate) fn show_html(note_id: String, notes: State<NoteStore>) -> Option<String> {
|
||||
pub(crate) async 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", rank = 2)]
|
||||
pub(crate) fn show_json(note_id: String, notes: State<NoteStore>) -> Option<Json<Note>> {
|
||||
pub(crate) async 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>")]
|
||||
pub(crate) fn update(
|
||||
pub(crate) async fn update(
|
||||
note_id: String,
|
||||
new_note: Json<Note>,
|
||||
notes: State<NoteStore>,
|
||||
notes: &State<NoteStore>,
|
||||
) -> Option<Json<Note>> {
|
||||
let mut notes = notes.write().unwrap();
|
||||
let note = notes.iter_mut().find(|note| note.id == note_id)?;
|
||||
|
@ -37,21 +38,22 @@ pub(crate) fn update(
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use rocket;
|
||||
use rocket::http::{Accept, ContentType, Status};
|
||||
use rocket::local::Client;
|
||||
use rocket::local::blocking::Client;
|
||||
use serde_json::{self, json};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn index() {
|
||||
let client = Client::new(crate::rocket(Some("test"))).unwrap();
|
||||
let client = Client::untracked(crate::rocket(Some("test"))).unwrap();
|
||||
|
||||
// Try to get all the notes
|
||||
let mut res = client.get("/notes").header(Accept::JSON).dispatch();
|
||||
let res = client.get("/notes").header(Accept::JSON).dispatch();
|
||||
assert_eq!(res.status(), Status::Ok);
|
||||
|
||||
let body = res.body_string().unwrap();
|
||||
let body = res.into_string().unwrap();
|
||||
let notes = serde_json::from_str::<Vec<Note>>(body.as_str()).unwrap();
|
||||
assert_eq!(notes[0].id, "test");
|
||||
assert_eq!(notes[0].index, 0);
|
||||
|
@ -70,13 +72,13 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn show_html() {
|
||||
let client = Client::new(crate::rocket(Some("test"))).unwrap();
|
||||
let client = Client::untracked(crate::rocket(Some("test"))).unwrap();
|
||||
|
||||
// Try to get the note and verify the body
|
||||
let mut res = client.get("/notes/test").header(Accept::HTML).dispatch();
|
||||
let res = client.get("/notes/test").header(Accept::HTML).dispatch();
|
||||
assert_eq!(res.status(), Status::Ok);
|
||||
|
||||
let body = res.body_string().unwrap();
|
||||
let body = res.into_string().unwrap();
|
||||
assert_eq!(
|
||||
body,
|
||||
r#"<p>This is a test note</p>
|
||||
|
@ -91,13 +93,13 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn show_json() {
|
||||
let client = Client::new(crate::rocket(Some("test"))).unwrap();
|
||||
let client = Client::untracked(crate::rocket(Some("test"))).unwrap();
|
||||
|
||||
// Try to get the note and verify the body
|
||||
let mut res = client.get("/notes/test").header(Accept::JSON).dispatch();
|
||||
let res = client.get("/notes/test").header(Accept::JSON).dispatch();
|
||||
assert_eq!(res.status(), Status::Ok);
|
||||
|
||||
let body = res.body_string().unwrap();
|
||||
let body = res.into_string().unwrap();
|
||||
let note = serde_json::from_str::<Note>(body.as_str()).unwrap();
|
||||
assert_eq!(note.id, "test");
|
||||
assert_eq!(note.index, 0);
|
||||
|
@ -118,14 +120,14 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn update() {
|
||||
let client = Client::new(crate::rocket(Some("test"))).unwrap();
|
||||
let client = Client::untracked(crate::rocket(Some("test"))).unwrap();
|
||||
|
||||
// Try to get the note and determine what to change it to
|
||||
let mut res = client
|
||||
let res = client
|
||||
.get("/notes/updatable")
|
||||
.header(Accept::JSON)
|
||||
.dispatch();
|
||||
let body = res.body_string().unwrap();
|
||||
let body = res.into_string().unwrap();
|
||||
let note = serde_json::from_str::<Note>(body.as_str()).unwrap();
|
||||
assert_eq!(note.data, "Some content");
|
||||
|
||||
|
@ -149,11 +151,11 @@ mod tests {
|
|||
.dispatch();
|
||||
assert_eq!(res.status(), Status::Ok);
|
||||
|
||||
let mut res = client
|
||||
let res = client
|
||||
.get("/notes/updatable")
|
||||
.header(Accept::JSON)
|
||||
.dispatch();
|
||||
let body = res.body_string().unwrap();
|
||||
let body = res.into_string().unwrap();
|
||||
let note = serde_json::from_str::<Note>(body.as_str()).unwrap();
|
||||
assert_eq!(note.data, new_data);
|
||||
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
use rocket::get;
|
||||
use rocket::response::NamedFile;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[get("/<path..>", rank = 5)]
|
||||
pub(crate) fn all(path: PathBuf) -> Option<NamedFile> {
|
||||
NamedFile::open(
|
||||
Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("static")
|
||||
.join(path),
|
||||
)
|
||||
.ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use rocket::http::Status;
|
||||
use rocket::local::Client;
|
||||
|
||||
#[test]
|
||||
fn all() {
|
||||
let client = Client::new(crate::rocket(Some("test"))).unwrap();
|
||||
|
||||
// Try to get the main JavaScript file
|
||||
let res = client.get("/js/pinboard.js").dispatch();
|
||||
assert_eq!(res.status(), Status::Ok);
|
||||
}
|
||||
}
|
29
src/main.rs
29
src/main.rs
|
@ -1,8 +1,9 @@
|
|||
#![feature(proc_macro_hygiene, decl_macro)]
|
||||
|
||||
use rocket::{routes, Rocket};
|
||||
use serde_derive::Deserialize;
|
||||
use std::sync::RwLock;
|
||||
|
||||
use rocket::fs::{relative, FileServer};
|
||||
use rocket::serde::Deserialize;
|
||||
use rocket::{routes, Build, Rocket};
|
||||
use rocket_dyn_templates::Template;
|
||||
use toml;
|
||||
|
||||
mod handlers;
|
||||
|
@ -11,11 +12,12 @@ mod models;
|
|||
type NoteStore = RwLock<Vec<models::note::Note>>;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
pub struct Config {
|
||||
title: String,
|
||||
}
|
||||
|
||||
fn rocket(notes_path: Option<&str>) -> Rocket {
|
||||
fn rocket(notes_path: Option<&str>) -> Rocket<Build> {
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::Path;
|
||||
|
@ -32,13 +34,11 @@ fn rocket(notes_path: Option<&str>) -> Rocket {
|
|||
|
||||
let notes = models::note::Note::load_all(notes_path);
|
||||
|
||||
rocket::ignite()
|
||||
rocket::build()
|
||||
.manage(RwLock::new(notes))
|
||||
.manage(config)
|
||||
.mount(
|
||||
"/",
|
||||
routes![handlers::home::index, handlers::static_files::all],
|
||||
)
|
||||
.mount("/", FileServer::from(relative!("static")))
|
||||
.mount("/", routes![handlers::home::index])
|
||||
.mount(
|
||||
"/notes",
|
||||
routes![
|
||||
|
@ -48,9 +48,12 @@ fn rocket(notes_path: Option<&str>) -> Rocket {
|
|||
handlers::note::update
|
||||
],
|
||||
)
|
||||
.attach(rocket_contrib::templates::Template::fairing())
|
||||
.attach(Template::fairing())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
rocket(None).launch();
|
||||
#[rocket::main]
|
||||
async fn main() -> Result<(), rocket::Error> {
|
||||
rocket(None).launch().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
use comrak;
|
||||
use glob::glob;
|
||||
use inflector::Inflector;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::PathBuf;
|
||||
use std::time::SystemTime;
|
||||
|
||||
use comrak;
|
||||
use glob::glob;
|
||||
use inflector::Inflector;
|
||||
use rocket::serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(crate = "rocket::serde")]
|
||||
/// Structure for representing a wish note
|
||||
pub struct Note {
|
||||
/// The ID of the note (unique string)
|
||||
|
|
Loading…
Reference in New Issue