/
context.rs
64 lines (55 loc) · 1.36 KB
/
context.rs
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
use std::collections::{HashMap};
use smallvec::SmallVec;
use thruster::{Context, Request, Response};
pub struct Ctx {
pub body: String,
pub method: String,
pub path: String,
pub request_body: String,
pub params: HashMap<String, String>,
pub headers: SmallVec<[(String, String); 8]>,
pub status_code: u32,
response: Response
}
impl Ctx {
pub fn new(context: Ctx) -> Ctx {
Ctx {
body: context.body,
method: context.method,
path: context.path,
params: context.params,
request_body: context.request_body,
headers: SmallVec::new(),
status_code: 200,
response: Response::new()
}
}
pub fn set_header(&mut self, key: &str, val: &str) {
self.response.header(key, val);
}
}
impl Context for Ctx {
fn get_response(mut self) -> Response {
self.response.status_code(200, "OK");
self.response.body(&self.body);
self.response
}
fn set_body(&mut self, body: String) {
self.body = body;
}
}
pub fn generate_context(request: Request) -> Ctx {
let method = request.method().to_owned();
let path = request.path().to_owned();
let request_body = request.raw_body().to_owned();
Ctx {
body: "".to_owned(),
method: method,
path: path,
params: request.params,
request_body: request_body,
headers: SmallVec::new(),
status_code: 200,
response: Response::new()
}
}