-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.go
138 lines (114 loc) · 2.84 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
130
131
132
133
134
135
136
137
138
package httprouter
import (
"fmt"
"github.com/makasim/httprouter/radix"
"github.com/savsgio/gotils"
"github.com/valyala/fasthttp"
)
var HandlerKeyUserValue = "fasthttprouter.handler_id"
type Router struct {
PageNotFoundHandler fasthttp.RequestHandler
MethodNotAllowedHandler fasthttp.RequestHandler
GlobalHandler fasthttp.RequestHandler
Handlers map[uint64]fasthttp.RequestHandler
Trees []radix.Tree
}
func New() *Router {
return &Router{
PageNotFoundHandler: func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusNotFound)
},
MethodNotAllowedHandler: func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusMethodNotAllowed)
},
Handlers: make(map[uint64]fasthttp.RequestHandler),
Trees: make([]radix.Tree, 9),
}
}
func (r *Router) Handle(ctx *fasthttp.RequestCtx) {
i := r.methodIndexOf(gotils.B2S(ctx.Method()))
if i == -1 {
r.MethodNotAllowedHandler(ctx)
return
}
hID := r.Trees[i].Search(gotils.B2S(ctx.Path()), func(n string, v interface{}) {
ctx.SetUserValue(n, v)
})
if hID == 0 {
r.PageNotFoundHandler(ctx)
return
}
ctx.SetUserValue(HandlerKeyUserValue, hID)
if h, ok := r.Handlers[hID]; ok {
h(ctx)
return
}
if r.GlobalHandler != nil {
r.GlobalHandler(ctx)
return
}
r.PageNotFoundHandler(ctx)
}
// Add adds a route for method and path to the router
// It is not safe for concurrent use.
// Add routes before using Handle or protect Add, Remove, Handle with mutex.
func (r *Router) Add(method, path string, handlerID uint64) error {
methodIndex := r.methodIndexOf(method)
if methodIndex == -1 {
return fmt.Errorf("method not allowed")
}
if len(path) == 0 {
return fmt.Errorf("path empty")
}
var err error
tree := r.Trees[methodIndex].Clone()
tree, err = tree.Insert(path, handlerID)
if err != nil {
return err
}
r.Trees[methodIndex] = tree
return nil
}
// Remove removes a route for method and path frmo the router
// It is not safe for concurrent use.
// Remove routes before using Handle or protect Add, Remove, Handle with mutex.
func (r *Router) Remove(method, path string) error {
methodIndex := r.methodIndexOf(method)
if methodIndex == -1 {
return fmt.Errorf("method not allowed")
}
if len(path) == 0 {
return fmt.Errorf("path empty")
}
var err error
tree := r.Trees[methodIndex].Clone()
tree, err = tree.Delete(path)
if err != nil {
return err
}
r.Trees[methodIndex] = tree
return nil
}
func (r *Router) methodIndexOf(method string) int {
switch method {
case fasthttp.MethodGet:
return 0
case fasthttp.MethodHead:
return 1
case fasthttp.MethodPost:
return 2
case fasthttp.MethodPut:
return 3
case fasthttp.MethodPatch:
return 4
case fasthttp.MethodDelete:
return 5
case fasthttp.MethodConnect:
return 6
case fasthttp.MethodOptions:
return 7
case fasthttp.MethodTrace:
return 8
}
return -1
}