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
use super::{request::Request, response::Response};

type ControllerBoxType = Box<dyn Fn(Request) -> Response + Sync + Send + 'static>;

pub trait ControllerT {
    fn controller(&self, _: Request) -> Response;
}

/// Http Controller
pub struct Controller {
    call_back: ControllerBoxType,
}

impl Controller {
    /// Returns new Controller
    pub fn new<F>(cb: F) -> Controller
    where
        F: Fn(Request) -> Response + Sync + Send + 'static,
    {
        let action = Box::new(cb);

        Controller { call_back: action }
    }
}

impl Controller {
    /// Returns CallBack clouse function
    pub fn get_action(&self) -> &ControllerBoxType {
        &self.call_back
    }

    /// Use CallBack function
    ///
    /// # Arguments
    ///
    /// * `request` - Request
    pub fn use_action(&self, request: Request) -> Response {
        let cb = &self.get_action();

        cb(request)
    }
}