This repository has been archived by the owner on Oct 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.rs
192 lines (163 loc) · 5.25 KB
/
rest.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
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
extern crate futures;
extern crate hyper;
extern crate trek_router;
use futures::Future;
use hyper::server::Server;
use hyper::service::service_fn_ok;
use hyper::{Body, Request, Response, StatusCode};
use std::sync::Arc;
use trek_router::{Resource, ResourceOptions, Resources, Router};
type Params = Vec<(String, String)>;
type Handler = fn(Context) -> Body;
struct Context {
request: Request<Body>,
params: Params,
}
struct Geocoder {}
impl Resource for Geocoder {
type Context = Context;
type Body = Body;
fn show(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("Geocoder Show!");
Body::from(s)
}
fn create(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("Geocoder Create!");
Body::from(s)
}
fn update(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("Geocoder Update!");
Body::from(s)
}
fn delete(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("Geocoder Delete!");
Body::from(s)
}
fn edit(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("Geocoder Edit!");
Body::from(s)
}
fn new(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("Geocoder New!");
Body::from(s)
}
}
struct Users {}
impl Resources for Users {
type Context = Context;
type Body = Body;
fn index(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("Users Index!");
Body::from(s)
}
fn create(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("User Create!");
Body::from(s)
}
fn new(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("User New!");
Body::from(s)
}
fn show(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("User Show, ");
for (k, v) in ctx.params {
s.push_str(&format!("{} = {}", k, v));
}
s.push_str("!");
Body::from(s)
}
fn update(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("User Update, ");
for (k, v) in ctx.params {
s.push_str(&format!("{} = {}", k, v));
}
s.push_str("!");
Body::from(s)
}
fn delete(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("User Delete, ");
for (k, v) in ctx.params {
s.push_str(&format!("{} = {}", k, v));
}
s.push_str("!");
Body::from(s)
}
fn edit(ctx: Self::Context) -> Self::Body {
let mut s = String::new();
s.push_str(&ctx.request.uri().path().to_owned());
s.push_str("\n");
s.push_str("User Edit, ");
for (k, v) in ctx.params {
s.push_str(&format!("{} = {}", k, v));
}
s.push_str("!");
Body::from(s)
}
}
fn main() {
let addr = ([127, 0, 0, 1], 3000).into();
let mut router = Router::<Handler>::new();
router.resource("/geocoder", Geocoder::build(ResourceOptions::default()));
router.resources("/users", Users::build(ResourceOptions::default()));
let router = Arc::new(router);
let routing = move || {
let router = Arc::clone(&router);
service_fn_ok(move |request| {
let method = request.method().to_owned();
let path = request.uri().path().to_owned();
match router.find(&method, &path) {
Some((handler, params)) => Response::new(handler(Context {
request,
params: params
.iter()
.map(|(a, b)| (a.to_string(), b.to_string()))
.collect(),
})),
None => Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::from("Not Found"))
.unwrap(),
}
})
};
let server = Server::bind(&addr)
.serve(routing)
.map_err(|e| eprintln!("server error: {}", e));
hyper::rt::run(server);
}