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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#![deny(warnings)]
use std::env;
use warp::Filter;
#[tokio::main]
async fn main() {
if env::var_os("RUST_LOG").is_none() {
env::set_var("RUST_LOG", "todos=info");
}
pretty_env_logger::init();
let db = models::blank_db();
let api = filters::todos(db);
let routes = api.with(warp::log("todos"));
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
mod filters {
use super::handlers;
use super::models::{Db, ListOptions, Todo};
use warp::Filter;
pub fn todos(
db: Db,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
todos_list(db.clone())
.or(todos_create(db.clone()))
.or(todos_update(db.clone()))
.or(todos_delete(db))
}
pub fn todos_list(
db: Db,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path!("todos")
.and(warp::get())
.and(warp::query::<ListOptions>())
.and(with_db(db))
.and_then(handlers::list_todos)
}
pub fn todos_create(
db: Db,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path!("todos")
.and(warp::post())
.and(json_body())
.and(with_db(db))
.and_then(handlers::create_todo)
}
pub fn todos_update(
db: Db,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path!("todos" / u64)
.and(warp::put())
.and(json_body())
.and(with_db(db))
.and_then(handlers::update_todo)
}
pub fn todos_delete(
db: Db,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
let admin_only = warp::header::exact("authorization", "Bearer admin");
warp::path!("todos" / u64)
.and(admin_only)
.and(warp::delete())
.and(with_db(db))
.and_then(handlers::delete_todo)
}
fn with_db(db: Db) -> impl Filter<Extract = (Db,), Error = std::convert::Infallible> + Clone {
warp::any().map(move || db.clone())
}
fn json_body() -> impl Filter<Extract = (Todo,), Error = warp::Rejection> + Clone {
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}
}
mod handlers {
use super::models::{Db, ListOptions, Todo};
use std::convert::Infallible;
use warp::http::StatusCode;
pub async fn list_todos(opts: ListOptions, db: Db) -> Result<impl warp::Reply, Infallible> {
let todos = db.lock().await;
let todos: Vec<Todo> = todos
.clone()
.into_iter()
.skip(opts.offset.unwrap_or(0))
.take(opts.limit.unwrap_or(std::usize::MAX))
.collect();
Ok(warp::reply::json(&todos))
}
pub async fn create_todo(create: Todo, db: Db) -> Result<impl warp::Reply, Infallible> {
log::debug!("create_todo: {:?}", create);
let mut vec = db.lock().await;
for todo in vec.iter() {
if todo.id == create.id {
log::debug!(" -> id already exists: {}", create.id);
return Ok(StatusCode::BAD_REQUEST);
}
}
vec.push(create);
Ok(StatusCode::CREATED)
}
pub async fn update_todo(
id: u64,
update: Todo,
db: Db,
) -> Result<impl warp::Reply, Infallible> {
log::debug!("update_todo: id={}, todo={:?}", id, update);
let mut vec = db.lock().await;
for todo in vec.iter_mut() {
if todo.id == id {
*todo = update;
return Ok(StatusCode::OK);
}
}
log::debug!(" -> todo id not found!");
Ok(StatusCode::NOT_FOUND)
}
pub async fn delete_todo(id: u64, db: Db) -> Result<impl warp::Reply, Infallible> {
log::debug!("delete_todo: id={}", id);
let mut vec = db.lock().await;
let len = vec.len();
vec.retain(|todo| {
todo.id != id
});
let deleted = vec.len() != len;
if deleted {
Ok(StatusCode::NO_CONTENT)
} else {
log::debug!(" -> todo id not found!");
Ok(StatusCode::NOT_FOUND)
}
}
}
mod models {
use serde_derive::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::sync::Mutex;
pub type Db = Arc<Mutex<Vec<Todo>>>;
pub fn blank_db() -> Db {
Arc::new(Mutex::new(Vec::new()))
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Todo {
pub id: u64,
pub text: String,
pub completed: bool,
}
#[derive(Debug, Deserialize)]
pub struct ListOptions {
pub offset: Option<usize>,
pub limit: Option<usize>,
}
}
#[cfg(test)]
mod tests {
use warp::http::StatusCode;
use warp::test::request;
use super::{
filters,
models::{self, Todo},
};
#[tokio::test]
async fn test_post() {
let db = models::blank_db();
let api = filters::todos(db);
let resp = request()
.method("POST")
.path("/todos")
.json(&Todo {
id: 1,
text: "test 1".into(),
completed: false,
})
.reply(&api)
.await;
assert_eq!(resp.status(), StatusCode::CREATED);
}
#[tokio::test]
async fn test_post_conflict() {
let db = models::blank_db();
db.lock().await.push(todo1());
let api = filters::todos(db);
let resp = request()
.method("POST")
.path("/todos")
.json(&todo1())
.reply(&api)
.await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
#[tokio::test]
async fn test_put_unknown() {
let _ = pretty_env_logger::try_init();
let db = models::blank_db();
let api = filters::todos(db);
let resp = request()
.method("PUT")
.path("/todos/1")
.header("authorization", "Bearer admin")
.json(&todo1())
.reply(&api)
.await;
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
fn todo1() -> Todo {
Todo {
id: 1,
text: "test 1".into(),
completed: false,
}
}
}