2016-10-09 02:27:33 +00:00
|
|
|
#![feature(plugin, proc_macro)]
|
2016-10-04 00:56:43 +00:00
|
|
|
#![plugin(rocket_codegen)]
|
2016-09-19 23:24:01 +00:00
|
|
|
|
|
|
|
extern crate rocket;
|
|
|
|
extern crate serde_json;
|
2016-10-04 00:56:43 +00:00
|
|
|
#[macro_use] extern crate lazy_static;
|
|
|
|
#[macro_use] extern crate rocket_contrib;
|
|
|
|
#[macro_use] extern crate serde_derive;
|
2016-09-19 23:24:01 +00:00
|
|
|
|
|
|
|
use rocket_contrib::JSON;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
|
|
// The type to represent the ID of a message.
|
|
|
|
type ID = usize;
|
|
|
|
type SimpleMap = HashMap<&'static str, &'static str>;
|
|
|
|
|
|
|
|
// We're going to store all of the messages here. No need for a DB.
|
|
|
|
lazy_static! {
|
|
|
|
static ref MAP: Mutex<HashMap<ID, String>> = Mutex::new(HashMap::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct Message {
|
|
|
|
id: Option<ID>,
|
|
|
|
contents: String
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This example can be improved by using `route` with muliple HTTP verbs.
|
|
|
|
// To be precise, put/post could/should look like:
|
|
|
|
// #[route(PUT, POST, path = "/<id>", format = "application/json")]
|
|
|
|
// fn f(method: Method, id: ID, message: JSON<Message>) -> Option<JSON<SimpleMap>> {
|
|
|
|
// let mut hashmap = MAP.lock().unwrap();
|
|
|
|
// let exists = hashmap.contains_key(&id);
|
|
|
|
// if method == Method::Put && exists || method == Method::Post && !exists {
|
|
|
|
// hashmap.insert(id, message.0.contents);
|
|
|
|
// return Ok(JSON(map!{ "status" => "ok" }))
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// None
|
|
|
|
// }
|
|
|
|
|
|
|
|
#[post("/<id>", format = "application/json")]
|
|
|
|
fn new(id: ID, message: JSON<Message>) -> JSON<SimpleMap> {
|
|
|
|
let mut hashmap = MAP.lock().unwrap();
|
|
|
|
if hashmap.contains_key(&id) {
|
|
|
|
JSON(map!{
|
|
|
|
"status" => "error",
|
|
|
|
"reason" => "ID exists. Try put."
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
hashmap.insert(id, message.0.contents);
|
|
|
|
JSON(map!{ "status" => "ok" })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[put("/<id>", format = "application/json")]
|
|
|
|
fn update(id: ID, message: JSON<Message>) -> Option<JSON<SimpleMap>> {
|
|
|
|
let mut hashmap = MAP.lock().unwrap();
|
|
|
|
if hashmap.contains_key(&id) {
|
|
|
|
hashmap.insert(id, message.0.contents);
|
|
|
|
Some(JSON(map!{ "status" => "ok" }))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/<id>", format = "application/json")]
|
|
|
|
fn get(id: ID) -> Option<JSON<Message>> {
|
|
|
|
let hashmap = MAP.lock().unwrap();
|
|
|
|
hashmap.get(&id).map(|contents| {
|
|
|
|
JSON(Message {
|
|
|
|
id: Some(id),
|
|
|
|
contents: contents.clone()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[error(404)]
|
2016-10-07 03:38:13 +00:00
|
|
|
fn not_found() -> JSON<SimpleMap> {
|
2016-09-19 23:24:01 +00:00
|
|
|
JSON(map! {
|
|
|
|
"status" => "error",
|
|
|
|
"reason" => "Resource was not found."
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2016-10-04 02:48:33 +00:00
|
|
|
rocket::ignite()
|
|
|
|
.mount("/message", routes![new, update, get])
|
|
|
|
.catch(errors![not_found])
|
|
|
|
.launch();
|
2016-09-19 23:24:01 +00:00
|
|
|
}
|