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
68
69
70
71
72
73
74
75
76
77
78
79
mod body;
mod config;
mod cors;
mod header;
mod http;
mod request;
mod response;
mod router;
mod swish;
mod validater;

pub use crate::body::{Body, Json};
pub use crate::cors::{allow_everything, Cors};
pub use crate::request::Request;
pub use crate::swish::Swish;

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};
    #[test]
    // fn start() {
    //     swish_swish().bish();
    // }

    fn defined_like_this() -> Cors {
        Cors {
            access_control_allow_origin: Some(["nothing".to_string()].to_vec()),
            ..Default::default()
        }
    }

    fn swish_swish() -> Swish {
        let mut swish = Swish::new();
        swish.get("/path", path_handler);
        swish.get("/user/:id", user_id_handler);
        swish.post("/user/register", user_register_handler);
        swish.set_cors_as(defined_like_this());
        swish.set_address("0.0.0.0");
        swish
    }

    #[derive(Deserialize, Serialize)]
    struct Sample {
        code: u16,
        data: String,
    }

    fn path_handler(_: &Request) -> Box<dyn Body> {
        Box::new(Json(Sample {
            code: 200,
            data: "path request".to_string(),
        }))
    }

    fn user_id_handler(req: &Request) -> Box<dyn Body> {
        let param = match &req.param {
            Some(p) => p,
            None => "",
        };
        Box::new(Json(Sample {
            code: 200,
            data: format!("{}{}", "user id is ".to_string(), param),
        }))
    }

    fn user_register_handler(req: &Request) -> Box<dyn Body> {
        let sample: Sample =
            serde_json::from_str(&req.body).expect("fail to convert transaction to json");
        Box::new(Json(Sample {
            code: 200,
            data: format!(
                "success register id: {} msg: {}",
                sample.code.to_string(),
                sample.data
            ),
        }))
    }
}