-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
174 lines (144 loc) · 4.39 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
163
164
165
166
167
168
169
170
171
172
173
174
package Sex
import (
str "strings"
re "regexp"
"net/http"
"errors"
"bytes"
"time"
)
// Response to make complete response with Cookies, Headers, and all http.ResponseWrite another features
type Response struct {
http.ResponseWriter
Body []byte
Status int
}
// Request properties sent by client (*http.Request) with inproviments like path variables and Pistol Route configurations
// Example:
// router.Add("/hello/{name}", func (r Sex.Request) string {
// name := r.PathVars["name"]
// return "Hello "+ name
// }
type Request struct {
*http.Request
PathVars map[string]string
Conf Prop
Writer *Response
}
// Request function to write Json body on a variable
// Example:
// var data map[string]interface{} // Can be Structs too
// r.JsonBody(&data)
func (self *Request) JsonBody(v interface{}) error {
encoded := new(bytes.Buffer)
encoded.ReadFrom(self.Body)
return FromJson(encoded.Bytes(), v)
}
// Request function to write []byte body on a variable
// Example:
// var data []byte
// r.RawBody(&data)
func (self *Request) RawBody(b *[]byte) error {
body := new(bytes.Buffer)
_, err := body.ReadFrom(self.Body)
*b = body.Bytes()
return err
}
// Request function to get Response Writer
func (self *Request) MkResponse() *Response {
return self.Writer
}
// Function to set Response status code
func (r *Response) WriteHeader(status int) {
r.SetStatus(status)
}
// Function to set Response body
func (self *Response) SetBody(v []byte) *Response {
self.Body = v
return self
}
// Function to set Response status code
func (self *Response) SetStatus(code int) *Response {
self.Status = 200
return self
}
// Function to set Response cookies
func (self *Response) SetCookie(key string, value string, expires time.Duration) *Response {
cookie := &http.Cookie {
Name: key,
Value: value,
Expires: time.Now().Add(expires),
}
http.SetCookie(self, cookie)
return self
}
// Function to get regex pattern of a Sex path template
// Example:
// Sex.GetPathPattern("/hello/{name}")
func GetPathPattern(t string) string {
var_patt := re.MustCompile(`\{(\w{1,}):{0,1}(.{0,})\}`)
path_tmplt := str.Split(t, "/")
path_pattern := "/"
for i := 0; i < len(path_tmplt); i++ {
if path_tmplt[i] == "" {
continue
}
path_tmplt[i] += "/"
values := var_patt.FindStringSubmatch(path_tmplt[i])
if len(values)>=2 {
if values[2] == "" {
path_pattern += `[a-zA-Z0-9_\ ]{1,}`
} else {
path_pattern += values[2]
}
} else {
path_pattern += path_tmplt[i]
}
}
path_pattern = fixPath(path_pattern)
path_pattern += "$"
if path_pattern == "^$" {
path_pattern = "^/$"
}
return path_pattern
}
// Function to get variables of a path using a Sex path template
// Example:
// Sex.GetPathVars("/hello/{name}", "/hello/joao")
func GetPathVars(t string, p string) (map[string]string, error) {
var_patt := re.MustCompile(`\{(\w{1,}):{0,1}(.{0,})\}`)
path_tmplt := str.Split(t, "/")
path := str.Split(p, "/")
if len(path) != len(path_tmplt) {
return map[string]string {}, errors.New("Path don't match with the path template")
}
path_vars_values := []map[string]string{}
for i := 0; i < len(path); i++ {
values := var_patt.FindStringSubmatch(path_tmplt[i])
tmpl_patt := re.MustCompile("")
if len(values)==3 {
tmpl_patt = re.MustCompile(values[2])
if tmpl_patt.MatchString(path[i]){
path_vars_values = append(path_vars_values, map[string]string {
"name": values[1],
"value": path[i],
})
} else {
return map[string]string {}, errors.New(
Fmt("Variable \"%s\" need that \"%s\" match to \"%s\"", values[1], path[i], values[2]),
)
}
continue
}
path_vars_values = append(path_vars_values, map[string]string{
"value": path[i],
})
}
path_vars := map[string]string{}
for _, v := range path_vars_values {
if _, exist := v["name"]; exist {
path_vars[v["name"]] = v["value"]
}
}
return path_vars, nil
}