rocal_core/
route_handler.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
use std::cell::RefCell;
use std::rc::Rc;

use url::Url;
use web_sys::window;

use crate::enums::request_method::RequestMethod;
use crate::router::Router;

pub struct RouteHandler {
    router: Rc<RefCell<Router>>,
    not_found: Box<dyn Fn()>,
}

impl RouteHandler {
    pub fn new(router: Rc<RefCell<Router>>, not_found: Option<Box<dyn Fn()>>) -> Self {
        let not_found = match not_found {
            Some(nf) => nf,
            None => Box::new(Self::default_not_found_page),
        };

        RouteHandler { router, not_found }
    }

    pub async fn handle_route(&self) {
        let href = if let Some(w) = window() {
            if let Ok(href) = w.location().href() {
                href
            } else {
                (self.not_found)();
                return;
            }
        } else {
            (self.not_found)();
            return;
        };

        let url = match Url::parse(&href) {
            Ok(u) => u,
            Err(_) => {
                (self.not_found)();
                return;
            }
        };

        let path = url.fragment().unwrap_or_else(|| "/");

        if !self
            .router
            .borrow()
            .resolve(RequestMethod::Get, path, None)
            .await
        {
            (self.not_found)();
        }
    }

    fn default_not_found_page() {
        web_sys::window()
            .unwrap()
            .document()
            .unwrap()
            .body()
            .unwrap()
            .set_inner_html("<h1>404 - Page Not Found</h1>");
    }
}