mincat_core/
route.rs

1use http::Method;
2
3use crate::{
4    handler::{FuncParamHandler, Handler, HandlerFuncParam},
5    middleware::Middleware,
6};
7
8#[derive(Clone)]
9pub struct Route {
10    pub method: Method,
11    pub path: String,
12    pub handler: Handler,
13}
14
15impl Route {
16    pub fn middleware<T>(&mut self, middleware: T) -> Self
17    where
18        T: Into<Box<dyn Middleware>>,
19    {
20        self.handler.middleware(middleware);
21        self.clone()
22    }
23
24    pub fn init<Path, Func, Param>(method: Method, path: Path, func: Func) -> Self
25    where
26        Path: Into<String>,
27        Func: HandlerFuncParam<Param> + Sync + Clone + 'static,
28        Param: Send + Sync + 'static,
29    {
30        let path = path.into();
31        let handler = FuncParamHandler::from(func).into();
32        Self {
33            method,
34            path,
35            handler,
36        }
37    }
38}
39
40impl From<(Method, String, Handler)> for Route {
41    fn from(value: (Method, String, Handler)) -> Self {
42        Self {
43            method: value.0,
44            path: value.1,
45            handler: value.2,
46        }
47    }
48}