-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
181 lines (146 loc) · 4.21 KB
/
mux.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
package rapid
import (
"fmt"
"net/http"
"strings"
)
func getParams(path string, rPath string) map[string]string {
paramLocations := getParamLocations(path)
requestPath := strings.Split(rPath, "/")
params := map[string]string{}
if len(paramLocations) > 0 {
for i := 0; i < len(requestPath); i++ {
if val, ok := paramLocations[i]; ok {
params[val] = requestPath[i]
}
}
}
return params
}
func getParamLocations(path string) map[int]string {
routePath := strings.Split(path, "/")
params := map[int]string{}
for i := (len(routePath) - 1); i >= 0; i-- {
dir := routePath[i]
if dir != "" && dir[0] == ':' {
params[i] = dir[1:len(routePath[i])]
routePath = append(routePath[:i], routePath[i+1:]...)
}
}
newPath := strings.Join(routePath, "/")
if len(params) > 0 {
newPath += "/"
}
return params
}
type path struct {
path string
method string
subPaths map[string]path
}
type fileServerPath struct {
path string
dir string
}
// AddStaticPath - Add a static file server path
func (s *Server) addStaticPath(path string, dir string) {
if s.fileServerPaths == nil {
s.fileServerPaths = []fileServerPath{}
}
s.fileServerPaths = append(s.fileServerPaths, fileServerPath{path, dir})
}
func (s *Server) staticServerHandler(sPath fileServerPath) func(Connection) {
return func(c Connection) {
dirPath := strings.Replace(c.R.URL.Path, sPath.path, "", 1)
dirPath = sPath.dir + "/" + dirPath
http.ServeFile(c.W, c.R, dirPath)
}
}
// AddPath - Add route to the map of paths
func (s *Server) addPath(pathString string, handler func(Connection), method string) {
pathArr := formatPath(pathString)
if s.paths == nil {
s.paths = make(map[string]path)
s.pathHandlers = make(map[string]map[string]func(Connection))
}
s.insertPath(s.paths, pathArr, 0, method)
if s.pathHandlers[pathString] == nil {
s.pathHandlers[pathString] = make(map[string]func(Connection))
}
if _, ok := s.pathHandlers[pathString][method]; ok {
fmt.Println("Path: " + pathString + " - " + method + " is already registred.")
//os.Exit(0)
}
s.pathHandlers[pathString][method] = handler
}
func formatPath(pathString string) []string {
pathArr := strings.Split(pathString, "/")
//loop := true
for i := 0; i < len(pathArr); i++ {
if i+1 != len(pathArr) {
pathArr[i] += "/"
}
}
return pathArr
}
// Insert a path in to the global paths map
func (s *Server) insertPath(paths map[string]path, pathArr []string, index int, method string) {
singlePath := pathArr[index]
singlePath = checkPathParams(singlePath)
singlePath = strings.ToLower(singlePath)
if _, ok := paths[singlePath]; ok && index+1 < len(pathArr) {
s.insertPath(paths[singlePath].subPaths, pathArr, index+1, method)
} else {
emptySlice := make(map[string]path)
paths[singlePath] = path{strings.Join(pathArr, ""), method, emptySlice}
if index+1 < len(pathArr) {
s.insertPath(paths[singlePath].subPaths, pathArr, index+1, method)
}
}
}
// Check if the current path directory is a url parameter
// If it is a param then replace it with * wild card
func checkPathParams(singlePath string) string {
if len(singlePath) > 1 {
if singlePath[0] == ':' {
if singlePath[len(singlePath)-1] == '/' {
singlePath = "*/"
} else {
singlePath = "*"
}
}
}
return singlePath
}
// FindCorrectPath - Find the correct path to asscociate with a request url
func (s *Server) findCorrectPath(path string, method string) string {
path = strings.ToLower(path)
pathArr := formatPath(path)
currentPath := s.paths
lastMatch := "/"
for i := 0; i < len(pathArr); i++ {
dir := pathArr[i]
if _, ok := currentPath[dir]; ok {
lastMatch = currentPath[dir].path
currentPath = currentPath[dir].subPaths
} else if _, ok := currentPath["*"]; ok {
lastMatch = currentPath["*"].path
currentPath = currentPath["*"].subPaths
} else if _, ok := currentPath["*/"]; ok {
lastMatch = currentPath["*/"].path
currentPath = currentPath["*/"].subPaths
} else {
return "404"
}
}
return lastMatch
}
func (s *Server) findStaticServer(path string) fileServerPath {
for i := 0; i < len(s.fileServerPaths); i++ {
sPath := s.fileServerPaths[i].path
if path[0:len(sPath)] == sPath {
return s.fileServerPaths[i]
}
}
return fileServerPath{"", ""}
}