rs_express/middleware/
middleware.rs

1use crate::middleware::matchable_middleware::{
2    MatchableMethod, MatchableMiddleware, MatchablePath,
3};
4use crate::middleware::NextFunction;
5use crate::{Request, Response};
6
7pub struct Middleware<T> {
8    pub(crate) base_url: &'static str,
9    method: MatchableMethod,
10    path: MatchablePath,
11    func: Box<dyn Fn(&mut Request<T>, &mut Response, &mut NextFunction) + Send + Sync + 'static>,
12}
13
14impl<T> Middleware<T> {
15    pub fn new(
16        base_url: &'static str,
17        method: MatchableMethod,
18        path: MatchablePath,
19        func: Box<
20            dyn Fn(&mut Request<T>, &mut Response, &mut NextFunction) + Send + Sync + 'static,
21        >,
22    ) -> Middleware<T> {
23        Middleware {
24            base_url,
25            method,
26            path,
27            func,
28        }
29    }
30
31    pub fn invoke(&self, req: &mut Request<T>, res: &mut Response, next: &mut NextFunction) {
32        (self.func)(req, res, next);
33    }
34}
35
36impl<T> MatchableMiddleware<T> for Middleware<T> {
37    fn path(&self) -> MatchablePath {
38        self.path.clone()
39    }
40
41    fn method(&self) -> MatchableMethod {
42        self.method.clone()
43    }
44}