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
f7e7fab6
Commit
f7e7fab6
authored
Jul 27, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support autorouter
parent
fbde7df4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
1 deletion
+81
-1
beego.go
beego.go
+10
-0
router.go
router.go
+71
-1
No files found.
beego.go
View file @
f7e7fab6
...
@@ -138,6 +138,11 @@ func (app *App) Router(path string, c ControllerInterface, mappingMethods ...str
...
@@ -138,6 +138,11 @@ func (app *App) Router(path string, c ControllerInterface, mappingMethods ...str
return
app
return
app
}
}
func
(
app
*
App
)
AutoRouter
(
c
ControllerInterface
)
*
App
{
app
.
Handlers
.
AddAuto
(
c
)
return
app
}
func
(
app
*
App
)
Filter
(
filter
http
.
HandlerFunc
)
*
App
{
func
(
app
*
App
)
Filter
(
filter
http
.
HandlerFunc
)
*
App
{
app
.
Handlers
.
Filter
(
filter
)
app
.
Handlers
.
Filter
(
filter
)
return
app
return
app
...
@@ -192,6 +197,11 @@ func RESTRouter(rootpath string, c ControllerInterface) *App {
...
@@ -192,6 +197,11 @@ func RESTRouter(rootpath string, c ControllerInterface) *App {
return
BeeApp
return
BeeApp
}
}
func
AutoRouter
(
c
ControllerInterface
)
*
App
{
BeeApp
.
AutoRouter
(
c
)
return
BeeApp
}
func
RouterHandler
(
path
string
,
c
http
.
Handler
)
*
App
{
func
RouterHandler
(
path
string
,
c
http
.
Handler
)
*
App
{
BeeApp
.
Handlers
.
AddHandler
(
path
,
c
)
BeeApp
.
Handlers
.
AddHandler
(
path
,
c
)
return
BeeApp
return
BeeApp
...
...
router.go
View file @
f7e7fab6
...
@@ -10,6 +10,7 @@ import (
...
@@ -10,6 +10,7 @@ import (
"reflect"
"reflect"
"regexp"
"regexp"
"runtime"
"runtime"
"strconv"
"strings"
"strings"
)
)
...
@@ -35,10 +36,15 @@ type ControllerRegistor struct {
...
@@ -35,10 +36,15 @@ type ControllerRegistor struct {
fixrouters
[]
*
controllerInfo
fixrouters
[]
*
controllerInfo
filters
[]
http
.
HandlerFunc
filters
[]
http
.
HandlerFunc
userHandlers
map
[
string
]
*
userHandler
userHandlers
map
[
string
]
*
userHandler
autoRouter
map
[
string
]
map
[
string
]
reflect
.
Type
//key:controller key:method value:reflect.type
}
}
func
NewControllerRegistor
()
*
ControllerRegistor
{
func
NewControllerRegistor
()
*
ControllerRegistor
{
return
&
ControllerRegistor
{
routers
:
make
([]
*
controllerInfo
,
0
),
userHandlers
:
make
(
map
[
string
]
*
userHandler
)}
return
&
ControllerRegistor
{
routers
:
make
([]
*
controllerInfo
,
0
),
userHandlers
:
make
(
map
[
string
]
*
userHandler
),
autoRouter
:
make
(
map
[
string
]
map
[
string
]
reflect
.
Type
),
}
}
}
//methods support like this:
//methods support like this:
...
@@ -148,6 +154,21 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
...
@@ -148,6 +154,21 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface, mappingM
}
}
}
}
func
(
p
*
ControllerRegistor
)
AddAuto
(
c
ControllerInterface
)
{
reflectVal
:=
reflect
.
ValueOf
(
c
)
rt
:=
reflectVal
.
Type
()
ct
:=
reflect
.
Indirect
(
reflectVal
)
.
Type
()
firstParam
:=
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
++
{
p
.
autoRouter
[
firstParam
][
rt
.
Method
(
i
)
.
Name
]
=
ct
}
}
func
(
p
*
ControllerRegistor
)
AddHandler
(
pattern
string
,
c
http
.
Handler
)
{
func
(
p
*
ControllerRegistor
)
AddHandler
(
pattern
string
,
c
http
.
Handler
)
{
parts
:=
strings
.
Split
(
pattern
,
"/"
)
parts
:=
strings
.
Split
(
pattern
,
"/"
)
...
@@ -518,6 +539,55 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
...
@@ -518,6 +539,55 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
method
.
Call
(
in
)
method
.
Call
(
in
)
}
}
//start autorouter
if
!
findrouter
{
for
cName
,
methodmap
:=
range
p
.
autoRouter
{
if
strings
.
HasPrefix
(
strings
.
ToLower
(
requestPath
),
"/"
+
cName
)
{
for
mName
,
controllerType
:=
range
methodmap
{
if
strings
.
HasPrefix
(
strings
.
ToLower
(
requestPath
),
"/"
+
cName
+
"/"
+
strings
.
ToLower
(
mName
))
{
//parse params
otherurl
:=
requestPath
[
len
(
"/"
+
cName
+
"/"
+
strings
.
ToLower
(
mName
))
:
]
if
len
(
otherurl
)
>
1
&&
otherurl
[
0
]
!=
'/'
{
plist
:=
strings
.
Split
(
otherurl
,
"/"
)
for
k
,
v
:=
range
plist
{
params
[
strconv
.
Itoa
(
k
)]
=
v
}
}
//Invoke the request handler
vc
:=
reflect
.
New
(
controllerType
)
//call the controller init function
init
:=
vc
.
MethodByName
(
"Init"
)
in
:=
make
([]
reflect
.
Value
,
2
)
ct
:=
&
Context
{
ResponseWriter
:
w
,
Request
:
r
,
Params
:
params
,
RequestBody
:
requestbody
}
in
[
0
]
=
reflect
.
ValueOf
(
ct
)
in
[
1
]
=
reflect
.
ValueOf
(
controllerType
.
Name
())
init
.
Call
(
in
)
//call prepare function
in
=
make
([]
reflect
.
Value
,
0
)
method
:=
vc
.
MethodByName
(
"Prepare"
)
method
.
Call
(
in
)
method
=
vc
.
MethodByName
(
mName
)
method
.
Call
(
in
)
if
!
w
.
started
{
if
AutoRender
{
method
=
vc
.
MethodByName
(
"Render"
)
method
.
Call
(
in
)
}
method
=
vc
.
MethodByName
(
"Finish"
)
method
.
Call
(
in
)
}
method
=
vc
.
MethodByName
(
"Destructor"
)
method
.
Call
(
in
)
// set find
findrouter
=
true
}
}
}
}
}
//if no matches to url, throw a not found exception
//if no matches to url, throw a not found exception
if
!
findrouter
{
if
!
findrouter
{
if
h
,
ok
:=
ErrorMaps
[
"404"
];
ok
{
if
h
,
ok
:=
ErrorMaps
[
"404"
];
ok
{
...
...
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