-
Notifications
You must be signed in to change notification settings - Fork 4
/
server_response.go
80 lines (65 loc) · 1.5 KB
/
server_response.go
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
package porthos
import (
"encoding/json"
)
// Response represents a rpc response.
type Response interface {
// JSON sets the content of the response as JSON data.
JSON(int32, interface{})
// Raw sets the content of the response as an array of bytes.
Raw(int32, string, []byte)
// Empty leaves the content of the response as empty.
Empty(int32)
// GetHeaders returns the response headers.
GetHeaders() *Headers
// GetStatusCode returns the response status.
GetStatusCode() int32
GetBody() []byte
GetContentType() string
}
type response struct {
body []byte
contentType string
statusCode int32
headers *Headers
}
func newResponse() Response {
return &response{
headers: NewHeaders(),
}
}
func (r *response) JSON(statusCode int32, body interface{}) {
if body == nil {
panic("Response body is empty")
}
jsonBody, err := json.Marshal(&body)
if err != nil {
panic(err)
}
r.statusCode = statusCode
r.body = jsonBody
r.contentType = "application/json"
}
func (r *response) Raw(statusCode int32, contentType string, body []byte) {
if body == nil {
panic("Response body is empty")
}
r.statusCode = statusCode
r.body = body
r.contentType = contentType
}
func (r *response) Empty(statusCode int32) {
r.statusCode = statusCode
}
func (r *response) GetHeaders() *Headers {
return r.headers
}
func (r *response) GetStatusCode() int32 {
return r.statusCode
}
func (r *response) GetBody() []byte {
return r.body
}
func (r *response) GetContentType() string {
return r.contentType
}