-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequest.go
162 lines (139 loc) · 3.17 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
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
package fresh
import (
"encoding/json"
"golang.org/x/net/websocket"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
// Request structure
type (
Request interface {
setRouteParam(map[string]string)
IsWS() bool
IsTSL() bool
Auth() string
AuthBearer() string
URL() *url.URL
Method() string
Header() http.Header
JSON(interface{}) error
JSONraw() map[string]interface{}
Form() url.Values
Get() *http.Request
WS() *websocket.Conn
Body() io.ReadCloser
QueryString() string
SetWS(*websocket.Conn)
RouteParam(string) string
FormValue(string) string
QueryParam(string) string
}
request struct {
*context
r *http.Request
ws *websocket.Conn
p map[string]string
}
)
// IsTSL check for a web socket request
func (req *request) IsWS() bool {
h := req.r.Header.Get(Upgrade)
return h == "websocket" || h == "Websocket"
}
// IsTSL check for a tsl request
func (req *request) IsTSL() bool {
if req.r.TLS != nil {
return true
}
return false
}
// Auth return request header authorization
func (req *request) Auth() string {
return req.r.Header.Get("Authorization")
}
// Auth return request header authorization
func (req *request) AuthBearer() string {
token := req.r.Header.Get("Authorization")
split := strings.Split(token, "Bearer ")
if len(split) > 1 {
return split[1]
}
return ""
}
// Set URL parameters
func (req *request) URL() *url.URL {
return req.r.URL
}
// Method current request
func (req *request) Method() string {
return req.r.Method
}
// Get the form from a application/x-www-form-urlencoded request
func (req *request) Form() url.Values {
return req.r.Form
}
// Header return http request header
func (req *request) Header() http.Header {
return req.r.Header
}
// JSON return a json body mapped to a given interface
func (req *request) JSON(i interface{}) error {
err := json.NewDecoder(req.r.Body).Decode(i)
if err != nil {
return err
}
// TODO: handle errors
return nil
}
// JSONraw return a json body mapped to a raw interface
func (req *request) JSONraw() map[string]interface{} {
var raw map[string]interface{}
body, err := ioutil.ReadAll(req.r.Body)
if err != nil {
panic(err)
}
err = json.Unmarshal(body, &raw)
if err != nil {
panic(err)
}
return raw
}
// Request return current http request
func (req *request) Get() *http.Request {
return req.r
}
// IsTSL check for a web socket request
func (req *request) WS() *websocket.Conn {
return req.ws
}
// Get the body from a application/json request
func (req *request) Body() io.ReadCloser {
return req.r.Body
}
// Get the query string
func (req *request) QueryString() string {
return req.r.URL.RawQuery
}
// SetWS used by the current request
func (req *request) SetWS(ws *websocket.Conn) {
req.ws = ws
}
// Get a URL parameter
func (req *request) RouteParam(k string) string {
return req.p[k]
}
// Get the form value by a given key from a application/x-www-form-urlencoded request
func (req *request) FormValue(k string) string {
return req.r.FormValue(k)
}
// Get a query string parameter
func (req *request) QueryParam(k string) string {
return req.r.URL.Query().Get(k)
}
// Set URL parameters
func (req *request) setRouteParam(m map[string]string) {
req.p = m
}