d57557dc by astaxie

add AutoRouterWithPrefix

1 parent 803d91c0
...@@ -118,6 +118,14 @@ func (app *App) AutoRouter(c ControllerInterface) *App { ...@@ -118,6 +118,14 @@ func (app *App) AutoRouter(c ControllerInterface) *App {
118 return app 118 return app
119 } 119 }
120 120
121 // AutoRouterWithPrefix adds beego-defined controller handler with prefix.
122 // if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
123 // visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
124 func (app *App) AutoRouterWithPrefix(prefix string, c ControllerInterface) *App {
125 app.Handlers.AddAutoPrefix(prefix, c)
126 return app
127 }
128
121 // UrlFor creates a url with another registered controller handler with params. 129 // UrlFor creates a url with another registered controller handler with params.
122 // The endpoint is formed as path.controller.name to defined the controller method which will run. 130 // The endpoint is formed as path.controller.name to defined the controller method which will run.
123 // The values need key-pair data to assign into controller method. 131 // The values need key-pair data to assign into controller method.
......
...@@ -50,6 +50,15 @@ func (gr GroupRouters) AddRouter(pattern string, c ControllerInterface, mappingM ...@@ -50,6 +50,15 @@ func (gr GroupRouters) AddRouter(pattern string, c ControllerInterface, mappingM
50 gr = append(gr, newRG) 50 gr = append(gr, newRG)
51 } 51 }
52 52
53 func (gr GroupRouters) AddAuto(c ControllerInterface) {
54 newRG := groupRouter{
55 "",
56 c,
57 "",
58 }
59 gr = append(gr, newRG)
60 }
61
53 // AddGroupRouter with the prefix 62 // AddGroupRouter with the prefix
54 // it will register the router in BeeApp 63 // it will register the router in BeeApp
55 // the follow code is write in modules: 64 // the follow code is write in modules:
...@@ -62,7 +71,9 @@ func (gr GroupRouters) AddRouter(pattern string, c ControllerInterface, mappingM ...@@ -62,7 +71,9 @@ func (gr GroupRouters) AddRouter(pattern string, c ControllerInterface, mappingM
62 // AddRouterGroup("/admin", auth.GR) 71 // AddRouterGroup("/admin", auth.GR)
63 func AddGroupRouter(prefix string, groups GroupRouters) *App { 72 func AddGroupRouter(prefix string, groups GroupRouters) *App {
64 for _, v := range groups { 73 for _, v := range groups {
65 if v.mappingMethods != "" { 74 if v.pattern == "" {
75 BeeApp.AutoRouterWithPrefix(prefix, v.controller)
76 } else if v.mappingMethods != "" {
66 BeeApp.Router(prefix+v.pattern, v.controller, v.mappingMethods) 77 BeeApp.Router(prefix+v.pattern, v.controller, v.mappingMethods)
67 } else { 78 } else {
68 BeeApp.Router(prefix+v.pattern, v.controller) 79 BeeApp.Router(prefix+v.pattern, v.controller)
...@@ -95,6 +106,13 @@ func AutoRouter(c ControllerInterface) *App { ...@@ -95,6 +106,13 @@ func AutoRouter(c ControllerInterface) *App {
95 return BeeApp 106 return BeeApp
96 } 107 }
97 108
109 // AutoPrefix adds controller handler to BeeApp with prefix.
110 // it's same to App.AutoRouterWithPrefix.
111 func AutoPrefix(prefix string, c ControllerInterface) *App {
112 BeeApp.AutoRouterWithPrefix(prefix, c)
113 return BeeApp
114 }
115
98 // ErrorHandler registers http.HandlerFunc to each http err code string. 116 // ErrorHandler registers http.HandlerFunc to each http err code string.
99 // usage: 117 // usage:
100 // beego.ErrorHandler("404",NotFound) 118 // beego.ErrorHandler("404",NotFound)
......
...@@ -33,6 +33,14 @@ const ( ...@@ -33,6 +33,14 @@ const (
33 var ( 33 var (
34 // supported http methods. 34 // supported http methods.
35 HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"} 35 HTTPMETHOD = []string{"get", "post", "put", "delete", "patch", "options", "head"}
36 // these beego.Controller's methods shouldn't reflect to AutoRouter
37 exceptMethod = []string{"Init", "Prepare", "Finish", "Render", "RenderString",
38 "RenderBytes", "Redirect", "Abort", "StopRun", "UrlFor", "ServeJson", "ServeJsonp",
39 "ServeXml", "Input", "ParseForm", "GetString", "GetStrings", "GetInt", "GetBool",
40 "GetFloat", "GetFile", "SaveToFile", "StartSession", "SetSession", "GetSession",
41 "DelSession", "SessionRegenerateID", "DestroySession", "IsAjax", "GetSecureCookie",
42 "SetSecureCookie", "XsrfToken", "CheckXsrfCookie", "XsrfFormHtml",
43 "GetControllerAndAction"}
36 ) 44 )
37 45
38 type controllerInfo struct { 46 type controllerInfo struct {
...@@ -221,8 +229,8 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM ...@@ -221,8 +229,8 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
221 // Add auto router to ControllerRegistor. 229 // Add auto router to ControllerRegistor.
222 // example beego.AddAuto(&MainContorlller{}), 230 // example beego.AddAuto(&MainContorlller{}),
223 // MainController has method List and Page. 231 // MainController has method List and Page.
224 // visit the url /main/list to exec List function 232 // visit the url /main/list to execute List function
225 // /main/page to exec Page function. 233 // /main/page to execute Page function.
226 func (p *ControllerRegistor) AddAuto(c ControllerInterface) { 234 func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
227 p.enableAuto = true 235 p.enableAuto = true
228 reflectVal := reflect.ValueOf(c) 236 reflectVal := reflect.ValueOf(c)
...@@ -235,7 +243,32 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) { ...@@ -235,7 +243,32 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
235 p.autoRouter[firstParam] = make(map[string]reflect.Type) 243 p.autoRouter[firstParam] = make(map[string]reflect.Type)
236 } 244 }
237 for i := 0; i < rt.NumMethod(); i++ { 245 for i := 0; i < rt.NumMethod(); i++ {
238 p.autoRouter[firstParam][rt.Method(i).Name] = ct 246 if !utils.InSlice(rt.Method(i).Name, exceptMethod) {
247 p.autoRouter[firstParam][rt.Method(i).Name] = ct
248 }
249 }
250 }
251
252 // Add auto router to ControllerRegistor with prefix.
253 // example beego.AddAutoPrefix("/admin",&MainContorlller{}),
254 // MainController has method List and Page.
255 // visit the url /admin/main/list to execute List function
256 // /admin/main/page to execute Page function.
257 func (p *ControllerRegistor) AddAutoPrefix(prefix string, c ControllerInterface) {
258 p.enableAuto = true
259 reflectVal := reflect.ValueOf(c)
260 rt := reflectVal.Type()
261 ct := reflect.Indirect(reflectVal).Type()
262 firstParam := strings.Trim(prefix, "/") + "/" + strings.ToLower(strings.TrimSuffix(ct.Name(), "Controller"))
263 if _, ok := p.autoRouter[firstParam]; ok {
264 return
265 } else {
266 p.autoRouter[firstParam] = make(map[string]reflect.Type)
267 }
268 for i := 0; i < rt.NumMethod(); i++ {
269 if !utils.InSlice(rt.Method(i).Name, exceptMethod) {
270 p.autoRouter[firstParam][rt.Method(i).Name] = ct
271 }
239 } 272 }
240 } 273 }
241 274
......
...@@ -198,3 +198,15 @@ func TestPrepare(t *testing.T) { ...@@ -198,3 +198,15 @@ func TestPrepare(t *testing.T) {
198 t.Errorf(w.Body.String() + "user define func can't run") 198 t.Errorf(w.Body.String() + "user define func can't run")
199 } 199 }
200 } 200 }
201
202 func TestAutoPrefix(t *testing.T) {
203 r, _ := http.NewRequest("GET", "/admin/test/list", nil)
204 w := httptest.NewRecorder()
205
206 handler := NewControllerRegistor()
207 handler.AddAutoPrefix("/admin", &TestController{})
208 handler.ServeHTTP(w, r)
209 if w.Body.String() != "i am list" {
210 t.Errorf("TestAutoPrefix can't run")
211 }
212 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!