-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
129 lines (103 loc) · 2.95 KB
/
router.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
package router
import (
"regexp"
"strings"
"github.com/starmanmartin/simple-router/request"
)
const (
mGet string = "get"
mPost string = "post"
mPut string = "put"
mDelete string = "delete"
mAll string = "all"
)
func paramsCopy(src request.Params) request.Params {
dst := make(request.Params, len(src))
for k, v := range src {
dst[k] = v
}
return dst
}
var (
routerList map[string]*routeElement
splitPath *regexp.Regexp
)
func init() {
initTree()
initUpload()
resetRouteing()
splitPath = regexp.MustCompile(`\/\{([^\}]|\}[^\/])+\}\}?`)
}
func resetRouteing() {
routerList = make(map[string]*routeElement)
routerList[mGet] = newRouteElement(mGet, false)
routerList[mPost] = newRouteElement(mPost, false)
routerList[mPut] = newRouteElement(mPut, false)
routerList[mDelete] = newRouteElement(mDelete, false)
routerList[mAll] = newRouteElement(mAll, false)
}
func findListOfHandler(elem *routeElement, path []string, xhr bool) ([]*finalRouteElement, bool) {
list := make(request.Params)
return findListOfHandlerRec(elem, list, path, xhr)
}
func findListOfHandlerRec(elem *routeElement, params request.Params, path []string, xhr bool) (finalHandler []*finalRouteElement, returnOk bool) {
if len(path) == 0 {
return
}
if handlerList, tempHandler, ok := elem.getNext(path[0], len(path) == 1); ok {
finalHandler = make([]*finalRouteElement, 0, len(tempHandler))
for _, tempElem := range tempHandler {
if !tempElem.Xhr || xhr {
tempParams := paramsCopy(params)
if tempElem.isVariable {
tempParams[tempElem.route.variableName()] = path[0]
}
finalHandler = append(finalHandler, &finalRouteElement{tempElem, &tempParams})
}
}
for _, tempElem := range handlerList {
tempParams := paramsCopy(params)
if tempElem.isVariable {
tempParams[tempElem.route.variableName()] = path[0]
}
if tempHandlerNext, okNext := findListOfHandlerRec(tempElem, tempParams, path[1:], xhr); okNext {
finalHandler = append(finalHandler, tempHandlerNext...)
}
}
}
returnOk = len(finalHandler) != 0
sortRoutingList(finalHandler)
return
}
func addNew(route string, method string, handler []HTTPHandler, xhr bool) (*routeElement, bool) {
return addElemToTree(handler, routerList[method], prepareRoute(route), xhr)
}
func prepareRoute(route string) []string {
if route[:1] == "/" {
route = route[1:]
}
firstSplit := splitPath.Split(route, -1)
regSplit := splitPath.FindAllString(route, -1)
if len(firstSplit) > 0 && firstSplit[len(firstSplit)-1] == "" {
firstSplit = firstSplit[:len(firstSplit)-1]
}
var finaleRoute []string
for i, rp := range firstSplit {
if len(rp) > 0 {
if rp[:1] == "/" {
rp = rp[1:]
}
finaleRoute = append(finaleRoute, strings.Split(rp, "/")...)
}
if i < len(regSplit) {
if regSplit[i][:1] == "/" {
regSplit[i] = regSplit[i][1:]
}
finaleRoute = append(finaleRoute, regSplit[i])
}
}
if len(finaleRoute) == 0 {
return []string{""}
}
return finaleRoute
}