-
Notifications
You must be signed in to change notification settings - Fork 3
/
router.go
67 lines (55 loc) · 1.74 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
package tingle
// HandlerFunc 注册路由时的闭包
type HandlerFunc func(c *Context) error
// BeforeStartupHandler 前置**启动**handler
// 可以定义一些前置(同步或异步任务或同步+异步)
// 比如异步更新内存缓存
type BeforeStartupHandler func(tingle *Tingle) error
// BeforeRequestHandler 前置请求handler
// 可以定义一些接口请求的前置逻辑(同步或异步任务或同步+异步)
// 比如校验用户是否登陆逻辑
type BeforeRequestHandler func(c *Context) error
// AfterRequestHandler 后置请求handler
// 可以定义一些接口请求的后置逻辑(同步或异步任务或同步+异步) 比如对一致性要求不高的 异步刷新缓存到db
type AfterRequestHandler func(c *Context) error
// tree 路由树
type tree struct {
Method string
Path string
UserHandle Handler
}
// Router 路由结构体
type Router struct {
Trees map[string]*node
BeforeStartupHandles []BeforeStartupHandler
BeforeRequestHandles []BeforeRequestHandler
AfterRequestHandles []AfterRequestHandler
}
// Add 绑定路由
func (router *Router) Add(method string, path string, handlerFunc HandlerFunc) {
root := router.Trees[method]
if root == nil {
root = new(node)
router.Trees[method] = root
}
root.addRoute(path, &TemplateHandler{
handlerFunc: handlerFunc,
})
}
// GetHandler 读取路由
func (router *Router) GetHandler(method, path string) Handler {
t := router.Trees[method]
if t != nil {
return t.getValue(path)
}
return nil
}
// TemplateHandler 注册路由的模板handler
type TemplateHandler struct {
Next
handlerFunc HandlerFunc
}
// Do 模板handler执行注册路由时的业务闭包
func (h *TemplateHandler) Do(c *Context) error {
return h.handlerFunc(c)
}