-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
242 lines (196 loc) · 9.63 KB
/
engine.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package reply
import (
"net/http"
)
// Writer is used by an Engine to construct replies to HTTP server requests.
type Writer interface {
Error(w http.ResponseWriter, error string, code int)
Reply(w http.ResponseWriter, code int, opts Options) error
}
// Engine provides convenience reply methods by
// wrapping its embedded Writer's Error and Reply.
type Engine struct {
// Debug defines whether error strings encountered in the Writer's Reply
// are sent in responses. If debug is false, the error string will simply
// be the plain text representation of the error code.
Debug bool
// Writer is an interface used to construct replies to HTTP server requests.
Writer
}
// BadRequest replies with HTTP Status 400 Bad Request.
func (e Engine) BadRequest(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
// Unauthorized replies with HTTP Status 401 Unauthorized.
func (e Engine) Unauthorized(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
}
// Forbidden replies with HTTP Status 403 Forbidden.
func (e Engine) Forbidden(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
}
// NotFound replies with HTTP Status 404 Not Found.
func (e Engine) NotFound(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
// MethodNotAllowed replies with HTTP Status 405 Method Not Allowed.
func (e Engine) MethodNotAllowed(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
}
// NotAcceptable replies with HTTP Status 406 Not Acceptable.
func (e Engine) NotAcceptable(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusNotAcceptable), http.StatusNotAcceptable)
}
// ProxyAuthRequired replies with HTTP Status 407 Proxy Authentication Required.
func (e Engine) ProxyAuthRequired(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusProxyAuthRequired), http.StatusProxyAuthRequired)
}
// RequestTimeout replies with HTTP Status 408 Request Timeout.
func (e Engine) RequestTimeout(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusRequestTimeout), http.StatusRequestTimeout)
}
// Conflict replies with HTTP Status 409 Conflict.
func (e Engine) Conflict(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusConflict), http.StatusConflict)
}
// Gone replies with HTTP Status 410 Gone.
func (e Engine) Gone(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusGone), http.StatusGone)
}
// LengthRequired replies with HTTP Status 411 Length Required.
func (e Engine) LengthRequired(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusLengthRequired), http.StatusLengthRequired)
}
// PreconditionFailed replies with HTTP Status 412 Precondition Failed.
func (e Engine) PreconditionFailed(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusPreconditionFailed), http.StatusPreconditionFailed)
}
// RequestEntityTooLarge replies with HTTP Status 413 Request Entity Too Large.
func (e Engine) RequestEntityTooLarge(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge)
}
// RequestURITooLong replies with HTTP Status 414 Request URI Too Long.
func (e Engine) RequestURITooLong(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusRequestURITooLong), http.StatusRequestURITooLong)
}
// UnsupportedMediaType replies with HTTP Status 415 Unsupported Media Type.
func (e Engine) UnsupportedMediaType(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusUnsupportedMediaType), http.StatusUnsupportedMediaType)
}
// RequestedRangeNotSatisfiable replies with HTTP Status 416 Requested Range Not Satisfiable.
func (e Engine) RequestedRangeNotSatisfiable(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusRequestedRangeNotSatisfiable), http.StatusRequestedRangeNotSatisfiable)
}
// ExpectationFailed replies with HTTP Status 417 Expectation Failed.
func (e Engine) ExpectationFailed(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusExpectationFailed), http.StatusExpectationFailed)
}
// Teapot replies with HTTP Status 418 I'm a teapot.
func (e Engine) Teapot(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusTeapot), http.StatusTeapot)
}
// MisdirectedRequest replies with HTTP Status 421 Misdirected Request.
func (e Engine) MisdirectedRequest(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusMisdirectedRequest), http.StatusMisdirectedRequest)
}
// UnprocessableEntity replies with HTTP Status 422 Unprocessable Entity.
func (e Engine) UnprocessableEntity(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusUnprocessableEntity), http.StatusUnprocessableEntity)
}
// Locked replies with HTTP Status 423 Locked.
func (e Engine) Locked(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusLocked), http.StatusLocked)
}
// FailedDependency replies with HTTP Status 424 Failed Dependency.
func (e Engine) FailedDependency(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusFailedDependency), http.StatusFailedDependency)
}
// TooEarly replies with HTTP Status 425 Too Early.
func (e Engine) TooEarly(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusTooEarly), http.StatusTooEarly)
}
// UpgradeRequired replies with HTTP Status 426 Upgrade Required.
func (e Engine) UpgradeRequired(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusUpgradeRequired), http.StatusUpgradeRequired)
}
// PreconditionRequired replies with HTTP Status 428 Precondition Required.
func (e Engine) PreconditionRequired(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusPreconditionRequired), http.StatusPreconditionRequired)
}
// TooManyRequests replies with HTTP Status 429 Too Many Requests.
func (e Engine) TooManyRequests(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
}
// RequestHeaderFieldsTooLarge replies with HTTP Status 431 Request Header Fields Too Large.
func (e Engine) RequestHeaderFieldsTooLarge(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusRequestHeaderFieldsTooLarge), http.StatusRequestHeaderFieldsTooLarge)
}
// UnavailableForLegalReasons replies with HTTP Status 451 Unavailable For Legal Reasons.
func (e Engine) UnavailableForLegalReasons(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusUnavailableForLegalReasons), http.StatusUnavailableForLegalReasons)
}
// InternalServerError replies with HTTP Status 500 Internal Server Error.
func (e Engine) InternalServerError(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
// NotImplemented replies with HTTP Status 501 Not Implemented.
func (e Engine) NotImplemented(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
}
// BadGateway replies with HTTP Status 502 Bad Gateway.
func (e Engine) BadGateway(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusBadGateway), http.StatusBadGateway)
}
// ServiceUnavailable replies with HTTP Status 503 Service Unavailable.
func (e Engine) ServiceUnavailable(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
}
// GatewayTimeout replies with HTTP Status 504 Gateway Timeout.
func (e Engine) GatewayTimeout(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusGatewayTimeout), http.StatusGatewayTimeout)
}
// HTTPVersionNotSupported replies with HTTP Status 505 HTTP Version Not Supported.
func (e Engine) HTTPVersionNotSupported(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusHTTPVersionNotSupported), http.StatusHTTPVersionNotSupported)
}
// VariantAlsoNegotiates replies with HTTP Status 506 Variant Also Negotiates.
func (e Engine) VariantAlsoNegotiates(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusVariantAlsoNegotiates), http.StatusVariantAlsoNegotiates)
}
// InsufficientStorage replies with HTTP Status 507 Insufficient Storage.
func (e Engine) InsufficientStorage(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusInsufficientStorage), http.StatusInsufficientStorage)
}
// LoopDetected replies with HTTP Status 508 Loop Detected.
func (e Engine) LoopDetected(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusLoopDetected), http.StatusLoopDetected)
}
// NetworkAuthenticationRequired replies with HTTP Status 511 Network Authentication Required
func (e Engine) NetworkAuthenticationRequired(w http.ResponseWriter) {
e.Error(w, http.StatusText(http.StatusNetworkAuthenticationRequired), http.StatusNetworkAuthenticationRequired)
}
// ReplyOrError wraps Reply with error debugging. If an error is encountered in
// Reply, the Writer's Error function is triggered. Error essages are replaced
// with 'Internal Server Error' if e.Debug is false.
func (e Engine) ReplyOrError(w http.ResponseWriter, code int, opts Options) {
if err := e.Reply(w, code, opts); err != nil {
code = http.StatusInternalServerError
if !e.Debug {
e.Error(w, http.StatusText(code), code)
} else {
e.Error(w, err.Error(), code)
}
}
}
// OK replies with HTTP 200 Status OK.
func (e Engine) OK(w http.ResponseWriter, opts Options) {
e.ReplyOrError(w, http.StatusOK, opts)
}
// Created replies with HTTP 201 Status Created.
func (e Engine) Created(w http.ResponseWriter, opts Options) {
e.ReplyOrError(w, http.StatusCreated, opts)
}
// NoContent replies with HTTP Status 204 No Content.
func (e Engine) NoContent(w http.ResponseWriter) {
e.ReplyOrError(w, http.StatusNoContent, Options{TemplateKey: "no_content.html"})
}