This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RequestContext.go
598 lines (484 loc) · 15.6 KB
/
RequestContext.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package catu
import (
"bytes"
"fmt"
"html/template"
"io"
"mime/multipart"
"net/http"
"net/url"
"path"
"strconv"
"strings"
"github.com/go-catupiry/catu/helpers"
"github.com/go-catupiry/catu/pagination"
"github.com/go-catupiry/query_parser_to_db"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type RequestContextOpts struct {
EchoContext echo.Context
}
func NewRequestContext(opts *RequestContextOpts) *RequestContext {
app := GetApp()
cfg := app.GetConfiguration()
port := cfg.GetF("PORT", "8080")
protocol := cfg.GetF("PROTOCOL", "http")
domain := cfg.GetF("DOMAIN", "localhost")
ctx := RequestContext{
App: app,
EchoContext: opts.EchoContext,
Protocol: protocol,
Domain: domain,
AppOrigin: cfg.GetF("APP_ORIGIN", protocol+"://"+domain+":"+port),
// Title: "",
Theme: app.GetTheme(),
Layout: app.GetLayout(),
ENV: cfg.GetF("GO_ENV", "development"),
Query: query_parser_to_db.NewQuery(50),
Pager: pagination.NewPager(),
}
// Is a context used on CLIs, not in HTTP request / echo then skip it
if opts.EchoContext == nil || ctx.Request().URL == nil {
return &ctx
}
ctx.Pager.CurrentUrl = ctx.Request().URL.Path
ctx.Pager.Limit, _ = strconv.ParseInt(cfg.GetF("PAGER_LIMIT", "20"), 10, 64)
if opts.EchoContext.Request().Method != "GET" {
return &ctx
}
limitMax, _ := strconv.ParseInt(app.GetConfiguration().GetF("PAGER_LIMIT_MAX", "50"), 10, 64)
rawParams := opts.EchoContext.QueryParams()
filteredParamArray := []string{}
for key, param := range rawParams {
// get limit with max value for security:
if key == "limit" && len(param) == 1 {
queryLimit, err := strconv.ParseInt(param[0], 10, 64)
if err != nil {
logrus.WithFields(logrus.Fields{
"key": key,
"param": param,
}).Error("NewRequestContext invalid query param limit")
continue
}
if queryLimit > 0 && queryLimit < limitMax {
ctx.Pager.Limit = queryLimit
}
}
if key == "page" && len(param) == 1 {
page, _ := strconv.ParseInt(param[0], 10, 64)
ctx.Pager.Page = page
continue
}
ctx.Query.AddQueryParamFromRaw(key, param)
}
if len(filteredParamArray) > 0 {
strings.Join(filteredParamArray[:], ",")
}
return &ctx
}
type RequestContext struct {
EchoContext echo.Context
App App
Protocol string
Domain string
AppOrigin string
Title string
IsAuthenticated bool
AuthenticatedUser UserInterface
// authenticated user role name list
Roles []string
Session SessionData
Widgets map[string]map[string]string
Theme string
Layout string
BodyClass []string
Content template.HTML
Query query_parser_to_db.QueryInterface
Pager *pagination.Pager
ENV string
}
/// --- Start echo.Context overrides
// Request returns `*http.Request`.
func (c *RequestContext) Request() *http.Request {
return c.EchoContext.Request()
}
// SetRequest sets `*http.Request`.
func (c *RequestContext) SetRequest(r *http.Request) {
c.EchoContext.SetRequest(r)
}
// SetResponse sets `*Response`.
func (c *RequestContext) SetResponse(r *echo.Response) {
c.EchoContext.SetResponse(r)
}
// Response returns `*Response`.
func (c *RequestContext) Response() *echo.Response {
return c.EchoContext.Response()
}
// IsTLS returns true if HTTP connection is TLS otherwise false.
func (c *RequestContext) IsTLS() bool {
return c.EchoContext.IsTLS()
}
// IsWebSocket returns true if HTTP connection is WebSocket otherwise false.
func (c *RequestContext) IsWebSocket() bool {
return c.EchoContext.IsWebSocket()
}
// Scheme returns the HTTP protocol scheme, `http` or `https`.
func (c *RequestContext) Scheme() string {
return c.EchoContext.Scheme()
}
// RealIP returns the client's network address based on `X-Forwarded-For`
// or `X-Real-IP` request header.
// The behavior can be configured using `Echo#IPExtractor`.
func (c *RequestContext) RealIP() string {
return c.EchoContext.RealIP()
}
// Path returns the registered path for the handler.
func (c *RequestContext) Path() string {
return c.EchoContext.Path()
}
// SetPath sets the registered path for the handler.
func (c *RequestContext) SetPath(p string) {
c.EchoContext.SetPath(p)
}
// Param returns path parameter by name.
func (c *RequestContext) Param(name string) string {
return c.EchoContext.Param(name)
}
// ParamNames returns path parameter names.
func (c *RequestContext) ParamNames() []string {
return c.EchoContext.ParamNames()
}
// SetParamNames sets path parameter names.
func (c *RequestContext) SetParamNames(names ...string) {
c.EchoContext.SetParamNames(names...)
}
// ParamValues returns path parameter values.
func (c *RequestContext) ParamValues() []string {
return c.EchoContext.ParamValues()
}
// SetParamValues sets path parameter values.
func (c *RequestContext) SetParamValues(values ...string) {
c.EchoContext.SetParamValues(values...)
}
// QueryParam returns the query param for the provided name.
func (c *RequestContext) QueryParam(name string) string {
return c.EchoContext.QueryParam(name)
}
// QueryParams returns the query parameters as `url.Values`.
func (c *RequestContext) QueryParams() url.Values {
return c.EchoContext.QueryParams()
}
// QueryString returns the URL query string.
func (c *RequestContext) QueryString() string {
return c.EchoContext.QueryString()
}
// FormValue returns the form field value for the provided name.
func (c *RequestContext) FormValue(name string) string {
return c.EchoContext.FormValue(name)
}
// FormParams returns the form parameters as `url.Values`.
func (c *RequestContext) FormParams() (url.Values, error) {
return c.EchoContext.FormParams()
}
// FormFile returns the multipart form file for the provided name.
func (c *RequestContext) FormFile(name string) (*multipart.FileHeader, error) {
return c.EchoContext.FormFile(name)
}
// MultipartForm returns the multipart form.
func (c *RequestContext) MultipartForm() (*multipart.Form, error) {
return c.EchoContext.MultipartForm()
}
// Cookie returns the named cookie provided in the request.
func (c *RequestContext) Cookie(name string) (*http.Cookie, error) {
return c.EchoContext.Cookie(name)
}
// SetCookie adds a `Set-Cookie` header in HTTP response.
func (c *RequestContext) SetCookie(cookie *http.Cookie) {
c.EchoContext.SetCookie(cookie)
}
// Cookies returns the HTTP cookies sent with the request.
func (c *RequestContext) Cookies() []*http.Cookie {
return c.EchoContext.Cookies()
}
// Bind binds the request body into provided type `i`. The default binder
// does it based on Content-Type header.
func (c *RequestContext) Bind(i interface{}) error {
return c.EchoContext.Bind(i)
}
// Validate validates provided `i`. It is usually called after `Context#Bind()`.
// Validator must be registered using `Echo#Validator`.
func (c *RequestContext) Validate(i interface{}) error {
return c.EchoContext.Validate(i)
}
// Render renders a template with data and sends a text/html response with status
// code. Renderer must be registered using `Echo.Renderer`.
func (c *RequestContext) Render(code int, name string, data interface{}) error {
return c.EchoContext.Render(code, name, data)
}
// HTML sends an HTTP response with status code.
func (c *RequestContext) HTML(code int, html string) error {
return c.EchoContext.HTML(code, html)
}
// HTMLBlob sends an HTTP blob response with status code.
func (c *RequestContext) HTMLBlob(code int, b []byte) error {
return c.EchoContext.HTMLBlob(code, b)
}
// String sends a string response with status code.
func (c *RequestContext) String(code int, s string) error {
return c.EchoContext.String(code, s)
}
// JSON sends a JSON response with status code.
func (c *RequestContext) JSON(code int, i interface{}) error {
return c.EchoContext.JSON(code, i)
}
// JSONPretty sends a pretty-print JSON with status code.
func (c *RequestContext) JSONPretty(code int, i interface{}, indent string) error {
return c.EchoContext.JSONPretty(code, i, indent)
}
// JSONBlob sends a JSON blob response with status code.
func (c *RequestContext) JSONBlob(code int, b []byte) error {
return c.EchoContext.JSONBlob(code, b)
}
// JSONP sends a JSONP response with status code. It uses `callback` to construct
// the JSONP payload.
func (c *RequestContext) JSONP(code int, callback string, i interface{}) error {
return c.EchoContext.JSONP(code, callback, i)
}
// JSONPBlob sends a JSONP blob response with status code. It uses `callback`
// to construct the JSONP payload.
func (c *RequestContext) JSONPBlob(code int, callback string, b []byte) error {
return c.EchoContext.JSONPBlob(code, callback, b)
}
// XML sends an XML response with status code.
func (c *RequestContext) XML(code int, i interface{}) error {
return c.EchoContext.XML(code, i)
}
// XMLPretty sends a pretty-print XML with status code.
func (c *RequestContext) XMLPretty(code int, i interface{}, indent string) error {
return c.EchoContext.XMLPretty(code, i, indent)
}
// XMLBlob sends an XML blob response with status code.
func (c *RequestContext) XMLBlob(code int, b []byte) error {
return c.EchoContext.XMLBlob(code, b)
}
// Blob sends a blob response with status code and content type.
func (c *RequestContext) Blob(code int, contentType string, b []byte) error {
return c.EchoContext.Blob(code, contentType, b)
}
// Stream sends a streaming response with status code and content type.
func (c *RequestContext) Stream(code int, contentType string, r io.Reader) error {
return c.EchoContext.Stream(code, contentType, r)
}
// File sends a response with the content of the file.
func (c *RequestContext) File(file string) error {
return c.EchoContext.File(file)
}
// Attachment sends a response as attachment, prompting client to save the
// file.
func (c *RequestContext) Attachment(file string, name string) error {
return c.EchoContext.Attachment(file, name)
}
// Inline sends a response as inline, opening the file in the browser.
func (c *RequestContext) Inline(file string, name string) error {
return c.EchoContext.Inline(file, name)
}
// NoContent sends a response with no body and a status code.
func (c *RequestContext) NoContent(code int) error {
return c.EchoContext.NoContent(code)
}
// Redirect redirects the request to a provided URL with status code.
func (c *RequestContext) Redirect(code int, url string) error {
return c.EchoContext.Redirect(code, url)
}
// Error invokes the registered HTTP error handler. Generally used by middleware.
func (c *RequestContext) Error(err error) {
c.EchoContext.Error(err)
}
// Handler returns the matched handler by router.
func (c *RequestContext) Handler() echo.HandlerFunc {
return c.EchoContext.Handler()
}
// SetHandler sets the matched handler by router.
func (c *RequestContext) SetHandler(h echo.HandlerFunc) {
c.EchoContext.SetHandler(h)
}
// Logger returns the `Logger` instance.
func (c *RequestContext) Logger() echo.Logger {
return c.EchoContext.Logger()
}
// Set the logger
func (c *RequestContext) SetLogger(l echo.Logger) {
c.EchoContext.SetLogger(l)
}
// Echo returns the `Echo` instance.
func (c *RequestContext) Echo() *echo.Echo {
return c.EchoContext.Echo()
}
/// --- END echo.Context overrides
// Reset resets the context after request completes. It must be called along
// with `Echo#AcquireContext()` and `Echo#ReleaseContext()`.
// See `Echo#ServeHTTP()`
func (c *RequestContext) Reset(r *http.Request, w http.ResponseWriter) {
c.EchoContext.Reset(r, w)
}
type SessionData struct {
UserID string
}
func (r *RequestContext) Set(name string, value interface{}) {
r.EchoContext.Set(name, value)
}
func (r *RequestContext) Get(name string) interface{} {
return r.EchoContext.Get(name)
}
func (r *RequestContext) GetString(name string) string {
v := r.EchoContext.Get(name)
if v == nil {
return ""
}
return v.(string)
}
// Get value from echo context data in boolean format
func (r *RequestContext) GetBool(name string) bool {
v := r.Get(name)
if v == nil {
return false
}
return v.(bool)
}
// Get data in string map format ([]string) from echo context data
func (r *RequestContext) GetStringMap(name string) []string {
v := r.Get(name)
if v == nil {
return []string{}
}
return v.([]string)
}
func (r *RequestContext) GetTemplateHTML(name string) template.HTML {
v := r.Get(name)
if v == nil {
return template.HTML("")
}
return v.(template.HTML)
}
func (r *RequestContext) RenderPagination(name string) string {
html := ""
return html
}
// Render one template, with support for themes
func (r *RequestContext) RenderTemplate(wr io.Writer, name string, data interface{}) error {
return r.App.GetTemplates().ExecuteTemplate(wr, path.Join(r.Theme, name), data)
}
// Partial - Include and render one template inside other
func (r *RequestContext) Partial(name string, data interface{}) template.HTML {
var htmlBuffer bytes.Buffer
err := r.RenderTemplate(&htmlBuffer, name, data)
if err != nil {
logrus.WithFields(logrus.Fields{
"partialName": name,
"error": fmt.Sprintf("%+v\n", err),
}).Error("catu.Partial error on render partial template")
return template.HTML("")
}
return template.HTML(htmlBuffer.String())
}
// Add a body class string checking if is unique
func (r *RequestContext) AddBodyClass(class string) {
if helpers.SliceContains(r.BodyClass, class) {
return
}
r.BodyClass = append(r.BodyClass, class)
}
// Remove a body class string checking if is unique
func (r *RequestContext) RemoveBodyClass(class string) {
if !helpers.SliceContains(r.BodyClass, class) {
return
}
r.BodyClass = append(r.BodyClass, class)
}
// Get body class as string,
func (r *RequestContext) GetBodyClassText() string {
return strings.Join(r.BodyClass, " ")
}
// Get selected response type
func (r *RequestContext) GetResponseContentType() string {
v := r.GetString("responseContentType")
if v == "" {
return r.Request().Header.Get(echo.HeaderContentType) // default ...
}
return v
}
// Set response type, returns error if the type is invalid
func (r *RequestContext) SetResponseContentType(v string) error {
if v == "" {
return errors.New("RequestContext.SetResponseContentType value should not be empty")
}
r.Set("responseContentType", v)
return nil
}
func (r *RequestContext) GetLimit() int {
return int(r.Pager.Limit)
}
func (r *RequestContext) GetOffset() int {
page := int(r.Pager.Page)
if page < 2 {
return 0
}
limit := int(r.Pager.Limit)
return limit * (page - 1)
}
func (r *RequestContext) ParseQueryFromReq(c echo.Context) error {
return nil
}
func (r *RequestContext) GetAuthenticatedRoles() *[]string {
if r.IsAuthenticated {
return &r.Roles
}
return &[]string{"unAuthenticated"}
}
func (r *RequestContext) SetAuthenticatedUser(user UserInterface) {
r.AuthenticatedUser = user
r.IsAuthenticated = true
}
func (r *RequestContext) SetAuthenticatedUserAndFillRoles(user UserInterface) {
r.SetAuthenticatedUser(user)
r.Roles = user.GetRoles()
r.Roles = append(r.Roles, "authenticated")
}
func (r *RequestContext) Can(permission string) bool {
roles := r.GetAuthenticatedRoles()
return r.App.Can(permission, *roles)
}
func GetQueryIntFromReq(param string, c echo.Context) int {
var err error
var valueInt int
page := c.QueryParam(param)
if page != "" {
valueInt, err = strconv.Atoi(page)
if err != nil {
logrus.WithFields(logrus.Fields{
"path": c.Path(),
"param": param,
"page": page,
}).Warn("NewRequestRequestContext invalid page query param")
}
}
return valueInt
}
func GetQueryInt64FromReq(param string, c echo.Context) int64 {
var err error
var valueInt int64
value := c.QueryParam(param)
if value != "" {
valueInt, err = strconv.ParseInt(value, 10, 64)
if err != nil {
logrus.WithFields(logrus.Fields{
"path": c.Path(),
"param": param,
"value": value,
}).Warn("GetQueryInt64FromReq invalid page query param")
}
}
return valueInt
}