-
Notifications
You must be signed in to change notification settings - Fork 0
/
reqRoute.go
287 lines (261 loc) · 5.27 KB
/
reqRoute.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
package grrt
// Copyright 2022 GolangToolKits Authors. All rights reserved.
// Use of this source code is governed by the MIT License
// that can be found in the LICENSE file.
import (
"log"
"net/http"
"strings"
)
// ReqRoute ReqRoute
type ReqRoute struct {
handler http.Handler
host string
path string
prefix string
active bool
pathVarsUsed bool
isPrefix bool
pathVarNames *[]string
methods *[]string
}
// New New creates new Route
func (t *ReqRoute) New() Route {
t.pathVarNames = &[]string{}
t.methods = &[]string{}
return t
}
// Handler Handler
func (t *ReqRoute) Handler(handler http.Handler) Route {
if t.active {
t.handler = handler
}
return t
}
// HandlerFunc HandlerFunc
func (t *ReqRoute) HandlerFunc(f func(http.ResponseWriter, *http.Request)) Route {
return t.Handler(http.HandlerFunc(f))
}
// Path Path
func (t *ReqRoute) Path(p string) Route {
if t.chechPath(p) {
t.pathVarNames, t.path = t.extractPathAndVarNames(p)
if len(*t.pathVarNames) > 0 {
t.pathVarsUsed = true
}
t.active = true
}
return t
}
// PathPrefix PathPrefix
func (t *ReqRoute) PathPrefix(px string) Route {
if t.chechPrefix(px) {
t.prefix = px
t.active = true
t.isPrefix = true
}
return t
}
// Methods Methods
func (t *ReqRoute) Methods(ms ...string) Route {
var mts []string
for _, m := range ms {
mts = append(mts, strings.ToUpper(m))
}
t.methods = &mts
return t
}
// GetMethods GetMethods
func (t *ReqRoute) GetMethods() *[]string {
return t.methods
}
// IsMethodAllowed IsMethodAllowed
func (t *ReqRoute) IsMethodAllowed(m string) bool {
var rtn bool
if len(*t.methods) == 0 {
rtn = true
} else {
for _, mt := range *t.methods {
if mt == m {
rtn = true
break
}
}
}
return rtn
}
// IsPathVarsUsed IsPathVarsUsed
func (t *ReqRoute) IsPathVarsUsed() bool {
return t.pathVarsUsed
}
// GetPathVarsCount GetPathVarsCount
func (t *ReqRoute) GetPathVarsCount() int {
var rtn int = 0
if t.pathVarNames != nil {
rtn = len(*t.pathVarNames)
}
return rtn
}
// Host Host --future development----
// func (t *ReqRoute) Host(h string) Route {
// return nil
// }
// GetHandler GetHandler
func (t *ReqRoute) GetHandler() http.Handler {
return t.handler
}
// GetPath GetPath
func (t *ReqRoute) GetPath() string {
return t.path
}
// GetPrefix GetPrefix
func (t *ReqRoute) GetPrefix() string {
return t.prefix
}
// GetVarNames GetVarNames
func (t *ReqRoute) GetVarNames() *[]string {
return t.pathVarNames
}
// IsActive IsActive
func (t *ReqRoute) IsActive() bool {
return t.active
}
func (t *ReqRoute) chechPath(p string) bool {
var rtn = false
if t.chechCurlys(p) && t.chechBackSlash(p) && t.chechCurlyPlacement(p) {
rtn = true
} else {
t.printError(p, "Problem with path")
}
return rtn
}
func (t *ReqRoute) chechCurlys(p string) bool {
var rtn = false
open := rune('{')
closed := rune('}')
var cl int = 0
for _, c := range p {
if c == open {
cl++
} else if c == closed {
cl--
}
}
if cl == 0 {
rtn = true
} else {
t.printError(p, "Mismatched curly brackets")
}
return rtn
}
func (t *ReqRoute) chechCurlyPlacement(p string) bool {
//checks to make sure there are no {{ or }} missmatch
var rtn = true
oc := rune('{')
cc := rune('}')
var lastOc int = 0
var lastCc int = 0
for i, c := range p {
if c == oc && i == lastOc+1 && lastOc != 0 {
t.printError(p, "Route can not have {{")
rtn = false
break
} else if c == oc {
lastOc = i
}
if c == cc && i == lastCc+1 {
t.printError(p, "Route can not have }}")
rtn = false
break
} else if c == cc {
lastCc = i
}
}
return rtn
}
func (t *ReqRoute) chechBackSlash(p string) bool {
var rtn = true
bs := rune('/')
var lastBs int = 0
for i, c := range p {
if i == 0 && c != bs {
rtn = false
t.printError(p, "Route must have leading backslash")
break
} else if i != 0 {
if c == bs && i == lastBs+1 {
rtn = false
t.printError(p, "Misplaced backslash")
break
} else if c == bs && i != len(p)-1 {
lastBs = i
} else if c == bs && i == len(p)-1 {
rtn = false
t.printError(p, "Backslash not allowed on end of route")
}
}
}
return rtn
}
func (t *ReqRoute) chechPrefix(p string) bool {
var rtn = true
bs := rune('/')
var lastBs int = 0
for i, c := range p {
if i == 0 && c != bs {
rtn = false
t.printError(p, "Prefix must have leading backslash")
break
} else if i != 0 {
if c == bs && i == lastBs+1 {
rtn = false
t.printError(p, "Misplaced backslash")
break
} else if c == bs && i != len(p)-1 {
lastBs = i
}
}
}
return rtn
}
func (t *ReqRoute) extractPathAndVarNames(p string) (*[]string, string) {
var vars = []string{}
var pth string
// rtn:= make([]string,1)
oc := rune('{')
cc := rune('}')
var pend int
var ind1 int
var ind2 int
for i, c := range p {
if c == oc {
ind1 = i
if pend == 0 {
pend = i
}
}
if c == cc {
ind2 = i
}
if ind1 > 0 && ind1 < ind2 {
pvar := p[ind1+1 : ind2]
vars = append(vars, pvar)
ind1 = 0
ind2 = 0
}
}
if pend > 0 {
if pth = p[0 : pend-1]; len(pth) == 0 {
pth = p[0:pend]
}
//pth = p[0 : pend-1]
} else {
pth = p
}
return &vars, pth
}
func (t *ReqRoute) printError(p string, cause string) {
log.Println("Path not added to route:")
log.Println(p)
log.Println(cause)
}