-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
54 lines (47 loc) · 992 Bytes
/
route.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
package pantsu
import (
"bytes"
"github.com/cornelk/hashmap"
)
type (
Route struct {
Path string
Method string
Handler Handler
Params [][]string
}
//routeMap *hashmap.HashMap
)
func buildRoute(url, method string, handler Handler, middleware ...MiddlewareFunc) Route {
handler = WithMiddlewares(middleware...)(handler)
return Route{
Path: url,
Method: method,
Handler: handler,
}
}
func (mux *Pantsu) addRoute(r Route) {
path := s2b(r.Path)
method := s2b(r.Method)
if !bytes.ContainsRune(path, ':') {
if v, ok := mux.routes.Get(path); !ok {
hm := hashmap.HashMap{}
hm.Set(method, r)
mux.routes.Set(path, &hm)
} else {
hm := v.(*hashmap.HashMap)
hm.Set(method, r)
}
return
}
idx := findPathIndex(r.Path)
routeParams := path[:idx]
if v, ok := mux.routes.Get(routeParams); !ok {
hm := hashmap.HashMap{}
hm.Set(r.Method, r)
mux.routes.Set(routeParams, &hm)
} else {
hm := v.(*hashmap.HashMap)
hm.Set(r.Method, r)
}
}