-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
219 lines (181 loc) · 4.96 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package cotton
import (
"context"
"fmt"
"net/http"
"strings"
"text/template"
"time"
"github.com/tonny-zhang/cotton/utils"
)
// HandlerFunc handler func
type HandlerFunc func(ctx *Context)
// Router router struct
type Router struct {
srv *http.Server
prefix string
domain string
hasHandled bool
trees map[string]*tree
middlewares []HandlerFunc
notfoundHandlers []HandlerFunc
groups groupArr
domains map[string]*Router
globalTemplate *template.Template
}
// NewRouter new router
func NewRouter() *Router {
return &Router{
trees: make(map[string]*tree),
domains: make(map[string]*Router),
middlewares: make([]HandlerFunc, 0),
}
}
// Default get default router
func Default() *Router {
router := NewRouter()
router.Use(Recover())
router.Use(Logger())
return router
}
// NotFound custom NotFoundHandler
func (router *Router) NotFound(handler ...HandlerFunc) {
router.notfoundHandlers = handler
}
func (router *Router) addHandleFunc(method, path string, handler HandlerFunc) {
if "" != router.prefix {
path = utils.CleanPath(router.prefix + "/" + path)
}
if !strings.HasPrefix(path, "/") {
panic(fmt.Errorf("[%s] shold absolute path", path))
}
if handler == nil {
panic(fmt.Errorf("%s %s has no handler", method, path))
}
if _, ok := router.trees[method]; !ok {
router.trees[method] = newTree()
}
result := router.trees[method].root.find(path)
if result.node != nil && result.node.isRealNode {
panic(fmt.Errorf("[%s %s] has being setted", method, path))
}
nodeAdded := router.trees[method].add(path, nil)
nodeAdded.middleware = append(nodeAdded.middleware, router.middlewares...)
nodeAdded.middleware = append(nodeAdded.middleware, handler)
router.hasHandled = true
debugPrintRoute(method, router.domain+path, handler)
}
// Get router get method
func (router *Router) Get(path string, handler HandlerFunc) {
router.addHandleFunc(http.MethodGet, path, handler)
}
// Post router post method
func (router *Router) Post(path string, handler HandlerFunc) {
router.addHandleFunc(http.MethodPost, path, handler)
}
// Put router put method
func (router *Router) Put(path string, handler HandlerFunc) {
router.addHandleFunc(http.MethodPut, path, handler)
}
// Options router options method
func (router *Router) Options(path string, handler HandlerFunc) {
router.addHandleFunc(http.MethodOptions, path, handler)
}
// Delete router delete method
func (router *Router) Delete(path string, handler HandlerFunc) {
router.addHandleFunc(http.MethodDelete, path, handler)
}
// Patch router patch method
func (router *Router) Patch(path string, handler HandlerFunc) {
router.addHandleFunc(http.MethodPatch, path, handler)
}
// Head router head method
func (router *Router) Head(path string, handler HandlerFunc) {
router.addHandleFunc(http.MethodHead, path, handler)
}
// func (router *Router) PrintTree(method string) {
// router.trees[method].root.print(0)
// }
// func (router *Router) Find(method, path string) {
// if tree, ok := router.trees[method]; ok {
// tree.root.find(path)
// }
// }
// ServeHTTP serve http handler
func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := newContext(w, r, router)
host := strings.Split(r.Host, ":")[0]
if r, ok := router.domains[host]; ok {
router = r
}
r.Method = strings.ToUpper(r.Method)
reqURI := r.URL.Path
if tree, ok := router.trees[r.Method]; ok {
result := tree.root.find(reqURI)
if result.node != nil {
ctx.paramCache = result.params
ctx.handlers = result.node.middleware
ctx.Next()
ctx.destroy()
return
}
}
routerUse := router.matchGroup(reqURI)
if routerUse == nil {
routerUse = router
}
notfoundHandlers := routerUse.notfoundHandlers
middlewares := routerUse.middlewares
if len(notfoundHandlers) > 0 {
ctx.handlers = append(middlewares, notfoundHandlers...)
} else {
ctx.handlers = append(middlewares, func(ctx *Context) {
ctx.NotFound()
})
}
ctx.Next()
ctx.destroy()
}
// Run run for http
func (router *Router) Run(addr string) error {
if addr == "" {
addr = ":5000"
}
for _, r := range router.domains {
var groupsNew []*Router
for _, g := range r.groups {
if g.hasHandled {
groupsNew = append(groupsNew, g)
} else {
debugPrint("group [%s] has no handler, will be discarded", g.prefix)
}
}
r.groups = groupsNew
}
router.sort() // 对group进行排序
debugPrint("Listening and serving HTTP on %s\n", addr)
srv := &http.Server{
Addr: addr,
Handler: router,
}
router.srv = srv
e := srv.ListenAndServe()
return e
}
// Stop stop http service
func (router *Router) Stop(ctx context.Context) (e error) {
if router.srv != nil {
if ctx == nil {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
}
e = router.srv.Shutdown(ctx)
debugPrint("exit http")
}
return
}
// Use use for middleware
func (router *Router) Use(handler ...HandlerFunc) {
router.middlewares = append(router.middlewares, handler...)
}