rs_express/
router.rs

1use crate::middleware::matchable_middleware::{MatchableMethod, MatchablePath};
2use crate::{Error, ErrorMiddleware, Middleware, NextFunction, Request, Response};
3use hyper::Method;
4
5pub struct Router<T> {
6    path: &'static str,
7    pub(crate) middleware: Vec<Middleware<T>>,
8    pub(crate) error_middleware: Vec<ErrorMiddleware<T>>,
9}
10
11impl<T> Router<T> {
12    /// Creates a new router
13    pub fn new(path: &'static str) -> Router<T> {
14        Router {
15            path,
16            middleware: vec![],
17            error_middleware: vec![],
18        }
19    }
20
21    /// Adds a new middleware that apply to every request received.
22    pub fn use_ok<F>(&mut self, handler: F) -> &mut Self
23    where
24        F: Fn(&mut Request<T>, &mut Response, &mut NextFunction) + Send + Sync + 'static,
25    {
26        self.middleware.push(Middleware::new(
27            self.path,
28            MatchableMethod::All,
29            MatchablePath::All,
30            Box::new(handler),
31        ));
32
33        self
34    }
35
36    /// Adds a new middleware to handle errors.
37    pub fn use_err<F>(&mut self, handler: F) -> &mut Self
38    where
39        F: Fn(&Error, &mut Request<T>, &mut Response, &mut NextFunction) + Send + Sync + 'static,
40    {
41        self.error_middleware
42            .push(ErrorMiddleware::new(self.path, Box::new(handler)));
43
44        self
45    }
46
47    /// Adds a new middleware that apply to a GET request received.
48    pub fn get<F>(&mut self, path: &'static str, handler: F) -> &mut Self
49    where
50        F: Fn(&mut Request<T>, &mut Response, &mut NextFunction) + Send + Sync + 'static,
51    {
52        self.middleware.push(Middleware::new(
53            self.path,
54            MatchableMethod::Method(Method::GET),
55            MatchablePath::Path(path),
56            Box::new(handler),
57        ));
58
59        self
60    }
61
62    /// Adds a new middleware that apply to a POST request received.
63    pub fn post<F>(&mut self, path: &'static str, handler: F) -> &mut Self
64    where
65        F: Fn(&mut Request<T>, &mut Response, &mut NextFunction) + Send + Sync + 'static,
66    {
67        self.middleware.push(Middleware::new(
68            self.path,
69            MatchableMethod::Method(Method::POST),
70            MatchablePath::Path(path),
71            Box::new(handler),
72        ));
73
74        self
75    }
76
77    /// Adds a new middleware that apply to a PUT request received.
78    pub fn put<F>(&mut self, path: &'static str, handler: F) -> &mut Self
79    where
80        F: Fn(&mut Request<T>, &mut Response, &mut NextFunction) + Send + Sync + 'static,
81    {
82        self.middleware.push(Middleware::new(
83            self.path,
84            MatchableMethod::Method(Method::PUT),
85            MatchablePath::Path(path),
86            Box::new(handler),
87        ));
88
89        self
90    }
91
92    /// Adds a new middleware that apply to a PATCH request received.
93    pub fn patch<F>(&mut self, path: &'static str, handler: F) -> &mut Self
94    where
95        F: Fn(&mut Request<T>, &mut Response, &mut NextFunction) + Send + Sync + 'static,
96    {
97        self.middleware.push(Middleware::new(
98            self.path,
99            MatchableMethod::Method(Method::PATCH),
100            MatchablePath::Path(path),
101            Box::new(handler),
102        ));
103
104        self
105    }
106
107    /// Adds a new middleware that apply to a DELETE request received.
108    pub fn delete<F>(&mut self, path: &'static str, handler: F) -> &mut Self
109    where
110        F: Fn(&mut Request<T>, &mut Response, &mut NextFunction) + Send + Sync + 'static,
111    {
112        self.middleware.push(Middleware::new(
113            self.path,
114            MatchableMethod::Method(Method::DELETE),
115            MatchablePath::Path(path),
116            Box::new(handler),
117        ));
118
119        self
120    }
121
122    /// Adds a new middleware that apply to a OPTIONS request received.
123    pub fn options<F>(&mut self, path: &'static str, handler: F) -> &mut Self
124    where
125        F: Fn(&mut Request<T>, &mut Response, &mut NextFunction) + Send + Sync + 'static,
126    {
127        self.middleware.push(Middleware::new(
128            self.path,
129            MatchableMethod::Method(Method::OPTIONS),
130            MatchablePath::Path(path),
131            Box::new(handler),
132        ));
133
134        self
135    }
136}