2017-01-08 04:59:35 +00:00
|
|
|
#![feature(plugin)]
|
2016-10-04 00:56:43 +00:00
|
|
|
#![plugin(rocket_codegen)]
|
2016-09-19 23:24:01 +00:00
|
|
|
|
|
|
|
extern crate rocket;
|
2016-10-04 00:56:43 +00:00
|
|
|
#[macro_use] extern crate rocket_contrib;
|
|
|
|
#[macro_use] extern crate serde_derive;
|
2016-09-19 23:24:01 +00:00
|
|
|
|
2016-12-22 06:56:58 +00:00
|
|
|
#[cfg(test)] mod tests;
|
|
|
|
|
2017-08-26 06:14:42 +00:00
|
|
|
use rocket_contrib::{Json, JsonValue};
|
2017-02-13 10:28:31 +00:00
|
|
|
use rocket::State;
|
2016-09-19 23:24:01 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
|
|
// The type to represent the ID of a message.
|
|
|
|
type ID = usize;
|
2016-12-29 03:01:16 +00:00
|
|
|
|
2016-09-19 23:24:01 +00:00
|
|
|
// We're going to store all of the messages here. No need for a DB.
|
2017-02-13 10:28:31 +00:00
|
|
|
type MessageMap = Mutex<HashMap<ID, String>>;
|
2016-09-19 23:24:01 +00:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct Message {
|
|
|
|
id: Option<ID>,
|
|
|
|
contents: String
|
|
|
|
}
|
|
|
|
|
2017-02-17 02:56:44 +00:00
|
|
|
// TODO: This example can be improved by using `route` with multiple HTTP verbs.
|
2016-10-12 07:14:42 +00:00
|
|
|
#[post("/<id>", format = "application/json", data = "<message>")]
|
2017-08-26 06:14:42 +00:00
|
|
|
fn new(id: ID, message: Json<Message>, map: State<MessageMap>) -> JsonValue {
|
2017-02-13 10:28:31 +00:00
|
|
|
let mut hashmap = map.lock().expect("map lock.");
|
2016-09-19 23:24:01 +00:00
|
|
|
if hashmap.contains_key(&id) {
|
2017-08-26 06:14:42 +00:00
|
|
|
json!({
|
2017-02-01 01:15:42 +00:00
|
|
|
"status": "error",
|
|
|
|
"reason": "ID exists. Try put."
|
2017-08-26 06:14:42 +00:00
|
|
|
})
|
2016-09-19 23:24:01 +00:00
|
|
|
} else {
|
|
|
|
hashmap.insert(id, message.0.contents);
|
2017-08-26 06:14:42 +00:00
|
|
|
json!({ "status": "ok" })
|
2016-09-19 23:24:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 07:14:42 +00:00
|
|
|
#[put("/<id>", format = "application/json", data = "<message>")]
|
2017-08-26 06:14:42 +00:00
|
|
|
fn update(id: ID, message: Json<Message>, map: State<MessageMap>) -> Option<JsonValue> {
|
2017-02-13 10:28:31 +00:00
|
|
|
let mut hashmap = map.lock().unwrap();
|
2016-09-19 23:24:01 +00:00
|
|
|
if hashmap.contains_key(&id) {
|
|
|
|
hashmap.insert(id, message.0.contents);
|
2017-08-26 06:14:42 +00:00
|
|
|
Some(json!({ "status": "ok" }))
|
2016-09-19 23:24:01 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/<id>", format = "application/json")]
|
2017-07-12 22:11:41 +00:00
|
|
|
fn get(id: ID, map: State<MessageMap>) -> Option<Json<Message>> {
|
2017-02-13 10:28:31 +00:00
|
|
|
let hashmap = map.lock().unwrap();
|
2016-09-19 23:24:01 +00:00
|
|
|
hashmap.get(&id).map(|contents| {
|
2017-07-12 22:11:41 +00:00
|
|
|
Json(Message {
|
2016-09-19 23:24:01 +00:00
|
|
|
id: Some(id),
|
|
|
|
contents: contents.clone()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[error(404)]
|
2017-08-26 06:14:42 +00:00
|
|
|
fn not_found() -> JsonValue {
|
|
|
|
json!({
|
2017-02-01 01:15:42 +00:00
|
|
|
"status": "error",
|
|
|
|
"reason": "Resource was not found."
|
2017-08-26 06:14:42 +00:00
|
|
|
})
|
2016-09-19 23:24:01 +00:00
|
|
|
}
|
|
|
|
|
2017-02-13 10:28:31 +00:00
|
|
|
fn rocket() -> rocket::Rocket {
|
2016-10-04 02:48:33 +00:00
|
|
|
rocket::ignite()
|
|
|
|
.mount("/message", routes![new, update, get])
|
|
|
|
.catch(errors![not_found])
|
2017-02-13 10:28:31 +00:00
|
|
|
.manage(Mutex::new(HashMap::<ID, String>::new()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
rocket().launch();
|
2016-09-19 23:24:01 +00:00
|
|
|
}
|