-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrouter.go
More file actions
163 lines (154 loc) · 3.61 KB
/
router.go
File metadata and controls
163 lines (154 loc) · 3.61 KB
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
package ghttp
import (
"github.com/julienschmidt/httprouter"
"net/http"
"fmt"
)
const (
strGET = "GET"
strPOST = "POST"
strHEAD = "HEAD"
strOPTIONS = "OPTIONS"
strPUT = "PUT"
strPATCH = "PATCH"
strDELETE = "DELETE"
)
//
type Router struct {
httprouter.Router
optimizationPath func(string) string
routerMap map[string][]*routerStmt // method
}
//
type routerStmt struct {
urlPath string
handleMethod Method
}
//
func (rs *routerStmt) handle(w http.ResponseWriter,r *http.Request,argus httprouter.Params){
var ctx *Context
if customizeW,ok := w.(*ResponseWriter);ok{
ctx = customizeW.getContext()
}
if ctx == nil{
ctx = NewContext(w, r)
}
ctx.SetParams(argus)
rs.handleMethod(ctx)
}
//
func NewRouter() *Router {
return &Router{
routerMap: make(map[string][]*routerStmt),
}
}
//
func (r *Router) len() int {
return len(r.routerMap)
}
//
func (r *Router) setOptimizationPath(optiFunc func(string) string) {
r.optimizationPath = optiFunc
}
//
func (r *Router) initRouter() error {
for k, rStmts := range r.routerMap {
switch k {
case strGET:
for _, rStmt := range rStmts {
if r.optimizationPath != nil{
rStmt.urlPath = r.optimizationPath(rStmt.urlPath)
}
r.Router.GET(rStmt.urlPath,rStmt.handle)
}
case strPOST:
for _, rStmt := range rStmts {
if r.optimizationPath != nil{
rStmt.urlPath = r.optimizationPath(rStmt.urlPath)
}
r.Router.POST(rStmt.urlPath,rStmt.handle)
}
case strHEAD:
for _, rStmt := range rStmts {
if r.optimizationPath != nil{
rStmt.urlPath = r.optimizationPath(rStmt.urlPath)
}
r.Router.HEAD(rStmt.urlPath,rStmt.handle)
}
case strOPTIONS:
for _, rStmt := range rStmts {
if r.optimizationPath != nil{
rStmt.urlPath = r.optimizationPath(rStmt.urlPath)
}
r.Router.OPTIONS(rStmt.urlPath,rStmt.handle)
}
case strPUT:
for _, rStmt := range rStmts {
if r.optimizationPath != nil{
rStmt.urlPath = r.optimizationPath(rStmt.urlPath)
}
r.Router.PUT(rStmt.urlPath,rStmt.handle)
}
case strPATCH:
for _, rStmt := range rStmts {
if r.optimizationPath != nil{
rStmt.urlPath = r.optimizationPath(rStmt.urlPath)
}
r.Router.PATCH(rStmt.urlPath,rStmt.handle)
}
case strDELETE:
for _, rStmt := range rStmts {
if r.optimizationPath != nil{
rStmt.urlPath = r.optimizationPath(rStmt.urlPath)
}
r.Router.DELETE(rStmt.urlPath,rStmt.handle)
}
default:
return fmt.Errorf("Invalid request method: %v", k)
}
}
return nil
}
//
func (r *Router) GET(path string, m Method) {
r.routerMap[strGET] = append(r.routerMap[strGET], &routerStmt{
urlPath: path,
handleMethod: m,
})
}
func (r *Router) POST(path string, m Method) {
r.routerMap[strPOST] = append(r.routerMap[strPOST], &routerStmt{
urlPath: path,
handleMethod: m,
})
}
func (r *Router) HEAD(path string, m Method) {
r.routerMap[strHEAD] = append(r.routerMap[strHEAD], &routerStmt{
urlPath: path,
handleMethod: m,
})
}
func (r *Router) OPTIONS(path string, m Method) {
r.routerMap[strOPTIONS] = append(r.routerMap[strOPTIONS], &routerStmt{
urlPath: path,
handleMethod: m,
})
}
func (r *Router) PUT(path string, m Method) {
r.routerMap[strPUT] = append(r.routerMap[strPUT], &routerStmt{
urlPath: path,
handleMethod: m,
})
}
func (r *Router) PATCH(path string, m Method) {
r.routerMap[strPATCH] = append(r.routerMap[strPATCH], &routerStmt{
urlPath: path,
handleMethod: m,
})
}
func (r *Router) DELETE(path string, m Method) {
r.routerMap[strDELETE] = append(r.routerMap[strDELETE], &routerStmt{
urlPath: path,
handleMethod: m,
})
}