-
Notifications
You must be signed in to change notification settings - Fork 1
/
routergroup.go
123 lines (107 loc) · 3.05 KB
/
routergroup.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
package goa
import (
"bytes"
"sort"
"github.com/lovego/goa/docs"
"github.com/lovego/regex_tree"
)
type RouterGroup struct {
basePath string
handlers []interface{}
routes map[string]*regex_tree.Node
docGroup docs.Group
}
func (g *RouterGroup) Lookup(method, path string) (HandlerFuncs, []string) {
if method == `HEAD` {
method = `GET`
}
tree := g.routes[method]
if tree == nil {
return nil, nil
}
if len(path) > 1 && path[len(path)-1] == '/' {
path = path[:len(path)-1]
}
handlers, params := tree.Lookup(path)
if handlers != nil {
return handlers.(HandlerFuncs), params
}
return nil, nil
}
func (g *RouterGroup) Get(path string, handler interface{}, args ...interface{}) *RouterGroup {
return g.Add("GET", path, handler, args...)
}
func (g *RouterGroup) Post(path string, handler interface{}, args ...interface{}) *RouterGroup {
return g.Add("POST", path, handler, args...)
}
func (g *RouterGroup) GetPost(path string, handler interface{}, args ...interface{}) *RouterGroup {
g.Add("GET", path, handler, args...)
return g.Add("POST", path, handler, args...)
}
func (g *RouterGroup) Put(path string, handler interface{}, args ...interface{}) *RouterGroup {
return g.Add("PUT", path, handler, args...)
}
func (g *RouterGroup) Patch(path string, handler interface{}, args ...interface{}) *RouterGroup {
return g.Add("PATCH", path, handler, args...)
}
func (g *RouterGroup) Delete(path string, handler interface{}, args ...interface{}) *RouterGroup {
return g.Add("DELETE", path, handler, args...)
}
func (g *RouterGroup) String() string {
var buf bytes.Buffer
buf.WriteString("{\n")
if g.basePath != "" {
buf.WriteString(" basePath: " + g.basePath + "\n")
}
buf.WriteString(g.RoutesString())
buf.WriteString("}\n")
return buf.String()
}
func (g *RouterGroup) RoutesString() string {
var buf bytes.Buffer
if len(g.handlers) > 0 {
buf.WriteString(" handlers: " + handlersStringIndent(g.handlers, " ") + "\n")
}
if len(g.routes) > 0 {
buf.WriteString(" routes: {\n")
methods := make([]string, 0, len(g.routes))
for method := range g.routes {
methods = append(methods, method)
}
sort.Strings(methods)
for _, method := range methods {
buf.WriteString(" " + method + ":\n" + g.routes[method].StringIndent(" ") + "\n")
}
buf.WriteString(" }\n")
}
return buf.String()
}
func handlersStringIndent(handlers []interface{}, indent string) string {
if len(handlers) == 0 {
return "[ ]"
}
var buf bytes.Buffer
buf.WriteString("[\n")
for _, h := range handlers {
buf.WriteString(indent + " " + funcName(h) + "\n")
}
buf.WriteString(indent + "]")
return buf.String()
}
type HandlerFuncs []func(*Context)
// to print regex_tree.Node in unit tests.
func (handlers HandlerFuncs) String() string {
return handlers.StringIndent("")
}
func (handlers HandlerFuncs) StringIndent(indent string) string {
if len(handlers) == 0 {
return "[ ]"
}
var buf bytes.Buffer
buf.WriteString("[\n")
for _, h := range handlers {
buf.WriteString(indent + " " + funcName(h) + "\n")
}
buf.WriteString(indent + "]")
return buf.String()
}