Skip to content

Commit d57557d

Browse files
committed
add AutoRouterWithPrefix
1 parent 803d91c commit d57557d

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

Diff for: app.go

+8
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ func (app *App) AutoRouter(c ControllerInterface) *App {
118118
return app
119119
}
120120

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+
121129
// UrlFor creates a url with another registered controller handler with params.
122130
// The endpoint is formed as path.controller.name to defined the controller method which will run.
123131
// The values need key-pair data to assign into controller method.

Diff for: beego.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ func (gr GroupRouters) AddRouter(pattern string, c ControllerInterface, mappingM
5050
gr = append(gr, newRG)
5151
}
5252

53+
func (gr GroupRouters) AddAuto(c ControllerInterface) {
54+
newRG := groupRouter{
55+
"",
56+
c,
57+
"",
58+
}
59+
gr = append(gr, newRG)
60+
}
61+
5362
// AddGroupRouter with the prefix
5463
// it will register the router in BeeApp
5564
// the follow code is write in modules:
@@ -62,7 +71,9 @@ func (gr GroupRouters) AddRouter(pattern string, c ControllerInterface, mappingM
6271
// AddRouterGroup("/admin", auth.GR)
6372
func AddGroupRouter(prefix string, groups GroupRouters) *App {
6473
for _, v := range groups {
65-
if v.mappingMethods != "" {
74+
if v.pattern == "" {
75+
BeeApp.AutoRouterWithPrefix(prefix, v.controller)
76+
} else if v.mappingMethods != "" {
6677
BeeApp.Router(prefix+v.pattern, v.controller, v.mappingMethods)
6778
} else {
6879
BeeApp.Router(prefix+v.pattern, v.controller)
@@ -95,6 +106,13 @@ func AutoRouter(c ControllerInterface) *App {
95106
return BeeApp
96107
}
97108

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+
98116
// ErrorHandler registers http.HandlerFunc to each http err code string.
99117
// usage:
100118
// beego.ErrorHandler("404",NotFound)

Diff for: router.go

+36-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ const (
3333
var (
3434
// supported http methods.
3535
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"}
3644
)
3745

3846
type controllerInfo struct {
@@ -221,8 +229,8 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
221229
// Add auto router to ControllerRegistor.
222230
// example beego.AddAuto(&MainContorlller{}),
223231
// MainController has method List and Page.
224-
// visit the url /main/list to exec List function
225-
// /main/page to exec Page function.
232+
// visit the url /main/list to execute List function
233+
// /main/page to execute Page function.
226234
func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
227235
p.enableAuto = true
228236
reflectVal := reflect.ValueOf(c)
@@ -235,7 +243,32 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
235243
p.autoRouter[firstParam] = make(map[string]reflect.Type)
236244
}
237245
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+
}
239272
}
240273
}
241274

Diff for: router_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,15 @@ func TestPrepare(t *testing.T) {
198198
t.Errorf(w.Body.String() + "user define func can't run")
199199
}
200200
}
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+
}

0 commit comments

Comments
 (0)