-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
135 lines (111 loc) · 4.11 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
package rough
import (
"log"
"net/http"
"path"
"regexp"
"strings"
)
var (
regEnLetter = regexp.MustCompile("^[A-Z]+$")
anyMethods = []string{
http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch,
http.MethodHead, http.MethodOptions, http.MethodDelete, http.MethodConnect,
http.MethodTrace,
}
)
type RouterGroup struct {
Handlers []HandleFunc
basePath string
engine *Engine
}
func (group *RouterGroup) Group(relativePath string, handlers ...HandleFunc) *RouterGroup {
return &RouterGroup{
Handlers: group.combineHandlers(handlers),
basePath: group.calculateAbsolutePath(relativePath),
engine: group.engine,
}
}
func (group *RouterGroup) combineHandlers(handlers []HandleFunc) []HandleFunc {
finalSize := len(group.Handlers) + len(handlers)
mergedHandlers := make([]HandleFunc, finalSize)
copy(mergedHandlers, group.Handlers)
copy(mergedHandlers[len(group.Handlers):], handlers)
return mergedHandlers
}
func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {
return joinPaths(group.basePath, relativePath)
}
func (group *RouterGroup) Static(relativePath, root string) {
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
panic("URL parameters can not be used when serving a static folder")
}
fs := http.Dir(root)
absolutePath := group.calculateAbsolutePath(relativePath)
fileServer := http.StripPrefix(absolutePath, http.FileServer(fs))
handler := func(c *Context) {
file := c.Param("filepath")
log.Println(file)
f, err := fs.Open(file)
if err != nil {
c.W.WriteHeader(http.StatusNotFound)
c.handlers = group.engine.noRoute
c.index = -1
return
}
f.Close()
fileServer.ServeHTTP(c.W, c.R)
}
urlPattern := path.Join(relativePath, "/*filepath")
group.GET(urlPattern, handler)
group.HEAD(urlPattern, handler)
}
func (group *RouterGroup) Use(middleware ...HandleFunc) {
group.Handlers = append(group.Handlers, middleware...)
}
func (group *RouterGroup) handle(httpMethod, relativePath string, handlers []HandleFunc) {
absolutePath := group.calculateAbsolutePath(relativePath)
handlers = group.combineHandlers(handlers)
group.engine.addRoute(httpMethod, absolutePath, handlers)
}
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandleFunc) {
if matched := regEnLetter.MatchString(httpMethod); !matched {
panic("http method " + httpMethod + " is not valid")
}
group.handle(httpMethod, relativePath, handlers)
}
// POST is a shortcut for router.Handle("POST", path, handlers).
func (group *RouterGroup) POST(relativePath string, handlers ...HandleFunc) {
group.handle(http.MethodPost, relativePath, handlers)
}
// GET is a shortcut for router.Handle("GET", path, handlers).
func (group *RouterGroup) GET(relativePath string, handlers ...HandleFunc) {
group.handle(http.MethodGet, relativePath, handlers)
}
// DELETE is a shortcut for router.Handle("DELETE", path, handlers).
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandleFunc) {
group.handle(http.MethodDelete, relativePath, handlers)
}
// PATCH is a shortcut for router.Handle("PATCH", path, handlers).
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandleFunc) {
group.handle(http.MethodPatch, relativePath, handlers)
}
// PUT is a shortcut for router.Handle("PUT", path, handlers).
func (group *RouterGroup) PUT(relativePath string, handlers ...HandleFunc) {
group.handle(http.MethodPut, relativePath, handlers)
}
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handlers).
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandleFunc) {
group.handle(http.MethodOptions, relativePath, handlers)
}
// HEAD is a shortcut for router.Handle("HEAD", path, handlers).
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandleFunc) {
group.handle(http.MethodHead, relativePath, handlers)
}
// Any registers a route that matches all the HTTP methods.
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
func (group *RouterGroup) Any(relativePath string, handlers ...HandleFunc) {
for _, method := range anyMethods {
group.handle(method, relativePath, handlers)
}
}