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
use crate::{router::Router, Endpoint};
pub struct Route<'a, AppData> {
router: &'a mut Router<AppData>,
path: String,
}
impl<'a, AppData: 'static> Route<'a, AppData> {
pub(crate) fn new(router: &'a mut Router<AppData>, path: String) -> Route<'a, AppData> {
Route { router, path }
}
pub fn at<'b>(&'b mut self, path: &str) -> Route<'b, AppData> {
let mut p = self.path.clone();
if !p.ends_with('/') && !path.starts_with('/') {
p.push_str("/");
}
if path != "/" {
p.push_str(path);
}
Route {
router: &mut self.router,
path: p,
}
}
pub fn nest(&mut self, f: impl FnOnce(&mut Route<'a, AppData>)) -> &mut Self {
f(self);
self
}
pub fn method(&mut self, method: http::Method, ep: impl Endpoint<AppData>) -> &mut Self {
self.router.add(&self.path, method, ep);
self
}
pub fn get(&mut self, ep: impl Endpoint<AppData>) -> &mut Self {
self.method(http::Method::GET, ep);
self
}
pub fn head(&mut self, ep: impl Endpoint<AppData>) -> &mut Self {
self.method(http::Method::HEAD, ep);
self
}
pub fn put(&mut self, ep: impl Endpoint<AppData>) -> &mut Self {
self.method(http::Method::PUT, ep);
self
}
pub fn post(&mut self, ep: impl Endpoint<AppData>) -> &mut Self {
self.method(http::Method::POST, ep);
self
}
pub fn delete(&mut self, ep: impl Endpoint<AppData>) -> &mut Self {
self.method(http::Method::DELETE, ep);
self
}
pub fn options(&mut self, ep: impl Endpoint<AppData>) -> &mut Self {
self.method(http::Method::OPTIONS, ep);
self
}
pub fn connect(&mut self, ep: impl Endpoint<AppData>) -> &mut Self {
self.method(http::Method::CONNECT, ep);
self
}
pub fn patch(&mut self, ep: impl Endpoint<AppData>) -> &mut Self {
self.method(http::Method::PATCH, ep);
self
}
pub fn trace(&mut self, ep: impl Endpoint<AppData>) -> &mut Self {
self.method(http::Method::TRACE, ep);
self
}
}