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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;

use viz_core::{Context, Middleware, Middlewares, Response, Result};
use viz_utils::tracing;

use crate::{Method, PathTree, Route, RouteHandler};

/// Router
pub struct Router {
    // inherit parrent's middleware
    carry: bool,
    path: String,
    name: Option<String>,
    routes: Option<Vec<Route>>,
    children: Option<Vec<Router>>,
    middleware: Option<Middlewares>,
}

impl Router {
    /// Creates new Router with path.
    pub fn new(path: &str) -> Self {
        Self {
            name: None,
            carry: true,
            routes: None,
            children: None,
            middleware: None,
            path: path.to_owned(),
        }
    }

    /// Prefix path of the Router.
    pub fn path(mut self, path: &str) -> Self {
        self.path.insert_str(0, path);
        self
    }

    /// Name of the Router.
    pub fn name(mut self, name: &str) -> Self {
        self.name.replace(name.to_owned());
        self
    }

    /// Inherits parrent's middleware
    pub fn carry(mut self, b: bool) -> Self {
        self.carry = b;
        self
    }

    /// Appends middleware to the Router.
    pub fn mid<M>(mut self, m: M) -> Self
    where
        M: for<'a> Middleware<'a, Context, Output = Result<Response>>,
    {
        self.middleware.get_or_insert_with(Vec::new).insert(0, Arc::new(m));
        self
    }

    /// Creates a scope Router with path.
    pub fn scope(mut self, path: &str, router: Router) -> Self {
        self.children.get_or_insert_with(Vec::new).push(router.path(path));
        self
    }

    /// Appends a route.
    pub fn route(mut self, route: Route) -> Self {
        self.routes.get_or_insert_with(Vec::new).push(route);
        self
    }

    /// Appends a route with path.
    pub fn at(mut self, path: &str, route: Route) -> Self {
        self.routes.get_or_insert_with(Vec::new).push(route.path(path));
        self
    }

    /// Outputs the routes to map.
    pub fn finish(mut self, tree: &mut HashMap<Method, PathTree<Middlewares>>) {
        let m0 = self.middleware.take().unwrap_or_default();
        let h0 = m0.len() > 0;

        if let Some(routes) = self.routes.take() {
            for mut route in routes {
                let m1 = route.middleware.take().unwrap_or_default();
                let carry = route.carry;
                let guard = route.guard.take().map(|g| Arc::new(g));
                let path = join_paths(&self.path, &route.path);

                for (method, handler) in route.handlers {
                    tracing::info!("{:>6}:{}", method.as_str(), &path);

                    let mut m = vec![if let Some(guard) = guard.clone().take() {
                        Arc::new(RouteHandler::new(guard, handler))
                    } else {
                        handler
                    }];

                    if m1.len() > 0 {
                        m.extend_from_slice(&m1);
                    }

                    if h0 && carry {
                        m.extend_from_slice(&m0);
                    }

                    tree.entry(method).or_insert_with(PathTree::new).insert(&path, m);
                }
            }
        }

        if let Some(children) = self.children.take() {
            for mut child in children {
                let path = join_paths(&self.path, &child.path);
                // log::debug!("{}", &path);
                child.path = path;
                if h0 && child.carry {
                    child.middleware.get_or_insert_with(Vec::new).extend_from_slice(&m0);
                }
                child.finish(tree);
            }
        }

        //         if h0 && self.path.is_empty() {
        //             let method = Method::All;
        //             let path = "/*";

        //             log::info!("{:>6}:{}", method.as_str(), &path);

        //             tree.entry(method)
        //                 .or_insert_with(PathTree::new)
        //                 .insert(path, m0);
        //         }
    }
}

/// Creates new Router with empty path
pub fn router() -> Router {
    Router::new("")
}

pub(crate) fn join_paths(a: &str, b: &str) -> String {
    if b.is_empty() {
        return a.to_owned();
    }
    a.trim_end_matches('/').to_owned() + "/" + b.trim_start_matches('/')
}

impl fmt::Debug for Router {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Router")
            .field("path", &self.path)
            .field("name", &self.name.as_ref().map_or_else(String::new, |v| v.to_owned()))
            .field("carry", &self.carry)
            .field("middle", &self.middleware.as_ref().map_or_else(|| 0, |v| v.len()))
            .field("routes", &self.routes.as_ref().map_or_else(|| &[][..], |v| v))
            .field("children", &self.children.as_ref().map_or_else(|| &[][..], |v| v))
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use futures_executor::block_on;
    use hyper::body::to_bytes;

    use viz_core::{http, Context, Error, Guard, Response, Result};

    use crate::*;

    #[test]
    fn routing() {
        let routes = router()
            .mid(m_0)
            // `/`
            .route(route().path("/").get(hello_world))
            .at("/*any", route().all(any))
            // `/users`
            .scope(
                "/users",
                router()
                    .mid(m_1)
                    // `/users`
                    .route(route().get(index_users).post(create_user))
                    // `/users/new`
                    .at("/new", route().guard(edit_guard).get(new_user))
                    // `/users/:id`
                    .scope(
                        "/:id",
                        router()
                            // `/users/:id`
                            .route(
                                route()
                                    .get(show_user)
                                    .patch(update_user)
                                    .put(update_user)
                                    .delete(delete_user),
                            )
                            // `/users/:id/edit`
                            .at("/edit", route().guard(edit_guard).get(edit_user))
                            // `/users/*``
                            .at("*any", route().all(any)),
                    )
                    .scope(
                        "/:user_id",
                        router()
                            // .path("/posts")
                            // `/users/:user_id/posts`
                            .scope(
                                "/posts",
                                router()
                                    .carry(false)
                                    .mid(m_2)
                                    .at("/*any", route().all(any))
                                    // `/users/:user_id/posts`
                                    .route(route().get(index_posts).post(create_post))
                                    // `/users/:user_id/posts/new`
                                    .at("/new", route().guard(edit_guard).get(new_post))
                                    // `/users/:user_id/posts/:id`
                                    .scope(
                                        "/:id",
                                        router()
                                            // `/users/:user_id/posts/:id`
                                            .route(
                                                route()
                                                    .guard(
                                                        <Box<dyn Guard>>::from(edit_guard)
                                                            & Into::<Box<dyn Guard>>::into(
                                                                get_guard,
                                                            ),
                                                    )
                                                    .get(show_post),
                                            )
                                            .route(
                                                route()
                                                    // .get(show_post)
                                                    .patch(update_post)
                                                    .put(update_post)
                                                    .delete(delete_post),
                                            )
                                            // `/users/:user_id/posts/:id/edit`
                                            .at("/edit", route().guard(edit_guard).get(edit_post)),
                                    ),
                            ),
                    ),
            )
            // `/posts`
            .scope(
                "/posts",
                router()
                    // `/posts`
                    .route(route().get(index_posts))
                    // `/posts/:id`
                    .at("/:id", route().get(show_post)),
            )
            // `/comments`
            .scope(
                "/comments",
                router()
                    // `/comments`
                    .route(route().get(index_comments).post(create_comment))
                    // `/comments/new`
                    .at("/new", route().guard(edit_guard).get(new_comment))
                    // `/comments/:id`
                    .scope(
                        "/:id",
                        router()
                            // `/comments/:id`
                            .route(
                                route().get(show_comment).patch(update_comment).put(update_comment),
                            )
                            // `/comments/:id/edit`
                            .at("/edit", route().get(edit_comment)),
                    ),
            );

        let mut tree = HashMap::new();

        routes.finish(&mut tree);

        for i in 0..3 {
            println!("");
            println!("request {}", i);

            let tr = tree.get(&Method::Verb(http::Method::GET));

            if let Some(t) = tr {
                // Open Edit User Page
                let mut req = http::Request::default();
                *req.uri_mut() = "/users/fundon/edit".parse().unwrap();
                *req.body_mut() = "Open Edit User Page".into();

                println!("");
                println!("request {} {}", i, req.uri());

                if let Some(r) = t.find(&req.uri().to_string()) {
                    let mut cx = Context::from(req);

                    *cx.middleware_mut() = r.0.to_vec();

                    assert_eq!(r.1, vec![("id", "fundon")]);

                    assert!(block_on(async move {
                        let res: http::Response = cx.next().await?.into();

                        assert_eq!(res.status(), 200);
                        assert_eq!(to_bytes(res.into_body()).await.unwrap(), "Edit user");

                        Ok::<_, Error>(())
                    })
                    .is_ok());
                }

                // Open Edit Post Page
                let mut req = http::Request::default();
                *req.uri_mut() = "/users/fundon/posts/233/edit".parse().unwrap();
                *req.body_mut() = "Open Edit Post Page".into();

                println!("");
                println!("request {} {}", i, req.uri());

                if let Some(r) = t.find(&req.uri().to_string()) {
                    let mut cx = Context::from(req);

                    *cx.middleware_mut() = r.0.to_vec();

                    assert_eq!(r.1, vec![("user_id", "fundon"), ("id", "233")]);

                    assert!(block_on(async move {
                        let res: http::Response = cx.next().await?.into();

                        assert_eq!(res.status(), 200);
                        assert_eq!(to_bytes(res.into_body()).await.unwrap(), "Edit post");

                        Ok::<_, Error>(())
                    })
                    .is_ok());
                }

                // Open Get Post Page
                let mut req = http::Request::default();
                *req.uri_mut() = "/users/fundon/posts/233".parse().unwrap();
                *req.body_mut() = "Open Get Post Page".into();

                println!("");
                println!("request {} {}", i, req.uri());

                if let Some(r) = t.find(&req.uri().to_string()) {
                    let mut cx = Context::from(req);

                    *cx.middleware_mut() = r.0.to_vec();

                    assert_eq!(r.1, vec![("user_id", "fundon"), ("id", "233")]);

                    assert!(block_on(async move {
                        let res: http::Response = cx.next().await?.into();

                        assert_eq!(res.status(), 404);
                        assert_eq!(to_bytes(res.into_body()).await.unwrap(), "");

                        Ok::<_, Error>(())
                    })
                    .is_ok());
                }
            }

            // let tr = tree.get(&Method::All);

            // if let Some(t) = tr {
            //     if let Some(r) = t.find("/user") {
            //         dbg!(r.1);
            //     }
            //     if let Some(r) = t.find("/users/fundon/edit") {
            //         dbg!(r.1);
            //     }
            //     if let Some(r) = t.find("/users/fundon/posts/233/edit") {
            //         dbg!(r.1);
            //     }
            // }
        }

        fn edit_guard(_cx: &Context) -> bool {
            true
        }

        fn get_guard(_cx: &Context) -> bool {
            false
        }

        async fn m_0(cx: &mut Context) -> Result<Response> {
            println!("middleware 0");
            cx.next().await
        }

        async fn m_1(cx: &mut Context) -> Result<Response> {
            println!("middleware 1");
            cx.next().await
        }

        async fn m_2(cx: &mut Context) -> Result<Response> {
            println!("middleware 2");
            cx.next().await
        }

        // `/`
        async fn hello_world() -> &'static str {
            "Hello, world!"
        }

        // `/*`
        async fn any() -> &'static str {
            "* any!"
        }

        // `/users`
        // -----------------
        async fn new_user() -> &'static str {
            "New user"
        }

        async fn edit_user() -> &'static str {
            "Edit user"
        }

        async fn index_users() -> &'static str {
            "Index users"
        }

        async fn create_user() -> &'static str {
            "Create user"
        }

        async fn show_user() -> &'static str {
            "Show user"
        }

        async fn update_user() -> &'static str {
            "Update user"
        }

        async fn delete_user() -> &'static str {
            "Delete user"
        }
        // -----------------

        // `/posts`
        // -----------------
        async fn new_post() -> &'static str {
            "New post"
        }

        async fn edit_post() -> &'static str {
            "Edit post"
        }

        async fn index_posts() -> &'static str {
            "Index posts"
        }

        async fn create_post() -> &'static str {
            "Create post"
        }

        async fn show_post() -> &'static str {
            "Show post"
        }

        async fn update_post() -> &'static str {
            "Update post"
        }

        async fn delete_post() -> &'static str {
            "Delete post"
        }
        // -----------------

        // `/comments`
        // -----------------
        async fn new_comment() -> &'static str {
            "New comment"
        }

        async fn edit_comment() -> &'static str {
            "Edit comment"
        }

        async fn index_comments() -> &'static str {
            "Index comments"
        }

        async fn create_comment() -> &'static str {
            "Create comment"
        }

        async fn show_comment() -> &'static str {
            "Show comment"
        }

        async fn update_comment() -> &'static str {
            "Update comment"
        }
        // -----------------
    }
}