-
Notifications
You must be signed in to change notification settings - Fork 0
/
routergroup.go
117 lines (100 loc) · 3.43 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
package gen
import (
"log"
"net/http"
"path"
"reflect"
"runtime"
"strings"
"github.com/julienschmidt/httprouter"
)
type RouterGroup struct {
prefix string
middlewares []HandlerFunc
parent *RouterGroup // for group nesting
engine *Engine // all groups share the same engine, to access its `router`
}
func (g *RouterGroup) Group(prefix string, handlers ...HandlerFunc) *RouterGroup {
engine := g.engine
newGroup := &RouterGroup{
prefix: g.prefix + prefix,
middlewares: handlers,
parent: g,
engine: engine,
}
engine.groups = append(engine.groups, newGroup)
return newGroup
}
func (g *RouterGroup) Use(middlewares ...HandlerFunc) {
g.middlewares = append(g.middlewares, middlewares...)
}
func (g *RouterGroup) addRoute(method, comp string, handlers ...HandlerFunc) {
path_ := g.prefix + comp
len_ := len(handlers)
f := handlers[len_-1]
name := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
log.Printf("%-6s %-25s --> %s\n", method, path_, name)
g.engine.router.Handle(method, path_, func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
c := newContext(w, req, params)
c.engine = g.engine
for _, group := range g.engine.groups {
if strings.HasPrefix(req.URL.Path, group.prefix) {
c.handlers = append(c.handlers, group.middlewares...)
}
}
c.handlers = append(c.handlers, handlers...)
c.Next()
})
}
func (g *RouterGroup) GET(path string, handlers ...HandlerFunc) {
g.addRoute(http.MethodGet, path, handlers...)
}
func (g *RouterGroup) POST(path string, handlers ...HandlerFunc) {
g.addRoute(http.MethodPost, path, handlers...)
}
func (g *RouterGroup) DELETE(path string, handlers ...HandlerFunc) {
g.addRoute(http.MethodDelete, path, handlers...)
}
func (g *RouterGroup) HEAD(path string, handlers ...HandlerFunc) {
g.addRoute(http.MethodHead, path, handlers...)
}
func (g *RouterGroup) PATCH(path string, handlers ...HandlerFunc) {
g.addRoute(http.MethodPatch, path, handlers...)
}
func (g *RouterGroup) CONNECT(path string, handlers ...HandlerFunc) {
g.addRoute(http.MethodConnect, path, handlers...)
}
func (g *RouterGroup) OPTIONS(path string, handlers ...HandlerFunc) {
g.addRoute(http.MethodOptions, path, handlers...)
}
func (g *RouterGroup) TRACE(path string, handlers ...HandlerFunc) {
g.addRoute(http.MethodTrace, path, handlers...)
}
func (g *RouterGroup) Any(path string, handlers ...HandlerFunc) {
g.addRoute(http.MethodGet, path, handlers...)
g.addRoute(http.MethodPost, path, handlers...)
g.addRoute(http.MethodDelete, path, handlers...)
g.addRoute(http.MethodPut, path, handlers...)
g.addRoute(http.MethodHead, path, handlers...)
g.addRoute(http.MethodPatch, path, handlers...)
g.addRoute(http.MethodConnect, path, handlers...)
g.addRoute(http.MethodOptions, path, handlers...)
g.addRoute(http.MethodTrace, path, handlers...)
}
func (g *RouterGroup) createStaticHandler(relativePath string, fs http.FileSystem) HandlerFunc {
absolutePath := path.Join(g.prefix, relativePath)
fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
return func(c *Context) {
filePath := c.Param("filePath")
if _, err := fs.Open(filePath); err != nil {
c.Status(http.StatusNotFound)
return
}
fileServer.ServeHTTP(c.Writer, c.Request)
}
}
func (g *RouterGroup) Static(relativePath, root string) {
handler := g.createStaticHandler(relativePath, http.Dir(root))
urlPath := path.Join(relativePath, "/*filePath")
g.GET(urlPath, handler)
}