-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
126 lines (110 loc) · 2.91 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package just
import (
"encoding/json"
"encoding/xml"
"net/http"
)
const (
ContentTypeHeaderKey = "Content-Type"
StrongRedirectHeaderKey = "__StrongRedirect"
ServeFileHeaderKey = "__ServeFilePath"
)
// Response interface.
type IResponse interface {
// Checkers
HasData() bool
HasHeaders() bool
HasStreamHandler() bool
// Getters
GetData() []byte
GetStatus() int
GetHeaders() map[string]string
GetStreamHandler() (http.HandlerFunc, bool)
}
// Base Response struct.
type Response struct {
Status int // HTTP status (200, 201,...).
Bytes []byte // Data bytes.
Headers map[string]string // Response headers.
Stream http.HandlerFunc // Stream method, to support standard HTTP package, as well as to work with WS.
}
func (r Response) HasStreamHandler() bool {
return r.Stream != nil
}
func (r Response) GetStreamHandler() (http.HandlerFunc, bool) {
if r.Stream != nil {
return r.Stream, true
}
return nil, false
}
func (r Response) HasData() bool {
if r.Bytes == nil {
return false
}
return len(r.Bytes) > 0
}
func (r Response) GetStatus() int {
return r.Status
}
func (r Response) GetData() []byte {
return r.Bytes
}
func (r Response) HasHeaders() bool {
if r.Headers == nil {
return false
}
return len(r.Headers) > 0
}
func (r *Response) GetHeaders() map[string]string {
if r.Headers == nil {
r.Headers = make(map[string]string)
}
return r.Headers
}
// Create a stream response.
func StreamResponse(handler http.HandlerFunc) IResponse {
return &Response{Bytes: nil, Status: -1, Headers: nil, Stream: handler}
}
// Create a JSON response.
func JsonResponse(status int, v interface{}) IResponse {
b, err := json.Marshal(v)
if err != nil {
return &Response{
Bytes: []byte(err.Error()),
Status: 500,
Headers: map[string]string{ContentTypeHeaderKey: "plain/text; charset=utf-8"},
}
}
return &Response{
Bytes: b,
Status: status,
Headers: map[string]string{ContentTypeHeaderKey: "application/json; charset=utf-8"},
}
}
// Create a redirect response (Use _StrongRedirect in header to set location)
func RedirectResponse(status int, location string) IResponse {
if (status < 300 || status > 308) && status != 201 {
status = 301
}
return &Response{Bytes: nil, Status: status, Headers: map[string]string{StrongRedirectHeaderKey: location}}
}
// Create a XML response.
func XmlResponse(status int, v interface{}) IResponse {
b, err := xml.Marshal(v)
if err != nil {
return &Response{
Bytes: []byte(err.Error()),
Status: 500,
Headers: map[string]string{ContentTypeHeaderKey: "plain/text; charset=utf-8"},
}
}
return &Response{
Bytes: b,
Status: status,
Headers: map[string]string{ContentTypeHeaderKey: "application/xml; charset=utf-8"},
}
}
// Create a response in the form of a local file.
func FileResponse(filePath string) IResponse {
return &Response{Bytes: nil, Status: -1, Headers: map[string]string{ServeFileHeaderKey: filePath}}
}