-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.go
116 lines (103 loc) · 2.47 KB
/
request.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
package http
import (
"github.com/goal-web/contracts"
"github.com/goal-web/supports"
"github.com/goal-web/supports/logs"
"github.com/goal-web/validation"
"github.com/labstack/echo/v4"
"strings"
)
type Request struct {
supports.BaseFields
echo.Context
fields contracts.Fields
}
func NewRequest(ctx echo.Context) contracts.HttpRequest {
var request = &Request{
Context: ctx,
fields: nil,
BaseFields: supports.BaseFields{},
}
request.BaseFields.FieldsProvider = request
request.BaseFields.Getter = request.get
return request
}
func (this *Request) Get(key string) (value interface{}) {
if value = this.Context.Get(key); value != nil && value != "" {
return value
}
if value = this.Context.QueryParam(key); value != nil && value != "" {
return value
}
if value = this.Context.FormValue(key); value != nil && value != "" {
return value
}
if value = this.Context.Param(key); value != nil && value != "" {
return value
}
if file, err := this.Context.FormFile(key); err == nil && file != nil {
return file
}
return
}
func (this *Request) get(key string) (value interface{}) {
if value = this.Get(key); value != nil && value != "" {
return value
}
return this.Fields()[key]
}
func (this *Request) Validate(v interface{}) error {
if err := this.Bind(v); err != nil {
return err
}
return validation.Struct(v)
}
func (this *Request) Fields() contracts.Fields {
if this.fields != nil {
return this.fields
}
var data = make(contracts.Fields)
if strings.Contains(this.Request().Header.Get("Content-Type"), "json") {
var bindErr = this.Context.Bind(&data)
if bindErr != nil {
logs.WithError(bindErr).Debug("http.Request.Fields: bind fields failed")
}
}
for key, query := range this.QueryParams() {
if len(query) == 1 {
data[key] = query[0]
} else {
data[key] = query
}
}
for _, paramName := range this.ParamNames() {
data[paramName] = this.Param(paramName)
}
if form, existsForm := this.FormParams(); existsForm == nil {
for key, values := range form {
if len(values) == 1 {
data[key] = values[0]
} else {
data[key] = values
}
}
}
if multiForm, existsForm := this.MultipartForm(); existsForm == nil {
for key, values := range multiForm.Value {
if len(values) == 1 {
data[key] = values[0]
} else {
data[key] = values
}
}
for key, values := range multiForm.File {
if len(values) == 1 {
data[key] = values[0]
} else {
data[key] = values
}
}
}
this.fields = data
return data
}