Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
d57557dc
Commit
d57557dc
authored
Jan 01, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add AutoRouterWithPrefix
parent
803d91c0
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
3 deletions
+74
-3
app.go
app.go
+8
-0
beego.go
beego.go
+19
-1
router.go
router.go
+35
-2
router_test.go
router_test.go
+12
-0
No files found.
app.go
View file @
d57557dc
...
@@ -118,6 +118,14 @@ func (app *App) AutoRouter(c ControllerInterface) *App {
...
@@ -118,6 +118,14 @@ func (app *App) AutoRouter(c ControllerInterface) *App {
return
app
return
app
}
}
// AutoRouterWithPrefix adds beego-defined controller handler with prefix.
// if beego.AutoPrefix("/admin",&MainContorlller{}) and MainController has methods List and Page,
// visit the url /admin/main/list to exec List function or /admin/main/page to exec Page function.
func
(
app
*
App
)
AutoRouterWithPrefix
(
prefix
string
,
c
ControllerInterface
)
*
App
{
app
.
Handlers
.
AddAutoPrefix
(
prefix
,
c
)
return
app
}
// UrlFor creates a url with another registered controller handler with params.
// UrlFor creates a url with another registered controller handler with params.
// The endpoint is formed as path.controller.name to defined the controller method which will run.
// The endpoint is formed as path.controller.name to defined the controller method which will run.
// The values need key-pair data to assign into controller method.
// The values need key-pair data to assign into controller method.
...
...
beego.go
View file @
d57557dc
...
@@ -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
gr
=
append
(
gr
,
newRG
)
gr
=
append
(
gr
,
newRG
)
}
}
func
(
gr
GroupRouters
)
AddAuto
(
c
ControllerInterface
)
{
newRG
:=
groupRouter
{
""
,
c
,
""
,
}
gr
=
append
(
gr
,
newRG
)
}
// AddGroupRouter with the prefix
// AddGroupRouter with the prefix
// it will register the router in BeeApp
// it will register the router in BeeApp
// the follow code is write in modules:
// 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
// AddRouterGroup("/admin", auth.GR)
// AddRouterGroup("/admin", auth.GR)
func
AddGroupRouter
(
prefix
string
,
groups
GroupRouters
)
*
App
{
func
AddGroupRouter
(
prefix
string
,
groups
GroupRouters
)
*
App
{
for
_
,
v
:=
range
groups
{
for
_
,
v
:=
range
groups
{
if
v
.
mappingMethods
!=
""
{
if
v
.
pattern
==
""
{
BeeApp
.
AutoRouterWithPrefix
(
prefix
,
v
.
controller
)
}
else
if
v
.
mappingMethods
!=
""
{
BeeApp
.
Router
(
prefix
+
v
.
pattern
,
v
.
controller
,
v
.
mappingMethods
)
BeeApp
.
Router
(
prefix
+
v
.
pattern
,
v
.
controller
,
v
.
mappingMethods
)
}
else
{
}
else
{
BeeApp
.
Router
(
prefix
+
v
.
pattern
,
v
.
controller
)
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 {
return
BeeApp
return
BeeApp
}
}
// AutoPrefix adds controller handler to BeeApp with prefix.
// it's same to App.AutoRouterWithPrefix.
func
AutoPrefix
(
prefix
string
,
c
ControllerInterface
)
*
App
{
BeeApp
.
AutoRouterWithPrefix
(
prefix
,
c
)
return
BeeApp
}
// ErrorHandler registers http.HandlerFunc to each http err code string.
// ErrorHandler registers http.HandlerFunc to each http err code string.
// usage:
// usage:
// beego.ErrorHandler("404",NotFound)
// beego.ErrorHandler("404",NotFound)
...
...
router.go
View file @
d57557dc
...
@@ -33,6 +33,14 @@ const (
...
@@ -33,6 +33,14 @@ const (
var
(
var
(
// supported http methods.
// supported http methods.
HTTPMETHOD
=
[]
string
{
"get"
,
"post"
,
"put"
,
"delete"
,
"patch"
,
"options"
,
"head"
}
HTTPMETHOD
=
[]
string
{
"get"
,
"post"
,
"put"
,
"delete"
,
"patch"
,
"options"
,
"head"
}
// these beego.Controller's methods shouldn't reflect to AutoRouter
exceptMethod
=
[]
string
{
"Init"
,
"Prepare"
,
"Finish"
,
"Render"
,
"RenderString"
,
"RenderBytes"
,
"Redirect"
,
"Abort"
,
"StopRun"
,
"UrlFor"
,
"ServeJson"
,
"ServeJsonp"
,
"ServeXml"
,
"Input"
,
"ParseForm"
,
"GetString"
,
"GetStrings"
,
"GetInt"
,
"GetBool"
,
"GetFloat"
,
"GetFile"
,
"SaveToFile"
,
"StartSession"
,
"SetSession"
,
"GetSession"
,
"DelSession"
,
"SessionRegenerateID"
,
"DestroySession"
,
"IsAjax"
,
"GetSecureCookie"
,
"SetSecureCookie"
,
"XsrfToken"
,
"CheckXsrfCookie"
,
"XsrfFormHtml"
,
"GetControllerAndAction"
}
)
)
type
controllerInfo
struct
{
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
// Add auto router to ControllerRegistor.
// Add auto router to ControllerRegistor.
// example beego.AddAuto(&MainContorlller{}),
// example beego.AddAuto(&MainContorlller{}),
// MainController has method List and Page.
// MainController has method List and Page.
// visit the url /main/list to exec List function
// visit the url /main/list to exec
ute
List function
// /main/page to exec Page function.
// /main/page to exec
ute
Page function.
func
(
p
*
ControllerRegistor
)
AddAuto
(
c
ControllerInterface
)
{
func
(
p
*
ControllerRegistor
)
AddAuto
(
c
ControllerInterface
)
{
p
.
enableAuto
=
true
p
.
enableAuto
=
true
reflectVal
:=
reflect
.
ValueOf
(
c
)
reflectVal
:=
reflect
.
ValueOf
(
c
)
...
@@ -235,8 +243,33 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
...
@@ -235,8 +243,33 @@ func (p *ControllerRegistor) AddAuto(c ControllerInterface) {
p
.
autoRouter
[
firstParam
]
=
make
(
map
[
string
]
reflect
.
Type
)
p
.
autoRouter
[
firstParam
]
=
make
(
map
[
string
]
reflect
.
Type
)
}
}
for
i
:=
0
;
i
<
rt
.
NumMethod
();
i
++
{
for
i
:=
0
;
i
<
rt
.
NumMethod
();
i
++
{
if
!
utils
.
InSlice
(
rt
.
Method
(
i
)
.
Name
,
exceptMethod
)
{
p
.
autoRouter
[
firstParam
][
rt
.
Method
(
i
)
.
Name
]
=
ct
p
.
autoRouter
[
firstParam
][
rt
.
Method
(
i
)
.
Name
]
=
ct
}
}
}
}
// Add auto router to ControllerRegistor with prefix.
// example beego.AddAutoPrefix("/admin",&MainContorlller{}),
// MainController has method List and Page.
// visit the url /admin/main/list to execute List function
// /admin/main/page to execute Page function.
func
(
p
*
ControllerRegistor
)
AddAutoPrefix
(
prefix
string
,
c
ControllerInterface
)
{
p
.
enableAuto
=
true
reflectVal
:=
reflect
.
ValueOf
(
c
)
rt
:=
reflectVal
.
Type
()
ct
:=
reflect
.
Indirect
(
reflectVal
)
.
Type
()
firstParam
:=
strings
.
Trim
(
prefix
,
"/"
)
+
"/"
+
strings
.
ToLower
(
strings
.
TrimSuffix
(
ct
.
Name
(),
"Controller"
))
if
_
,
ok
:=
p
.
autoRouter
[
firstParam
];
ok
{
return
}
else
{
p
.
autoRouter
[
firstParam
]
=
make
(
map
[
string
]
reflect
.
Type
)
}
for
i
:=
0
;
i
<
rt
.
NumMethod
();
i
++
{
if
!
utils
.
InSlice
(
rt
.
Method
(
i
)
.
Name
,
exceptMethod
)
{
p
.
autoRouter
[
firstParam
][
rt
.
Method
(
i
)
.
Name
]
=
ct
}
}
}
}
// [Deprecated] use InsertFilter.
// [Deprecated] use InsertFilter.
...
...
router_test.go
View file @
d57557dc
...
@@ -198,3 +198,15 @@ func TestPrepare(t *testing.T) {
...
@@ -198,3 +198,15 @@ func TestPrepare(t *testing.T) {
t
.
Errorf
(
w
.
Body
.
String
()
+
"user define func can't run"
)
t
.
Errorf
(
w
.
Body
.
String
()
+
"user define func can't run"
)
}
}
}
}
func
TestAutoPrefix
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/admin/test/list"
,
nil
)
w
:=
httptest
.
NewRecorder
()
handler
:=
NewControllerRegistor
()
handler
.
AddAutoPrefix
(
"/admin"
,
&
TestController
{})
handler
.
ServeHTTP
(
w
,
r
)
if
w
.
Body
.
String
()
!=
"i am list"
{
t
.
Errorf
(
"TestAutoPrefix can't run"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment