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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
#[macro_use] extern crate rocket;
mod database;
use rocket::serde::json::Json;
use rocket::State;
use std::sync::Mutex;
use database::types::{Message, User, Info, UID, ReceiveMessage};
struct SharedDB {
sdb: Mutex<database::Database>
}
#[derive(serde::Serialize)]
struct Response<T, Y> (Result<T, Y>);
#[get("/get_message/<id>")]
fn get_message(id: usize, db: &State<SharedDB>) -> Json<Response<Message, &'static str>> {
let lock = db.sdb.lock().unwrap();
match lock.get_message(id) {
Some(n) => Json(Response(Ok(n))),
None => Json(Response(Err("Message not found"))),
}
}
#[get("/get_message_id_list/<id_start>/<id_end>")]
fn get_message_id_list(id_start: u64, id_end: u64, db: &State<SharedDB>) -> Json<Response<Vec<UID>, &'static str>> {
let lock = db.sdb.lock().unwrap();
let list = lock.get_message_id_list(id_start.into(), id_end.into());
if list.len() == 0 {
Json(Response(Err("No messages")))
} else {
Json(Response(Ok(list)))
}
}
#[get("/get_message_id_newest")]
fn get_message_id_newest(db: &State<SharedDB>) -> Json<Response<UID, &'static str>> {
let lock = db.sdb.lock().unwrap();
match lock.get_message_newest_id() {
Some(n) => Json(Response(Ok(n))),
None => Json(Response(Err("No messages")))
}
}
#[post("/send_message", format = "application/json", data = "<message>")]
fn send_message(message: Json<ReceiveMessage>, db: &State<SharedDB>) -> Json<Response<UID, &'static str>> {
let mut lock = db.sdb.lock().unwrap();
let id = lock.get_next_message_id();
let message = message.fill(id);
let send = lock.send_message(&message);
Json(Response(send))
}
#[delete("/delete_message", format = "application/json", data = "<id>")]
fn delete_message(id: Json<UID>, db: &State<SharedDB>) -> Json<Response<&'static str, &'static str>> {
let mut lock = db.sdb.lock().unwrap();
Json(Response(lock.delete_message(id.0)))
}
#[get("/get_user/<id>")]
fn get_user(id: u64, db: &State<SharedDB>) -> Json<Response<User, &'static str>> {
let lock = db.sdb.lock().unwrap();
match lock.get_user(id.into()) {
Some(n) => Json(Response(Ok(n))),
None => Json(Response(Err("User not found")))
}
}
#[get("/get_user_by_name/<name>")]
fn get_user_by_name(name: String, db: &State<SharedDB>) -> Json<Response<User, &'static str>> {
let lock = db.sdb.lock().unwrap();
match lock.get_user_by_name(name) {
Some(n) => Json(Response(Ok(n))),
None => Json(Response(Err("User not found")))
}
}
#[post("/create_user", format = "application/json", data = "<name>")]
fn create_user(name: Json<String>, db: &State<SharedDB>) -> Json<Response<UID, &'static str>> {
let mut lock = db.sdb.lock().unwrap();
Json(Response(lock.create_user(name.0)))
}
#[get("/ping")]
fn ping() -> Json<Response<&'static str, &'static str>> {
Json(Response(Ok("pong")))
}
#[get("/api")]
fn api_index(db: &State<SharedDB>) -> Json<Info> {
let lock = db.sdb.lock().unwrap();
Json(Info::get(lock))
}
#[derive(serde::Serialize)]
struct HitCount {
c: usize
}
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/api", routes![
get_message,
send_message,
get_message_id_list,
ping,
get_user,
create_user,
get_user_by_name,
get_message_id_newest,
delete_message,
])
.mount("/", routes![api_index])
.manage(SharedDB{sdb: Mutex::new(database::Database::new())})
}
|