-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
185 lines (156 loc) · 4.45 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
package fmx
import (
"net/http"
"strings"
"github.com/CloudyKit/router"
)
//Router http router
type Router struct {
handlers []HandlerFunc
engine *Engine
}
//Use register middleware
//these middlewares are combined and added to Router
func (r *Router) Use(middlewares ...HandlerFunc) *Router {
r.handlers = append(r.handlers, middlewares...)
return r
}
//use fn to handle http 404
func (r *Router) UseNotFound(fn HandlerFunc) {
r.Use(func(c *Context) {
originWriter := c.Writer
h := ¬found{Realwriter: c.Writer, Is404: false}
c.Writer = NewWriter(h)
c.Next()
c.Writer = originWriter
if h.Is404 && fn != nil {
fn(c)
}
})
}
//GET handle GET method
func (r *Router) GET(path string, handlers ...HandlerFunc) {
r.Handle("GET", path, handlers)
}
//POST handle POST method
func (r *Router) POST(path string, handlers ...HandlerFunc) {
r.Handle("POST", path, handlers)
}
//PATCH handle PATCH method
func (r *Router) PATCH(path string, handlers ...HandlerFunc) {
r.Handle("PATCH", path, handlers)
}
//PUT handle PUT method
func (r *Router) PUT(path string, handlers ...HandlerFunc) {
r.Handle("PUT", path, handlers)
}
//DELETE handle DELETE method
func (r *Router) DELETE(path string, handlers ...HandlerFunc) {
r.Handle("DELETE", path, handlers)
}
//HEAD handle HEAD method
func (r *Router) HEAD(path string, handlers ...HandlerFunc) {
r.Handle("HEAD", path, handlers)
}
//OPTIONS handle OPTIONS method
func (r *Router) OPTIONS(path string, handlers ...HandlerFunc) {
r.Handle("OPTIONS", path, handlers)
}
//TRACE handle TRACE method
func (r *Router) TRACE(path string, handlers ...HandlerFunc) {
r.Handle("TRACE", path, handlers)
}
//CONNECT handle CONNECT method
func (r *Router) CONNECT(path string, handlers ...HandlerFunc) {
r.Handle("CONNECT", path, handlers)
}
// Any registers a route that matches all the HTTP methods.
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE
func (r *Router) Any(path string, handlers ...HandlerFunc) {
r.Handle("GET", path, handlers)
r.Handle("POST", path, handlers)
r.Handle("PUT", path, handlers)
r.Handle("PATCH", path, handlers)
r.Handle("HEAD", path, handlers)
r.Handle("OPTIONS", path, handlers)
r.Handle("DELETE", path, handlers)
r.Handle("CONNECT", path, handlers)
r.Handle("TRACE", path, handlers)
}
//Group group route
func (r *Router) Group(handlers ...HandlerFunc) *Router {
handlers = r.combineHandlers(handlers)
return &Router{
handlers: handlers,
engine: r.engine,
}
}
//Handle handle with specific method
func (r *Router) Handle(method, path string, handlers []HandlerFunc) {
handlers = r.combineHandlers(handlers)
r.engine.httprouter.AddRoute(method, path, func(w http.ResponseWriter, req *http.Request, params router.Parameter) {
c := r.engine.createContext(w, req)
c.params = ¶ms
c.handlers = handlers
c.Next()
c.Keys = nil
c._errs = c._errs[:0]
c._logs = c._logs[:0]
r.engine.pool.Put(c)
})
}
func (r *Router) ServeDir(prefix string, dir string) {
if !strings.HasSuffix(prefix, "/") {
prefix = prefix + "/"
}
cutPrefixhandler := http.StripPrefix(prefix, FileServer2(http.Dir(dir)))
h := func(c *Context) {
cutPrefixhandler.ServeHTTP(c.Writer, c.Request)
}
handlers := r.combineHandlers([]HandlerFunc{HandlerFunc(h)})
finalHandler := func(w http.ResponseWriter, req *http.Request, param router.Parameter) {
c := r.engine.createContext(w, req)
c.params = ¶m
c.handlers = handlers
c.Next()
c.Keys = nil
c._errs = c._errs[:0]
c._logs = c._logs[:0]
r.engine.pool.Put(c)
}
r.engine.httprouter.AddRoute("GET", prefix+"*filepath", finalHandler)
r.engine.httprouter.AddRoute("HEAD", prefix+"*filepath", finalHandler)
return
}
func (r *Router) combineHandlers(handlers []HandlerFunc) []HandlerFunc {
h := make([]HandlerFunc, 0, len(r.handlers)+len(handlers))
h = append(h, r.handlers...)
for _, fn := range handlers {
if fn != nil {
h = append(h, fn)
}
}
return h
}
// Conforms to the http.Handler interface.
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
handler, variables := r.engine.httprouter.FindRoute(req.Method, req.URL.Path)
if handler != nil {
handler(w, req, variables)
return
}
//handle not found
h := make([]HandlerFunc, 0, len(r.handlers)+1)
h = append(h, r.handlers...)
h = append(h, func(c *Context) {
c.String(404, "Not Found")
})
c := r.engine.createContext(w, req)
c.params = nil
c.handlers = h
c.Next()
c.Keys = nil
c._errs = c._errs[:0]
c._logs = c._logs[:0]
r.engine.pool.Put(c)
}