/
ctx.go
282 lines (243 loc) · 7.96 KB
/
ctx.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package box
import (
"fmt"
"net"
"net/http"
"strings"
_ "unsafe"
"github.com/emicklei/go-restful/v3"
"github.com/gin-gonic/gin/binding"
"github.com/golang-jwt/jwt/v5/request"
"github.com/mpvl/errc"
"github.com/tuotoo/biu/auth"
"github.com/tuotoo/biu/log"
"github.com/tuotoo/biu/param"
)
const (
defaultMaxMemory = 32 << 20 // 32 MB
BiuAttrErr = "__BIU_ERROR__"
BiuAttrErrCode = "__BIU_ERROR_CODE__"
BiuAttrErrMsg = "__BIU_ERROR_MESSAGE__"
BiuAttrErrArgs = "__BIU_ERROR_ARGS__"
BiuAttrRouteID = "__BIU_ROUTE_ID__"
BiuAttrAuthUserID = "__BIU_AUTH_USER_ID__"
BiuAttrEntities = "__BIU_ENTITIES__"
)
const CtxSignature = "github.com/tuotoo/biu/box.Ctx"
// Ctx wrap *restful.Request and *restful.Response in one struct.
type Ctx struct {
*restful.Request
*restful.Response
*restful.FilterChain
ErrCatcher errc.Catcher
Logger log.ILogger
}
// Req returns http.Request of ctx.
func (ctx *Ctx) Req() *http.Request {
return ctx.Request.Request
}
// Resp returns http.ResponseWriter of ctx.
func (ctx *Ctx) Resp() http.ResponseWriter {
return ctx.Response.ResponseWriter
}
// ResponseJSON is a convenience method
// for writing a value wrap in CommonResp as JSON.
func (ctx *Ctx) ResponseJSON(v ...interface{}) {
ctx.SetAttribute(BiuAttrEntities, v)
}
func (ctx *Ctx) Transform(f func(...interface{}) []interface{}) {
if entities, ok := ctx.Attribute(BiuAttrEntities).([]interface{}); ok {
ctx.SetAttribute(BiuAttrEntities, f(entities...))
}
}
// ResponseError is a convenience method to response an error code and message.
func (ctx *Ctx) ResponseError(code int, msg string) {
ctx.SetAttribute(BiuAttrErrCode, code)
ctx.SetAttribute(BiuAttrErrMsg, msg)
}
// RouteID returns the RouteID of current route.
func (ctx *Ctx) RouteID() string {
return ctx.Attribute(BiuAttrRouteID).(string)
}
// RouteSignature returns the signature of current route.
// Example: /v1/user/login POST
func (ctx *Ctx) RouteSignature() string {
return ctx.SelectedRoutePath() + " " + ctx.Req().Method
}
// Redirect replies to the request with a redirect to url.
func (ctx *Ctx) Redirect(url string, code int) {
http.Redirect(ctx.Resp(), ctx.Req(), url, code)
}
// ContainsError is a convenience method to check error is nil.
// If error is nil, it will return false,
// else it will log the error, make a CommonResp response and return true.
// if code is 0, it will use err.Error() as CommonResp.message.
func (ctx *Ctx) ContainsError(err error, code int, v ...interface{}) bool {
if err == nil {
return false
}
ctx.ResponseStdErrCode(code, v...)
return true
}
type errHandler struct {
ctx *Ctx
code int
v []interface{}
}
// Handle implements errc.Handle
func (e errHandler) Handle(s errc.State, err error) error {
vLen := len(e.v)
if vLen > 0 {
if f, ok := e.v[vLen-1].(func()); ok {
f()
e.v = e.v[:vLen-1]
}
}
e.ctx.ResponseStdErrCode(e.code, e.v...)
e.ctx.SetAttribute(BiuAttrErr, err)
return err
}
// Must causes a return from a function if err is not nil.
func (ctx *Ctx) Must(err error, code int, v ...interface{}) {
ctx.ErrCatcher.Must(err, errHandler{ctx: ctx, code: code, v: v})
}
// ResponseStdErrCode is a convenience method response a code
// with msg in Code Desc.
func (ctx *Ctx) ResponseStdErrCode(code int, v ...interface{}) {
ctx.SetAttribute(BiuAttrErrCode, code)
ctx.SetAttribute(BiuAttrErrArgs, v)
}
// UserID returns UserID stored in attribute.
func (ctx *Ctx) UserID() string {
userID, ok := ctx.Attribute(BiuAttrAuthUserID).(string)
if !ok {
return ""
}
return userID
}
// IP returns the IP address of request.
func (ctx *Ctx) IP() string {
ra := ctx.Req().RemoteAddr
if ip := ctx.HeaderParameter("X-Forwarded-For"); ip != "" {
ra = strings.Split(ip, ", ")[0]
} else if ip := ctx.HeaderParameter("X-Real-IP"); ip != "" {
ra = ip
} else {
ra, _, _ = net.SplitHostPort(ra)
}
return ra
}
// Host returns the host of request.
func (ctx *Ctx) Host() string {
if ctx.Req().Host != "" {
if hostPart, _, err := net.SplitHostPort(ctx.Req().Host); err == nil {
return hostPart
}
return ctx.Req().Host
}
return "localhost"
}
// Proxy returns the proxy endpoints behind a request.
func (ctx *Ctx) Proxy() []string {
if ipArr := ctx.HeaderParameter("X-Forwarded-For"); ipArr != "" {
return strings.Split(ipArr, ",")
}
return []string{}
}
// BodyParameterValues returns the array of parameter in a POST form body.
func (ctx *Ctx) BodyParameterValues(name string) ([]string, error) {
if strings.HasPrefix(ctx.Req().Header.Get(restful.HEADER_ContentType), "multipart/form-data") {
err := ctx.Req().ParseMultipartForm(defaultMaxMemory)
if err != nil {
return []string{}, err
}
} else {
err := ctx.Req().ParseForm()
if err != nil {
return []string{}, err
}
}
if vs := ctx.Req().PostForm[name]; len(vs) > 0 {
return vs, nil
}
return []string{}, nil
}
// Query reads query parameter with name.
func (ctx *Ctx) Query(name string) param.Parameter {
return param.NewParameter(ctx.QueryParameters(name), nil)
}
// Form reads form parameter with name.
func (ctx *Ctx) Form(name string) param.Parameter {
return param.NewParameter(ctx.BodyParameterValues(name))
}
// Path reads path parameter with name.
func (ctx *Ctx) Path(name string) param.Parameter {
return param.NewParameter([]string{ctx.PathParameter(name)}, nil)
}
// Header reads header parameter with name.
func (ctx *Ctx) Header(name string) param.Parameter {
return param.NewParameter([]string{ctx.HeaderParameter(name)}, nil)
}
func filterFlags(content string) string {
for i, char := range content {
if char == ' ' || char == ';' {
return content[:i]
}
}
return content
}
// Bind checks the Content-Type to select a binding engine automatically,
// Depending the "Content-Type" header different bindings are used:
//
// "application/json" --> JSON binding
// "application/xml" --> XML binding
//
// otherwise --> returns an error.
// It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
// It decodes the json payload into the struct specified as a pointer.
// It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
func (ctx *Ctx) Bind(obj interface{}) error {
b := binding.Default(ctx.Req().Method, filterFlags(ctx.Request.HeaderParameter("Content-Type")))
return ctx.BindWith(obj, b)
}
// MustBind is a shortcur for ctx.Must(ctx.Bind(obj), code, v...)
func (ctx *Ctx) MustBind(obj interface{}, code int, v ...interface{}) {
ctx.Must(ctx.Bind(obj), code, v...)
}
// BindWith binds the passed struct pointer using the specified binding engine.
// See the binding package.
func (ctx *Ctx) BindWith(obj interface{}, b binding.Binding) error {
return b.Bind(ctx.Req(), obj)
}
// MustBindWith is a shortcur for ctx.Must(ctx.BindWith(obj, b), code, v...)
func (ctx *Ctx) MustBindWith(obj interface{}, b binding.Binding, code int, v ...interface{}) {
ctx.Must(ctx.BindWith(obj, b), code, v...)
}
// BindJSON is a shortcut for ctx.BindWith(obj, binding.JSON).
func (ctx *Ctx) BindJSON(obj interface{}) error {
return ctx.BindWith(obj, binding.JSON)
}
// MustBindJSON is a shortcur for ctx.Must(ctx.BindJSON(obj), code, v...)
func (ctx *Ctx) MustBindJSON(obj interface{}, code int, v ...interface{}) {
ctx.Must(ctx.BindJSON(obj), code, v...)
}
// BindQuery is a shortcut for ctx.BindWith(obj, binding.Query).
func (ctx *Ctx) BindQuery(obj interface{}) error {
return ctx.BindWith(obj, binding.Query)
}
// MustBindQuery is a shortcur for ctx.Must(ctx.BindQuery(obj), code, v...)
func (ctx *Ctx) MustBindQuery(obj interface{}, code int, v ...interface{}) {
ctx.Must(ctx.BindQuery(obj), code, v...)
}
// IsLogin gets JWT token in request by OAuth2Extractor,
// and parse it with CheckToken.
func (ctx *Ctx) IsLogin(ck *auth.Instance) (userID string, err error) {
tokenString, err := request.OAuth2Extractor.ExtractToken(ctx.Req())
if err != nil {
return "", fmt.Errorf("no auth header: %w", err)
}
return ck.CheckToken(tokenString)
}
func (ctx *Ctx) Next() {
ctx.ProcessFilter(ctx.Request, ctx.Response)
}