-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathresponse.go
119 lines (101 loc) · 2.71 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
package context
import (
"net/http"
)
type Response struct {
// Writer context.ResponseWriter
contentType string
charset string
code int
content string
cookies map[string]*http.Cookie
CookieHandler *Cookie
Header *http.Header
}
// GetContentType sets the Content-Type on the response.
func (r *Response) SetContentType(val string) *Response {
r.contentType = val
return r
}
// GetContentType sets the Charset on the response.
func (r *Response) SetCharset(val string) *Response {
r.charset = val
return r
}
// SetCode sets the status code on the response.
func (r *Response) SetCode(val int) *Response {
r.code = val
return r
}
// SetContent sets the content on the response.
func (r *Response) SetContent(val string) *Response {
r.content = val
return r
}
// GetContentType get the Content-Type on the response.
func (r *Response) GetContentType() string {
return r.contentType
}
// GetContentType get the Charset on the response.
func (r *Response) GetCharset() string {
return r.charset
}
// GetCode get the response status code.
func (r *Response) GetCode() int {
return r.code
}
// GetCode get the response content.
func (r *Response) GetContent() string {
return r.content
}
// Cookie Add a cookie to the response.
func (r *Response) Cookie(name interface{}, params ...interface{}) error {
cookie, err := r.CookieHandler.Set(name, params...)
if err != nil {
if r.cookies == nil {
r.cookies = make(map[string]*http.Cookie)
}
r.cookies[cookie.Name] = cookie
}
return err
}
// Send Sends HTTP headers and content.
func (r *Response) Send(w http.ResponseWriter) {
for _, cookie := range r.cookies {
http.SetCookie(w, cookie)
}
for key, value := range *r.Header {
for _, val := range value {
w.Header().Add(key, val)
}
}
w.Header().Set("Content-Type", r.GetContentType()+";"+" charset="+r.GetCharset())
// r.Header.Write(w)
w.WriteHeader(r.GetCode())
w.Write([]byte(r.GetContent()))
}
// NewResponse Create a new HTTP Response
func NewResponse() *Response {
r := &Response{
Header: &http.Header{},
}
r.SetCode(http.StatusOK)
r.SetContentType("text/html")
r.SetCharset("utf-8")
r.CookieHandler = ParseCookieHandler()
return r
}
// NotFoundResponse Create a new HTTP NotFoundResponse
func NotFoundResponse() *Response {
return NewResponse().SetCode(http.StatusNotFound).SetContent("Not Found")
}
// NotFoundResponse Create a new HTTP Error Response
func ErrorResponse() *Response {
return NewResponse().SetCode(http.StatusInternalServerError).SetContent("Server Error")
}
// Redirect Create a new HTTP Redirect Response
func Redirect(to string) *Response {
r := NewResponse().SetCode(http.StatusMovedPermanently)
r.Header.Set("Location", to)
return r
}