-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
183 lines (155 loc) · 4.78 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
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
package http
import (
"fmt"
"html/template"
"io"
"net/http"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-logr/logr"
"github.com/unrolled/render"
)
type ResponseWriter interface {
http.ResponseWriter
R() Request
// render functions
TemplateLookup(t string) *template.Template
Render(e render.Engine, data interface{})
Data(status int, v []byte)
HTML(status int, name string, binding interface{}, htmlOpt ...render.HTMLOptions)
JSON(status int, v interface{})
JSONP(status int, callback string, v interface{})
Text(status int, v string)
XML(status int, v interface{})
Error(status int, contents ...interface{})
// extended ResponseWriter methods
Written() bool
// Status returns the HTTP status of the request, or 0 if one has not
// yet been sent.
Status() int
// BytesWritten returns the total number of bytes sent to the client.
BytesWritten() int
// API error
APIError(err error) int
// misc render/response functions
Redirect(location string, status ...int)
RedirectToFirst(appURL, appSubURL string, location ...string)
HTMLString(name string, binding interface{}, htmlOpt ...render.HTMLOptions) (string, error)
ServeContent(name string, r io.ReadSeeker, params ...interface{})
ServeFile(file string, names ...string)
}
type response struct {
http.ResponseWriter
req Request
r *render.Render
log logr.Logger
}
var _ ResponseWriter = &response{}
func NewResponseWriter(w http.ResponseWriter, req *http.Request, r *render.Render, sink logr.LogSink) ResponseWriter {
return &response{
ResponseWriter: w,
req: &request{req: req},
r: r,
log: logr.New(sink),
}
}
func (w *response) R() Request {
return w.req
}
func (w *response) TemplateLookup(t string) *template.Template {
return w.r.TemplateLookup(t)
}
func (w *response) Render(e render.Engine, data interface{}) {
if err := w.r.Render(w, e, data); err != nil {
if log := middleware.GetLogEntry(w.R().Request()); log != nil {
log.Panic(err.Error(), nil)
}
}
}
func (w *response) Data(status int, v []byte) {
if err := w.r.Data(w, status, v); err != nil {
if log := middleware.GetLogEntry(w.R().Request()); log != nil {
log.Panic(err.Error(), nil)
}
}
}
func (w *response) HTML(status int, name string, binding interface{}, htmlOpt ...render.HTMLOptions) {
if err := w.r.HTML(w, status, name, binding, htmlOpt...); err != nil {
if log := middleware.GetLogEntry(w.R().Request()); log != nil {
log.Panic(err.Error(), nil)
}
}
}
func (w *response) JSON(status int, v interface{}) {
if err := w.r.JSON(w, status, v); err != nil {
if log := middleware.GetLogEntry(w.R().Request()); log != nil {
log.Panic(err.Error(), nil)
}
}
}
func (w *response) JSONP(status int, callback string, v interface{}) {
if err := w.r.JSONP(w, status, callback, v); err != nil {
if log := middleware.GetLogEntry(w.R().Request()); log != nil {
log.Panic(err.Error(), nil)
}
}
}
func (w *response) Text(status int, v string) {
if err := w.r.Text(w, status, v); err != nil {
if log := middleware.GetLogEntry(w.R().Request()); log != nil {
log.Panic(err.Error(), nil)
}
}
}
func (w *response) XML(status int, v interface{}) {
if err := w.r.XML(w, status, v); err != nil {
if log := middleware.GetLogEntry(w.R().Request()); log != nil {
log.Panic(err.Error(), nil)
}
}
}
// Error if length of contents is more than 1, then the
// first content will be considered as title and the
// second content will be considered as the error
func (w *response) Error(status int, contents ...interface{}) {
v := http.StatusText(status)
var title string
var obj interface{}
if len(contents) > 1 {
title = fmt.Sprintf("%v", contents[0])
obj = contents[1]
} else if len(contents) > 0 {
obj = contents[0]
}
if err, ok := obj.(error); ok {
v = err.Error()
} else {
v = fmt.Sprintf("%v", obj)
}
if len(title) > 0 && w.log.GetSink() != nil {
// log the error with the title
w.log.Error(fmt.Errorf(v), title)
}
http.Error(w, v, status)
}
// Written returns true if there are something sent to web browser
func (w *response) Written() bool {
if ww, ok := w.ResponseWriter.(middleware.WrapResponseWriter); ok {
return ww.Status() > 0
}
panic("chi: unsupported method, r.Use(middleware.Logger) to implement")
}
// Status returns the HTTP status of the request, or 0 if one has not
// yet been sent.
func (w *response) Status() int {
if ww, ok := w.ResponseWriter.(middleware.WrapResponseWriter); ok {
return ww.Status()
}
panic("chi: unsupported method, r.Use(middleware.Logger) to implement")
}
// BytesWritten returns the total number of bytes sent to the client.
func (w *response) BytesWritten() int {
if ww, ok := w.ResponseWriter.(middleware.WrapResponseWriter); ok {
return ww.BytesWritten()
}
panic("chi: unsupported method, r.Use(middleware.Logger) to implement")
}