-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
149 lines (131 loc) · 3.52 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
package bytego
import (
"net/http"
"sync"
)
type Router interface {
GET(path string, handlers ...HandlerFunc) Router
POST(path string, handlers ...HandlerFunc) Router
PUT(path string, handlers ...HandlerFunc) Router
DELETE(path string, handlers ...HandlerFunc) Router
HEAD(path string, handlers ...HandlerFunc) Router
PATCH(path string, handlers ...HandlerFunc) Router
OPTIONS(path string, handlers ...HandlerFunc) Router
TRACE(path string, handlers ...HandlerFunc) Router
Handle(method string, path string, handlers ...HandlerFunc) Router
Any(path string, handlers ...HandlerFunc) Router
Static(relativePath, root string) Router
StaticFS(relativePath string, fsys http.FileSystem) Router
StaticFile(relativePath, filePath string) Router
Group(relativePath string, handlers ...HandlerFunc) Router
Use(middlewares ...HandlerFunc)
}
func newRouter() *router {
r := &router{
basePath: "/",
}
r.pool.New = func() interface{} {
return &Ctx{app: r.app}
}
return r
}
type router struct {
basePath string
paramsPool sync.Pool
trees map[string]*node
maxParams uint16
handlers []HandlerFunc
noRouteHandlers []HandlerFunc
allNoRouteHandlers []HandlerFunc
app *App
pool sync.Pool
}
func (r *router) noRoute(handlers ...HandlerFunc) {
r.noRouteHandlers = handlers
r.rebuild404Handlers()
}
func (r *router) rebuild404Handlers() {
r.allNoRouteHandlers = combineHandlers(r.handlers, r.noRouteHandlers)
}
func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ctx := r.pool.Get().(*Ctx)
ctx.reset()
ctx.Request = req
ctx.writer = newResponseWriter(w, r.app)
ctx.Response = ctx.writer
defer r.pool.Put(ctx)
path := req.URL.Path
if root := r.trees[req.Method]; root != nil {
if value := root.getValue(path, r.getParams); value.handlers != nil {
ctx.path = path
ctx.handlers = value.handlers
ctx.routePath = value.fullPath
var err error
if value.params != nil {
ctx.Params = *value.params
err = ctx.Next()
r.putParams(value.params)
} else {
err = ctx.Next()
}
if err != nil {
ctx.HandleError(err)
}
return
}
}
ctx.handlers = r.allNoRouteHandlers
r.serveError(ctx, http.StatusNotFound, default404Body)
}
func (r *router) serveError(c *Ctx, code int, defaultMessage []byte) {
c.writer.status = code
_ = c.Next() //middlewares
if c.Response.Committed() {
return
}
c.Status(code)
_, _ = c.Response.Write(defaultMessage)
}
func (r *router) add(method, path string, handlers ...HandlerFunc) Router {
varsCount := uint16(0)
if method == "" {
panic("method must not be empty")
}
if len(path) < 1 || path[0] != '/' {
panic("path must begin with '/' in path '" + path + "'")
}
if handlers == nil {
panic("handle must not be nil")
}
if r.trees == nil {
r.trees = make(map[string]*node)
}
root := r.trees[method]
if root == nil {
root = new(node)
r.trees[method] = root
}
root.addRoute(path, combineHandlers(r.handlers, handlers)...)
// Update maxParams
if paramsCount := countParams(path); paramsCount+varsCount > r.maxParams {
r.maxParams = paramsCount + varsCount
}
// Lazy-init paramsPool alloc func
if r.paramsPool.New == nil && r.maxParams > 0 {
r.paramsPool.New = func() interface{} {
ps := make(Params, 0, r.maxParams)
return &ps
}
}
return nil
}
func (r *router) getParams() *Params {
ps, _ := r.paramsPool.Get().(*Params)
*ps = (*ps)[0:0] // reset slice
return ps
}
func (r *router) putParams(ps *Params) {
if ps != nil {
r.paramsPool.Put(ps)
}
}