-
Notifications
You must be signed in to change notification settings - Fork 10
/
response.go
85 lines (71 loc) · 1.81 KB
/
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
81
82
83
84
85
package respond
import (
"encoding/json"
"net/http"
)
// Response is the HTTP response
type Response struct {
Writer http.ResponseWriter
Headers map[string]string
DefMessage bool
}
// DefaultMessageResponse is for transporting a default http message
type DefaultMessageResponse struct {
Status int `json:"status"`
Message string `json:"message"`
}
// NewResponse creates and returns a new response
func NewResponse(w http.ResponseWriter) *Response {
return &Response{
Writer: w,
Headers: map[string]string{
"Content-Type": "application/json; charset=utf-8",
},
}
}
func (resp *Response) DefaultMessage() *Response {
resp.DefMessage = true
return resp
}
// DeleteHeader deletes a single header from the response
func (resp *Response) DeleteHeader(key string) *Response {
resp.Writer.Header().Del(key)
return resp
}
// AddHeader adds a single header to the response
func (resp *Response) AddHeader(key string, value string) *Response {
resp.Writer.Header().Add(key, value)
return resp
}
// WriteResponse writes the HTTP response status, headers and body
func (resp *Response) writeResponse(code int, v interface{}) error {
if len(resp.Headers) > 0 {
resp.writeHeaders()
}
resp.writeStatusCode(code)
if v == nil && resp.DefMessage {
v = DefaultMessageResponse{
Status: code,
Message: http.StatusText(code),
}
}
if v != nil {
body, err := json.Marshal(v)
if err != nil {
panic(err)
}
// can just return an error when connection is hijacked or content-size is longer then declared.
if _, err := resp.Writer.Write(body); err != nil {
panic(err)
}
}
return nil
}
func (resp *Response) writeHeaders() {
for key, value := range resp.Headers {
resp.Writer.Header().Set(key, value)
}
}
func (resp *Response) writeStatusCode(code int) {
resp.Writer.WriteHeader(code)
}