-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathresponder.mbt
More file actions
99 lines (80 loc) · 2.21 KB
/
responder.mbt
File metadata and controls
99 lines (80 loc) · 2.21 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
///|
pub(open) trait Responder {
options(Self, res : HttpResponse) -> Unit
output(Self, buf : @buffer.Buffer) -> Unit
}
///|
pub impl Responder for &ToJson with options(_, res) -> Unit {
res.headers["Content-Type"] = "application/json; charset=utf-8"
}
///|
pub impl Responder for &ToJson with output(self, buf) -> Unit {
buf.write_bytes(@utf8.encode(self.to_json().stringify()))
}
///|
pub impl Responder for HttpRequest with options(self, res) -> Unit {
res.headers.merge_in_place(self.headers)
}
///|
pub impl Responder for HttpRequest with output(self, buf) -> Unit {
buf.write_bytes(self.raw_body)
}
///|
pub impl Responder for HttpResponse with options(self, res) -> Unit {
res.status_code = self.status_code
res.headers.merge_in_place(self.headers)
}
///|
pub impl Responder for HttpResponse with output(self, buf) -> Unit {
buf.write_bytes(self.raw_body)
}
///|
pub impl Responder for Json with options(_, res) -> Unit {
res.headers["Content-Type"] = "application/json; charset=utf-8"
}
///|
pub impl Responder for Json with output(self, buf) -> Unit {
buf.write_bytes(@utf8.encode(self.to_json().stringify()))
}
///|
pub impl Responder for Bytes with options(_, res) -> Unit {
res.headers["Content-Type"] = "application/octet-stream"
}
///|
pub impl Responder for Bytes with output(self, buf) -> Unit {
buf.write_bytes(self)
}
///|
pub impl Responder for String with options(_, res) -> Unit {
res.headers["Content-Type"] = "text/plain; charset=utf-8"
}
///|
pub impl Responder for String with output(self, buf) -> Unit {
buf.write_bytes(@utf8.encode(self))
}
///|
pub impl Responder for StringView with options(_, res) -> Unit {
res.headers["Content-Type"] = "text/plain; charset=utf-8"
}
///|
pub impl Responder for StringView with output(self, buf) -> Unit {
buf.write_bytes(@utf8.encode(self))
}
///|
struct Html(String)
///|
pub impl Responder for Html with options(_, res) -> Unit {
res.headers["Content-Type"] = "text/html; charset=utf-8"
}
///|
pub impl Responder for Html with output(self, buf) -> Unit {
buf.write_bytes(@utf8.encode(self.0))
}
///|
pub fn html(html : &Show) -> &Responder {
Html(html.to_string())
}
///|
pub fn text(text : &Show) -> &Responder {
text.to_string()
}