api/
api.rs

1extern crate futures;
2#[macro_use]
3extern crate resty;
4#[macro_use]
5extern crate serde_derive;
6
7use std::sync::RwLock;
8use futures::Future;
9
10#[derive(Default)]
11struct Products {
12    calls: RwLock<Vec<Call>>,
13}
14
15impl Products {
16    pub fn list(&self) -> Result<Vec<Call>, resty::Error> {
17        Ok(self.calls.read().unwrap().clone())
18    }
19
20    pub fn single(&self, id: usize) -> Result<Call, resty::Error> {
21        let calls = self.calls.read().unwrap();
22        if id < calls.len() {
23            Ok(calls[id].clone())
24        } else {
25            Err(resty::Error::not_found(""))
26        }
27    }
28
29    pub fn add(&self, call: Call) -> Result<Call, resty::Error> {
30        self.calls.write().unwrap().push(call.clone());
31        Ok(call)
32    }
33
34    pub fn update(&self, id: usize, call: Call) -> Result<Call, resty::Error> {
35        let mut calls = self.calls.write().unwrap();
36        if id < calls.len() {
37            calls[id] = call.clone();
38            Ok(call)
39        } else {
40            Err(resty::Error::not_found(""))
41        }
42    }
43}
44
45// TODO [ToDr] Derive this implementatio
46impl Into<resty::Router> for Products {
47    fn into(self) -> resty::Router {
48        let mut router = resty::Router::new();
49        let self_ = ::std::sync::Arc::new(self);
50        let a = self_.clone();
51        // no params
52        router.get("/", move |_request| {
53            a.list()
54        });
55
56        let a = self_.clone();
57        // dynamic params
58        router.get("/{id}", move |request| {
59            a.single(request.params().get_usize("id")?)
60        });
61
62        let a = self_.clone();
63        // static params
64        router.get(url!(/test/{id:usize}), move |request| {
65            a.single(request.params().id)
66        });
67
68        let a = self_.clone();
69        router.put(url!(/{id:usize}), move |request| {
70            let a = a.clone();
71            let id = request.params().id;
72            request.json().map_err(Into::into).and_then(move |call: Call| {
73                a.update(id, call)
74            })
75        });
76
77        let a = self_.clone();
78        // post request
79        router.post("/", move |request| {
80            let a = a.clone();
81            request.json().map_err(Into::into).and_then(move |call: Call| {
82                a.add(call)
83            })
84        });
85
86        router
87    }
88}
89
90#[derive(Deserialize, Serialize, Clone)]
91struct Call {
92    pub test: u64,
93}
94
95fn main() {
96    let mut v1 = resty::Router::new();
97    v1.add("/products", Products {
98        calls: RwLock::new(vec![Call { test: 1 }, Call { test: 2}]),
99    }.into());
100
101    let mut server = resty::Router::new();
102    server.add("/v1", v1);
103    server.post("/test", |request| {
104        request.json().map(|mut call: Call| {
105            call.test += 1;
106            call
107        })
108    });
109
110    println!("{}", server.routes());
111    let listening = server.bind("localhost:3000").unwrap();
112    listening.wait()
113}