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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use std::future::Future;
use std::io;
use std::pin::Pin;
use crate::core::context::Context;
use crate::core::request::Request;
use crate::parser::{middleware_traits::MiddlewareTuple, tree::Node, tree::NodeOutput};
use crate::{
context::basic_context::{generate_context, BasicContext},
core::request::ThrusterRequest,
};
pub trait Middleware<CIn: Send + Sync, COut: Send + Sync, CErr: Send + Sync>:
FnOnce(CIn) -> Pin<Box<dyn Future<Output = Result<COut, CErr>> + Send + Sync>>
{
fn val(&self) -> Self
where
Self: Sized + Copy,
{
panic!("Never use this.")
}
}
type ReturnValue<T> = T;
pub struct App<R: ThrusterRequest, T: 'static + Context + Clone + Send + Sync, S: Send> {
pub delete_root: Node<ReturnValue<T>>,
pub get_root: Node<ReturnValue<T>>,
pub options_root: Node<ReturnValue<T>>,
pub post_root: Node<ReturnValue<T>>,
pub put_root: Node<ReturnValue<T>>,
pub patch_root: Node<ReturnValue<T>>,
pub context_generator: fn(R, &S, &str) -> T,
pub state: std::sync::Arc<S>,
}
impl<R: 'static + ThrusterRequest, T: Context + Clone + Send + Sync, S: 'static + Send>
App<R, T, S>
{
pub fn new_basic() -> App<Request, BasicContext, ()> {
App::create(generate_context, ())
}
pub fn create(generate_context: fn(R, &S, &str) -> T, state: S) -> App<R, T, S> {
App {
delete_root: Node::default(),
get_root: Node::default(),
options_root: Node::default(),
post_root: Node::default(),
put_root: Node::default(),
patch_root: Node::default(),
context_generator: generate_context,
state: std::sync::Arc::new(state),
}
}
pub fn use_middleware(
&mut self,
path: &str,
middlewares: MiddlewareTuple<ReturnValue<T>>,
) -> &mut App<R, T, S>
where
T: Clone,
{
self.get_root
.add_non_leaf_value_at_path(path, middlewares.clone());
self.options_root
.add_non_leaf_value_at_path(path, middlewares.clone());
self.post_root
.add_non_leaf_value_at_path(path, middlewares.clone());
self.put_root
.add_non_leaf_value_at_path(path, middlewares.clone());
self.delete_root
.add_non_leaf_value_at_path(path, middlewares.clone());
self.patch_root
.add_non_leaf_value_at_path(path, middlewares.clone());
self
}
pub fn use_sub_app(&mut self, prefix: &str, app: App<R, T, S>) -> &mut App<R, T, S> {
self.get_root.add_node_at_path(prefix, app.get_root);
self.options_root.add_node_at_path(prefix, app.options_root);
self.post_root.add_node_at_path(prefix, app.post_root);
self.put_root.add_node_at_path(prefix, app.put_root);
self.delete_root.add_node_at_path(prefix, app.delete_root);
self.patch_root.add_node_at_path(prefix, app.patch_root);
self
}
pub fn get(
&mut self,
path: &str,
middlewares: MiddlewareTuple<ReturnValue<T>>,
) -> &mut App<R, T, S> {
self.get_root.add_value_at_path(path, middlewares);
self
}
pub fn options(
&mut self,
path: &str,
middlewares: MiddlewareTuple<ReturnValue<T>>,
) -> &mut App<R, T, S> {
self.options_root.add_value_at_path(path, middlewares);
self
}
pub fn post(
&mut self,
path: &str,
middlewares: MiddlewareTuple<ReturnValue<T>>,
) -> &mut App<R, T, S> {
self.post_root.add_value_at_path(path, middlewares);
self
}
pub fn put(
&mut self,
path: &str,
middlewares: MiddlewareTuple<ReturnValue<T>>,
) -> &mut App<R, T, S> {
self.put_root.add_value_at_path(path, middlewares);
self
}
pub fn delete(
&mut self,
path: &str,
middlewares: MiddlewareTuple<ReturnValue<T>>,
) -> &mut App<R, T, S> {
self.delete_root.add_value_at_path(path, middlewares);
self
}
pub fn patch(
&mut self,
path: &str,
middlewares: MiddlewareTuple<ReturnValue<T>>,
) -> &mut App<R, T, S> {
self.patch_root.add_value_at_path(path, middlewares);
self
}
pub fn set404(&mut self, middlewares: MiddlewareTuple<ReturnValue<T>>) -> &mut App<R, T, S>
where
T: Clone,
{
self.get_root.add_value_at_path("/*", middlewares.clone());
self.options_root
.add_value_at_path("/*", middlewares.clone());
self.post_root.add_value_at_path("/*", middlewares.clone());
self.put_root.add_value_at_path("/*", middlewares.clone());
self.delete_root
.add_value_at_path("/*", middlewares.clone());
self.patch_root.add_value_at_path("/*", middlewares.clone());
self
}
pub fn commit(mut self) -> Self {
self.get_root = self.get_root.commit();
self.options_root = self.options_root.commit();
self.post_root = self.post_root.commit();
self.put_root = self.put_root.commit();
self.delete_root = self.delete_root.commit();
self.patch_root = self.patch_root.commit();
self
}
pub fn resolve_from_method_and_path<'m>(
&'m self,
method: &str,
path: String,
) -> NodeOutput<'m, ReturnValue<T>> {
match method {
"OPTION" => self.options_root.get_value_at_path(path),
"POST" => self.post_root.get_value_at_path(path),
"PUT" => self.put_root.get_value_at_path(path),
"DELETE" => self.delete_root.get_value_at_path(path),
"PATCH" => self.patch_root.get_value_at_path(path),
_ => self.get_root.get_value_at_path(path),
}
}
pub fn match_and_resolve<'m>(
&'m self,
request: R,
) -> impl Future<Output = Result<T::Response, io::Error>> + 'm + Send + Sync {
let method = request.method();
let path = request.path();
let node = match method {
"OPTION" => self.options_root.get_value_at_path(path),
"POST" => self.post_root.get_value_at_path(path),
"PUT" => self.put_root.get_value_at_path(path),
"DELETE" => self.delete_root.get_value_at_path(path),
"PATCH" => self.patch_root.get_value_at_path(path),
_ => self.get_root.get_value_at_path(path),
};
let context = (self.context_generator)(request, &self.state, &node.path);
let copy = node.value;
async move {
let ctx = (copy)(context).await;
let ctx = match ctx {
Ok(val) => val,
Err(e) => e.context,
};
Ok(ctx.get_response())
}
}
pub fn resolve<'m>(
&self,
request: R,
matched_route: NodeOutput<'m, T>,
) -> impl Future<Output = Result<T::Response, io::Error>> + 'm + Send + Sync {
self._resolve(request, matched_route)
}
fn _resolve<'m>(
&self,
request: R,
matched_route: NodeOutput<'m, T>,
) -> impl Future<Output = Result<T::Response, io::Error>> + 'm + Send + Sync {
let context = (self.context_generator)(request, &self.state, &matched_route.path);
let copy = matched_route.value;
async move {
let ctx = (copy)(context).await;
let ctx = match ctx {
Ok(val) => val,
Err(e) => e.context,
};
Ok(ctx.get_response())
}
}
}