forked from http-rs/tide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.rs
72 lines (61 loc) · 1.84 KB
/
messages.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use http::status::StatusCode;
use serde::{Deserialize, Serialize};
use std::sync::Mutex;
use tide::{error::ResultExt, response, App, Context, EndpointResult};
#[derive(Default)]
struct Database {
contents: Mutex<Vec<Message>>,
}
#[derive(Serialize, Deserialize, Clone)]
struct Message {
author: Option<String>,
contents: String,
}
impl Database {
fn insert(&self, msg: Message) -> usize {
let mut table = self.contents.lock().unwrap();
table.push(msg);
table.len() - 1
}
fn get(&self, id: usize) -> Option<Message> {
self.contents.lock().unwrap().get(id).cloned()
}
fn set(&self, id: usize, msg: Message) -> bool {
let mut table = self.contents.lock().unwrap();
if let Some(old_msg) = table.get_mut(id) {
*old_msg = msg;
true
} else {
false
}
}
}
#[allow(unused_mut)] // Workaround clippy bug
async fn new_message(mut cx: Context<Database>) -> EndpointResult<String> {
let msg = cx.body_json().await.client_err()?;
Ok(cx.state().insert(msg).to_string())
}
#[allow(unused_mut)] // Workaround clippy bug
async fn set_message(mut cx: Context<Database>) -> EndpointResult<()> {
let msg = cx.body_json().await.client_err()?;
let id = cx.param("id").client_err()?;
if cx.state().set(id, msg) {
Ok(())
} else {
Err(StatusCode::NOT_FOUND)?
}
}
async fn get_message(cx: Context<Database>) -> EndpointResult {
let id = cx.param("id").client_err()?;
if let Some(msg) = cx.state().get(id) {
Ok(response::json(msg))
} else {
Err(StatusCode::NOT_FOUND)?
}
}
pub fn main() {
let mut app = App::with_state(Database::default());
app.at("/message").post(new_message);
app.at("/message/:id").get(get_message).post(set_message);
app.serve("127.0.0.1:8000").unwrap();
}