forked from Odinman/ogo
/
controller.go
317 lines (280 loc) · 7.49 KB
/
controller.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Ogo
package ogo
import (
"fmt"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"net/http"
"strings"
)
type Handler func(c *RESTContext)
type RouteOption map[string]interface{}
type Route struct {
Pattern interface{}
Method string
Handler Handler
Options RouteOption
}
type Controller struct {
Endpoint string
Routes map[string]*Route
SRoutes []*Route //排序的Route
ReqCount int //访问计数
Mux *Mux
}
type ControllerInterface interface {
//Init(endpoint string, c ControllerInterface)
Init(c ControllerInterface)
New(mux *Mux, endpoint string)
GetEndpoint() string
Get(c *RESTContext)
Post(c *RESTContext)
Put(c *RESTContext)
Delete(c *RESTContext)
Patch(c *RESTContext)
Head(c *RESTContext)
Options(c *RESTContext)
Trace(c *RESTContext)
NotFound(c *RESTContext)
AddRoute(m string, p interface{}, h Handler, options ...map[string]interface{})
}
func NewRoute(p interface{}, m string, h Handler, options ...map[string]interface{}) *Route {
r := &Route{
Pattern: p,
Method: m,
Handler: h,
Options: make(map[string]interface{}),
}
if len(options) > 0 { //不管有几个,目前只有第一个有效
r.Options = options[0]
}
return r
}
func getRouteKey(rt *Route) (key string) {
key = fmt.Sprint(strings.ToUpper(rt.Method), " ", rt.Pattern)
return
}
// 封装
//func handlerWrap(f Handler) web.HandlerFunc { //这里封装了webC到本地的结构中
func handlerWrap(rt *Route) web.HandlerFunc { //这里封装了webC到本地的结构中
fn := func(c web.C, w http.ResponseWriter, r *http.Request) {
// build newest RESTContext
rc := rcHolder(c, w, r)
//route
rc.Route = rt
if nl, ok := rt.Options[NoLogKey]; ok && nl == true {
rc.SetEnv(NoLogKey, true)
}
// pre hooks, 任何一个出错,都要结束
if hl := len(DMux.Hooks.preHooks); hl > 0 {
for i := 0; i < hl; i++ {
if err := DMux.Hooks.preHooks[i](rc); err != nil {
rc.RESTError(err)
return
}
}
}
// 执行业务handler
rt.Handler(rc)
// post hooks
if hl := len(DMux.Hooks.postHooks); hl > 0 {
for i := 0; i < hl; i++ {
DMux.Hooks.postHooks[i](rc)
}
}
}
return fn
}
/* {{{ func (ctr *Controller) New(mux *Mux, endpoint string)
*
*/
func (ctr *Controller) New(mux *Mux, endpoint string) {
ctr.Mux = mux
ctr.Endpoint = endpoint
}
/* }}} */
/* {{{ func (ctr *Controller) GetEndpoint() string
*
*/
func (ctr *Controller) GetEndpoint() string {
return ctr.Endpoint
}
/* }}} */
//func (ctr *Controller) Init(endpoint string, c ControllerInterface) {
func (ctr *Controller) Init(c ControllerInterface) {
//ctr.Endpoint = endpoint
ctr.DefaultRoutes(c) //默认路由
if len(ctr.Routes) > 0 {
for _, rt := range ctr.SRoutes {
//Debug("pattern: %s", rt.Pattern)
key := getRouteKey(rt)
// regist routes to Mux
ctr.Mux.Routes[key] = rt
switch strings.ToLower(rt.Method) {
case "get":
ctr.RouteGet(rt)
case "post":
ctr.RoutePost(rt)
case "put":
ctr.RoutePut(rt)
case "delete":
ctr.RouteDelete(rt)
case "patch":
ctr.RoutePatch(rt)
case "head":
ctr.RouteHead(rt)
default:
// unknow method
}
}
}
// not found
notFoundRoute := &Route{
Handler: c.NotFound,
Options: map[string]interface{}{NoLogKey: true},
}
ctr.RouteNotFound(notFoundRoute)
}
func (ctr *Controller) Get(c *RESTContext) {
c.HTTPError(http.StatusMethodNotAllowed)
}
func (ctr *Controller) Post(c *RESTContext) {
c.HTTPError(http.StatusMethodNotAllowed)
}
func (ctr *Controller) Put(c *RESTContext) {
c.HTTPError(http.StatusMethodNotAllowed)
}
func (ctr *Controller) Delete(c *RESTContext) {
c.HTTPError(http.StatusMethodNotAllowed)
}
func (ctr *Controller) Patch(c *RESTContext) {
c.HTTPError(http.StatusMethodNotAllowed)
}
func (ctr *Controller) Head(c *RESTContext) {
c.HTTPError(http.StatusMethodNotAllowed)
}
func (ctr *Controller) Options(c *RESTContext) {
c.HTTPError(http.StatusMethodNotAllowed)
}
func (ctr *Controller) Trace(c *RESTContext) {
c.HTTPError(http.StatusMethodNotAllowed)
}
func (ctr *Controller) NotFound(c *RESTContext) {
c.HTTPError(http.StatusNotFound)
}
func (ctr *Controller) AddRoute(m string, p interface{}, h Handler, options ...map[string]interface{}) {
key := fmt.Sprint(strings.ToUpper(m), " ", p)
if ctr.Routes == nil {
ctr.Routes = make(map[string]*Route)
ctr.SRoutes = make([]*Route, 0)
}
if _, ok := ctr.Routes[key]; ok {
//手动加路由, 如果冲突则以最早的为准
Info("route dup: %s", key)
} else {
ctr.Routes[key] = NewRoute(p, m, h, options...)
ctr.SRoutes = append(ctr.SRoutes, ctr.Routes[key])
}
}
// controller default route
// 默认路由, 如果已经定义了则忽略,没有定义则加上
func (ctr *Controller) DefaultRoutes(c ControllerInterface) {
if ctr.Endpoint == "" {
//没有endpoint,不需要默认路由
Info("Not need default Routes because no endpoint")
return
}
var pattern, method, key string
// GET /{endpoint}
pattern = "/" + ctr.Endpoint
method = "GET"
key = method + " " + pattern
if ctr.Routes == nil {
ctr.Routes = make(map[string]*Route)
ctr.SRoutes = make([]*Route, 0)
}
if _, ok := ctr.Routes[key]; ok {
// exists, warning, 默认路由不能覆盖自定义路由
Warn("default route dup: %s", key)
} else {
rt := NewRoute(pattern, method, c.Get)
ctr.Routes[key] = rt
ctr.SRoutes = append(ctr.SRoutes, rt)
}
// GET /{endpoint}/{id}
pattern = "/" + ctr.Endpoint + "/:_id_"
method = "GET"
key = method + " " + pattern
if _, ok := ctr.Routes[key]; ok {
// exists, warning, 默认路由不能覆盖自定义路由
} else {
rt := NewRoute(pattern, method, c.Get)
ctr.Routes[key] = rt
ctr.SRoutes = append(ctr.SRoutes, rt)
}
// POST /{endpoint}
pattern = "/" + ctr.Endpoint
method = "POST"
key = method + " " + pattern
if _, ok := ctr.Routes[key]; ok {
// exists, warning, 默认路由不能覆盖自定义路由
} else {
rt := NewRoute(pattern, method, c.Post)
ctr.Routes[key] = rt
ctr.SRoutes = append(ctr.SRoutes, rt)
}
// DELETE /{endpoint}/{id}
pattern = "/" + ctr.Endpoint + "/:_id_"
method = "DELETE"
key = method + " " + pattern
if _, ok := ctr.Routes[key]; ok {
// exists, warning, 默认路由不能覆盖自定义路由
} else {
rt := NewRoute(pattern, method, c.Delete)
ctr.Routes[key] = rt
ctr.SRoutes = append(ctr.SRoutes, rt)
}
// PATCH /{endpoint}/{id}
pattern = "/" + ctr.Endpoint + "/:_id_"
method = "PATCH"
key = method + " " + pattern
if _, ok := ctr.Routes[key]; ok {
// exists, warning, 默认路由不能覆盖自定义路由
} else {
rt := NewRoute(pattern, method, c.Patch)
ctr.Routes[key] = rt
ctr.SRoutes = append(ctr.SRoutes, rt)
}
// PUT /{endpoint}/{id}
pattern = "/" + ctr.Endpoint + "/:_id_"
method = "PUT"
key = method + " " + pattern
if _, ok := ctr.Routes[key]; ok {
// exists, warning, 默认路由不能覆盖自定义路由
} else {
rt := NewRoute(pattern, method, c.Put)
ctr.Routes[key] = rt
ctr.SRoutes = append(ctr.SRoutes, rt)
}
}
func (ctr *Controller) RouteGet(rt *Route) {
goji.Get(rt.Pattern, handlerWrap(rt))
}
func (ctr *Controller) RoutePost(rt *Route) {
goji.Post(rt.Pattern, handlerWrap(rt))
}
func (ctr *Controller) RoutePut(rt *Route) {
goji.Put(rt.Pattern, handlerWrap(rt))
}
func (ctr *Controller) RouteDelete(rt *Route) {
goji.Delete(rt.Pattern, handlerWrap(rt))
}
func (ctr *Controller) RoutePatch(rt *Route) {
goji.Patch(rt.Pattern, handlerWrap(rt))
}
func (ctr *Controller) RouteHead(rt *Route) {
goji.Head(rt.Pattern, handlerWrap(rt))
}
func (ctr *Controller) RouteNotFound(rt *Route) {
goji.NotFound(handlerWrap(rt))
}