Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
48cefc67
authored
2013-12-18 21:32:25 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
improve performance change reflect to interface
1 parent
079a4113
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
32 deletions
controller.go
router.go
controller.go
View file @
48cefc6
...
...
@@ -55,6 +55,8 @@ type ControllerInterface interface {
Options
()
Finish
()
Render
()
error
XsrfToken
()
string
CheckXsrfCookie
()
bool
}
func
(
c
*
Controller
)
Init
(
ctx
*
context
.
Context
,
controllerName
,
actionName
string
,
app
interface
{})
{
...
...
router.go
View file @
48cefc6
...
...
@@ -667,50 +667,45 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
//Invoke the request handler
vc
:=
reflect
.
New
(
runrouter
)
execController
,
ok
:=
vc
.
Interface
()
.
(
ControllerInterface
)
if
!
ok
{
panic
(
"controller is not ControllerInterface"
)
}
//call the controller init function
method
:=
vc
.
MethodByName
(
"Init"
)
in
:=
make
([]
reflect
.
Value
,
4
)
in
[
0
]
=
reflect
.
ValueOf
(
context
)
in
[
1
]
=
reflect
.
ValueOf
(
runrouter
.
Name
())
in
[
2
]
=
reflect
.
ValueOf
(
runMethod
)
in
[
3
]
=
reflect
.
ValueOf
(
vc
.
Interface
())
method
.
Call
(
in
)
execController
.
Init
(
context
,
runrouter
.
Name
(),
runMethod
,
vc
.
Interface
())
//if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
if
EnableXSRF
{
in
=
make
([]
reflect
.
Value
,
0
)
method
=
vc
.
MethodByName
(
"XsrfToken"
)
method
.
Call
(
in
)
execController
.
XsrfToken
()
if
r
.
Method
==
"POST"
||
r
.
Method
==
"DELETE"
||
r
.
Method
==
"PUT"
||
(
r
.
Method
==
"POST"
&&
(
r
.
Form
.
Get
(
"_method"
)
==
"delete"
||
r
.
Form
.
Get
(
"_method"
)
==
"put"
))
{
method
=
vc
.
MethodByName
(
"CheckXsrfCookie"
)
method
.
Call
(
in
)
execController
.
CheckXsrfCookie
()
}
}
//call prepare function
in
=
make
([]
reflect
.
Value
,
0
)
method
=
vc
.
MethodByName
(
"Prepare"
)
method
.
Call
(
in
)
execController
.
Prepare
()
if
!
w
.
started
{
//exec main logic
method
=
vc
.
MethodByName
(
runMethod
)
in
:=
make
([]
reflect
.
Value
,
0
)
method
:=
vc
.
MethodByName
(
runMethod
)
method
.
Call
(
in
)
//render template
if
!
w
.
started
&&
!
context
.
Input
.
IsWebsocket
()
{
if
AutoRender
{
method
=
vc
.
MethodByName
(
"Render"
)
callMethodWithError
(
method
,
in
)
if
err
:=
execController
.
Render
();
err
!=
nil
{
panic
(
err
)
}
}
}
}
// finish all runrouter. release resource
method
=
vc
.
MethodByName
(
"Finish"
)
method
.
Call
(
in
)
execController
.
Finish
()
//execute middleware filters
if
do_filter
(
AfterExec
)
{
...
...
@@ -805,15 +800,3 @@ func (w *responseWriter) WriteHeader(code int) {
w
.
started
=
true
w
.
writer
.
WriteHeader
(
code
)
}
// call method and panic with error if error is in result params
func
callMethodWithError
(
method
reflect
.
Value
,
params
[]
reflect
.
Value
)
{
results
:=
method
.
Call
(
params
)
if
len
(
results
)
>
0
{
for
_
,
result
:=
range
results
{
if
result
.
Type
()
==
errorType
&&
!
result
.
IsNil
()
{
panic
(
result
.
Interface
()
.
(
error
))
}
}
}
}
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
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 post a comment