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
80
81
use serde::ser::Serialize;
use std::collections::HashMap;
pub const OP_PERFORM_REQUEST: &str = "PerformRequest";
pub const OP_HANDLE_REQUEST: &str = "HandleRequest";
#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct Request {
pub method: String,
pub path: String,
pub query_string: String,
pub header: HashMap<String, String>,
pub body: Vec<u8>,
}
#[derive(Debug, PartialEq, Deserialize, Serialize, Default)]
pub struct Response {
pub status_code: u32,
pub status: String,
pub header: HashMap<String, String>,
pub body: Vec<u8>,
}
impl Response {
pub fn json<T>(payload: T, status_code: u32, status: &str) -> Response
where
T: Serialize,
{
Response {
body: serde_json::to_string(&payload).unwrap().into_bytes(),
header: HashMap::new(),
status: status.to_string(),
status_code,
}
}
pub fn not_found() -> Response {
Response {
status: "Not Found".to_string(),
status_code: 404,
..Default::default()
}
}
pub fn ok() -> Response {
Response {
status: "OK".to_string(),
status_code: 200,
..Default::default()
}
}
pub fn internal_server_error(msg: &str) -> Response {
Response {
status: "Internal Server Error".to_string(),
status_code: 500,
body: msg.to_string().as_bytes().into(),
..Default::default()
}
}
pub fn bad_request() -> Response {
Response {
status: "Bad Request".to_string(),
status_code: 400,
..Default::default()
}
}
}