forked from gominima/minima
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
187 lines (164 loc) · 3.65 KB
/
Copy pathrequest.go
File metadata and controls
187 lines (164 loc) · 3.65 KB
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
package minima
import (
"encoding/json"
"mime/multipart"
"net/http"
"net/url"
"strings"
)
/**
@info The request param structure
@property {string} [Path] Route path of the param
@property {string} [key] Key for the param
@property {string} [value] Value of the param
*/
type Param struct {
Path string
key string
value string
}
/**
@info The request structure
@property {*http.Request} [ref] The net/http request instance
@property {multipart.Reader} [fileReader] file reader instance
@property {map[string][]string} [body] Value of the request body
@property {string} [method] Request method
@property {[]*Params} [Params] Request path parameters
@property {query} [url.Values] Request path query params
@property {IncomingHeader} [header] Incoming headers of the request
@property {json.Decoder} [json] Json decoder instance
*/
type Request struct {
ref *http.Request
fileReader *multipart.Reader
body map[string][]string
method string
Params []*Param
query url.Values
header *IncomingHeader
json *json.Decoder
}
/**
@info Make a new default request instance
@param {http.Request} [http.Request] The net/http request instance
@returns {Request}
*/
func request(httpRequest *http.Request) *Request {
req := &Request{
ref: httpRequest,
header: &IncomingHeader{},
fileReader: nil,
method: httpRequest.Proto,
query: httpRequest.URL.Query(),
}
for i, v := range httpRequest.Header {
req.header.Set(strings.ToLower(i), strings.Join(v, ","))
}
if req.header.Get("content-type") == "application/json" {
req.json = json.NewDecoder(httpRequest.Body)
} else {
httpRequest.ParseForm()
}
if len(httpRequest.PostForm) > 0 {
req.body = make(map[string][]string)
for key, value := range httpRequest.PostForm {
req.body[key] = value
}
}
return req
}
/**
@info Gets param from route path
@param {string} [key] Key of the route param
@returns {string}
*/
func (r *Request) GetParam(key string) string {
var value string
pathURL := r.GetPathURL()
for _, v := range r.Params {
if v.Path == pathURL && v.key == key {
value = v.value
break
}
}
return value
}
/**
@info Gets request path url
@returns {string}
*/
func (r *Request) GetPathURL() string {
return r.ref.URL.Path
}
/**
@info Gets raw request body
@returns {map[string][]string}
*/
func (r *Request) Body() map[string][]string {
return r.body
}
/**
@info Gets specified request body
@param {string} [key] Key of the request body
@returns {[]string}
*/
func (r *Request) GetBodyValue(key string) []string {
return r.body[key]
}
/**
@info Gets raw IncomingHeader instance
@returns {IncomingHeader}
*/
func (r *Request) Header() *IncomingHeader {
return r.header
}
/**
@info Gets raw json decoder instance
@returns {json.Decoder}
*/
func (r *Request) Json() *json.Decoder {
return r.json
}
/**
@info Gets method of request
@returns {string}
*/
func (r *Request) Method() string {
return r.method
}
/**
@info Gets raw net/http request instance
@returns {http.Request}
*/
func (r *Request) Raw() *http.Request {
return r.ref
}
/**
@info Gets request path query
@param {string} [key] key of the request query
@returns {string}
*/
func (r *Request) GetQuery(key string) string {
return r.query[key][0]
}
/**
@info Get all the cookies from the request
@returns {[]*http.Cookie}
*/
func (r *Request) Cookies() []*http.Cookie {
return r.ref.Cookies()
}
/**
@info Get a paticular cookie by its name
@param {string} [name] name of the cookie
@returns {*http.Cookie}
*/
func (r *Request) Cookie(name string) *http.Cookie {
var result *http.Cookie
for _, cookie := range r.Cookies() {
if cookie.Name == name {
result = cookie
}
}
return result
}