-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
196 lines (160 loc) · 5.49 KB
/
context.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package router
import (
"encoding/json"
"encoding/xml"
"io/ioutil"
"net/http"
)
// Context holds the HTTP request, the HTTP responseWriter, the Route, and the Route parameters.
type Context interface {
// Route returns the dispatched Route
Route() *Route
// Request returns the HTTP request.
Request() *http.Request
// Response return the HTTP responseWriter.
Response() http.ResponseWriter
// Parameters returns Route parameters.
Parameters() map[string]string
// Parameter returns a router parameter by name.
Parameter(name string) string
// HasParameter checks if router parameter exists.
HasParameter(name string) bool
// URL generates a URL for given route name and actual parameters.
// It returns an empty string if it cannot find any route.
URL(route string, parameters map[string]string) string
// Bytes creates and sends a custom HTTP response.
Bytes(status int, body []byte) error
// Empty creates and sends an HTTP empty response.
Empty(status int) error
// Redirect creates and sends an HTTP redirection response.
Redirect(status int, url string) error
// Text creates and sends an HTTP text response.
Text(status int, body string) error
// HTML creates and sends an HTTP HTML response.
HTML(status int, body string) error
// JSON creates and sends an HTTP JSON response.
JSON(status int, body interface{}) error
// PrettyJSON creates and sends an HTTP JSON (with indents) response.
PrettyJSON(status int, body interface{}) error
// XML creates and sends an HTTP XML response.
XML(status int, body interface{}) error
// PrettyXML creates and sends an HTTP XML (with indents) response.
PrettyXML(status int, body interface{}) error
// File creates and sends an HTTP response that contains a file.
File(status int, contentType, path string) error
}
// DefaultContext is the default implementation of Context interface.
type DefaultContext struct {
route *Route
repository *repository
request *http.Request
rw http.ResponseWriter
parameters map[string]string
}
// Route returns the dispatched Route
func (d *DefaultContext) Route() *Route {
return d.route
}
// Request returns the HTTP request.
func (d *DefaultContext) Request() *http.Request {
return d.request
}
// Response return the HTTP responseWriter.
func (d *DefaultContext) Response() http.ResponseWriter {
return d.rw
}
// Parameters returns Route parameters.
func (d *DefaultContext) Parameters() map[string]string {
return d.parameters
}
// Parameter returns a router parameter by name.
func (d *DefaultContext) Parameter(name string) string {
if value, exist := d.parameters[name]; exist {
return value
}
return ""
}
// HasParameter checks if router parameter exists.
func (d *DefaultContext) HasParameter(name string) bool {
_, exist := d.parameters[name]
return exist
}
// URL generates a URL for given route name and actual parameters.
// It returns an empty string if it cannot find any route.
func (d *DefaultContext) URL(route string, parameters map[string]string) string {
if route := d.repository.findByName(route); route != nil {
return route.URL(parameters)
}
return ""
}
// Bytes creates and sends a custom HTTP response.
func (d *DefaultContext) Bytes(status int, body []byte) error {
d.rw.WriteHeader(status)
_, err := d.rw.Write(body)
return err
}
// Empty creates and sends an HTTP empty response.
func (d *DefaultContext) Empty(status int) error {
d.rw.WriteHeader(status)
return nil
}
// Redirect creates and sends an HTTP redirection response.
func (d *DefaultContext) Redirect(status int, url string) error {
http.Redirect(d.Response(), d.Request(), url, status)
return nil
}
// Text creates and sends an HTTP text response.
func (d *DefaultContext) Text(status int, body string) error {
d.Response().Header().Set("Content-Type", "text/plain")
return d.Bytes(status, []byte(body))
}
// HTML creates and sends an HTTP HTML response.
func (d *DefaultContext) HTML(status int, body string) error {
d.Response().Header().Set("Content-Type", "text/html")
return d.Bytes(status, []byte(body))
}
// JSON creates and sends an HTTP JSON response.
func (d *DefaultContext) JSON(status int, body interface{}) error {
d.Response().Header().Set("Content-Type", "application/json")
bytes, err := json.Marshal(body)
if err != nil {
return err
}
return d.Bytes(status, bytes)
}
// PrettyJSON creates and sends an HTTP JSON (with indents) response.
func (d *DefaultContext) PrettyJSON(status int, body interface{}) error {
d.Response().Header().Set("Content-Type", "application/json")
bytes, err := json.MarshalIndent(body, "", " ")
if err != nil {
return err
}
return d.Bytes(status, bytes)
}
// XML creates and sends an HTTP XML response.
func (d *DefaultContext) XML(status int, body interface{}) error {
d.Response().Header().Set("Content-Type", "application/xml")
bytes, err := xml.MarshalIndent(body, "", "")
if err != nil {
return err
}
return d.Bytes(status, bytes)
}
// PrettyXML creates and sends an HTTP XML (with indents) response.
func (d *DefaultContext) PrettyXML(status int, body interface{}) error {
d.Response().Header().Set("Content-Type", "application/xml")
bytes, err := xml.MarshalIndent(body, "", " ")
if err != nil {
return err
}
return d.Bytes(status, bytes)
}
// File creates and sends an HTTP response that contains a file.
func (d *DefaultContext) File(status int, contentType, path string) error {
content, err := ioutil.ReadFile(path)
if err != nil {
return err
}
d.Response().Header().Set("Content-Type", contentType)
return d.Bytes(status, content)
}