-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
83 lines (65 loc) · 1.82 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
package geh
import (
"encoding/json"
"net/http"
)
type Response struct {
headers map[string]string
responseWriter http.ResponseWriter
status int
data string
}
func NewResponse(responseWriter http.ResponseWriter) Response {
response := Response{}
response.SetResponseWriter(responseWriter)
return response
}
func (response *Response) SetResponseWriter(responseWriter http.ResponseWriter) *Response {
response.responseWriter = responseWriter
return response
}
func (response *Response) SetHeader(key string, value string) *Response {
if response.headers == nil {
response.headers = map[string]string{}
}
response.headers[key] = value
return response
}
func (response *Response) GetHeaders() map[string]string {
return response.headers
}
func (response *Response) GetHeader(key string) string {
return response.GetHeaderOrDefaultValue(key, "")
}
func (response *Response) GetHeaderOrDefaultValue(key string, defaultValue string) string {
if value, ok := response.headers[key]; ok {
return value
}
return defaultValue
}
func (response *Response) SetStatus(status int) *Response {
response.status = status
return response
}
func (response *Response) GetStatus() int {
if response.status == 0 {
response.status = http.StatusOK
}
return response.status
}
func (response *Response) GetData() string {
return response.data
}
func (response *Response) Json(data interface{}) *Response {
responseData, _ := json.Marshal(data)
response.data = string(responseData)
return response.SetHeader("Content-Type", "application/json")
}
func (response *Response) Html(data string) *Response {
response.data = data
return response.SetHeader("Content-Type", "text/html")
}
func (response *Response) Text(data string) *Response {
response.data = data
return response.SetHeader("Content-Type", "plain/text")
}