-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
73 lines (60 loc) · 1.65 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
package multiplexer
import (
"errors"
)
// Router struct is router
// that will help to routing every incoming request.
// This router used tree data structure to do request routin
type Router struct {
prefix string
saver saveInformation
tree *tree
}
type saveInformation struct {
methods []string
}
// NewRouter will return *Router.
func NewRouter(basepath string) *Router {
tree := newTree(basepath)
return &Router{
prefix: "",
tree: tree,
saver: saveInformation{},
}
}
// SetPrefix will set prefix for Router object.
// Every path will be start with prefix to access it
// example :
// the original path --> /id
// with prefix = "product" => product/id
func (rt *Router) SetPrefix(prefix string) *Router {
if prefix == "" || prefix == "/" {
return nil
}
rt.prefix = prefix
if rt.tree.root.label == "" {
rt.tree.root.label = prefix
} else {
currentTree := rt.tree.root
newNode := newNode(prefix)
newNode.child[currentTree.label] = currentTree
rt.tree.root = newNode
}
return rt
}
// Set every http method that will assign to the path
// that will be added into Router object
func (rt *Router) Methods(methods ...string) *Router {
for _, method := range methods {
rt.saver.methods = append(rt.saver.methods, method)
}
return rt
}
// Function SubRouter will add sub router to main router
func (rt *Router) SubRouter(router *Router) error {
if rt.tree.root.label == router.tree.root.label {
return errors.New("Router and SubRouter has same root path, make sure router and subrouter has different root path")
}
rt.tree.root.child[router.tree.root.label] = router.tree.root
return nil
}