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
use crate::{router::Router, Endpoint};
#[allow(missing_debug_implementations)]
pub struct Route<'a, State> {
router: &'a mut Router<State>,
path: String,
prefix: bool,
}
impl<'a, State: 'static> Route<'a, State> {
pub(crate) fn new(router: &'a mut Router<State>, path: String) -> Route<'a, State> {
Route {
router,
path,
prefix: false,
}
}
pub fn at<'b>(&'b mut self, path: &str) -> Route<'b, State> {
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,
prefix: false,
}
}
pub fn nest(&mut self, f: impl FnOnce(&mut Route<'a, State>)) -> &mut Self {
f(self);
self
}
#[cfg(any(feature = "unstable", feature = "docs"))]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
pub fn strip_prefix(&mut self) -> &mut Self {
self.prefix = true;
self
}
pub fn method(&mut self, method: http::Method, ep: impl Endpoint<State>) -> &mut Self {
if self.prefix {
let ep = StripPrefixEndpoint::new(ep);
self.router.add(&self.path, method.clone(), ep.clone());
let wildcard = self.at("*--tide-path-rest");
wildcard.router.add(&wildcard.path, method, ep);
} else {
self.router.add(&self.path, method, ep);
}
self
}
pub fn get(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::GET, ep);
self
}
pub fn head(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::HEAD, ep);
self
}
pub fn put(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::PUT, ep);
self
}
pub fn post(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::POST, ep);
self
}
pub fn delete(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::DELETE, ep);
self
}
pub fn options(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::OPTIONS, ep);
self
}
pub fn connect(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::CONNECT, ep);
self
}
pub fn patch(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::PATCH, ep);
self
}
pub fn trace(&mut self, ep: impl Endpoint<State>) -> &mut Self {
self.method(http::Method::TRACE, ep);
self
}
}
#[derive(Debug)]
struct StripPrefixEndpoint<E>(std::sync::Arc<E>);
impl<E> StripPrefixEndpoint<E> {
fn new(ep: E) -> Self {
Self(std::sync::Arc::new(ep))
}
}
impl<E> Clone for StripPrefixEndpoint<E> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<State, E: Endpoint<State>> Endpoint<State> for StripPrefixEndpoint<E> {
type Fut = E::Fut;
fn call(&self, mut req: crate::Request<State>) -> Self::Fut {
let rest = req.rest().unwrap_or("");
let mut path_and_query = format!("/{}", rest);
let uri = req.uri();
if let Some(query) = uri.query() {
path_and_query.push('?');
path_and_query.push_str(query);
}
let mut new_uri = http::Uri::builder();
if let Some(scheme) = uri.scheme_part() {
new_uri.scheme(scheme.clone());
}
if let Some(authority) = uri.authority_part() {
new_uri.authority(authority.clone());
}
new_uri.path_and_query(path_and_query.as_str());
let new_uri = new_uri.build().unwrap();
*req.request.uri_mut() = new_uri;
self.0.call(req)
}
}